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 as published by
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
30 #include <lttng/lttng.h>
31 #include <common/error.h>
36 static char *progname
;
40 static int opt_no_sessiond
;
41 static char *opt_sessiond_path
;
42 static pid_t sessiond_pid
;
43 static volatile int recv_child_signal
;
51 /* Getopt options. No first level command. */
52 static struct option long_options
[] = {
53 {"help", 0, NULL
, 'h'},
54 {"group", 1, NULL
, 'g'},
55 {"verbose", 0, NULL
, 'v'},
56 {"quiet", 0, NULL
, 'q'},
57 {"no-sessiond", 0, NULL
, 'n'},
58 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
59 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
60 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
64 /* First level command */
65 static struct cmd_struct commands
[] = {
67 { "create", cmd_create
},
68 { "destroy", cmd_destroy
},
69 { "start", cmd_start
},
71 { "enable-event", cmd_enable_events
},
72 { "disable-event", cmd_disable_events
},
73 { "enable-channel", cmd_enable_channels
},
74 { "disable-channel", cmd_disable_channels
},
75 { "add-context", cmd_add_context
},
76 { "set-session", cmd_set_session
},
77 { "version", cmd_version
},
78 { "calibrate", cmd_calibrate
},
79 { NULL
, NULL
} /* Array closure */
82 static void usage(FILE *ofp
)
84 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
85 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND>\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
, " list List possible tracing options\n");
107 fprintf(ofp
, " set-session Set current session name\n");
108 fprintf(ofp
, " start Start tracing\n");
109 fprintf(ofp
, " stop Stop tracing\n");
110 fprintf(ofp
, " version Show version information\n");
112 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
114 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
115 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
121 * List options line by line. This is mostly for bash auto completion and to
122 * avoid difficult parsing.
124 static void list_options(FILE *ofp
)
127 struct option
*option
= NULL
;
129 option
= &long_options
[i
];
130 while (option
->name
!= NULL
) {
131 fprintf(ofp
, "--%s\n", option
->name
);
133 if (isprint(option
->val
)) {
134 fprintf(ofp
, "-%c\n", option
->val
);
138 option
= &long_options
[i
];
145 * List commands line by line. This is mostly for bash auto completion and to
146 * avoid difficult parsing.
148 static void list_commands(FILE *ofp
)
151 struct cmd_struct
*cmd
= NULL
;
154 while (cmd
->name
!= NULL
) {
155 fprintf(ofp
, "%s\n", cmd
->name
);
164 static void clean_exit(int code
)
173 * Signal handler for the daemon
175 static void sighandler(int sig
)
181 DBG("SIGTERM caught");
182 clean_exit(EXIT_FAILURE
);
185 DBG("SIGCHLD caught");
186 waitpid(sessiond_pid
, &status
, 0);
187 recv_child_signal
= 1;
188 /* Indicate that the session daemon died */
190 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
194 recv_child_signal
= 1;
195 DBG("SIGUSR1 caught");
198 DBG("Unknown signal %d caught", sig
);
208 * Setup signal handler for SIGCHLD and SIGTERM.
210 static int set_signal_handler(void)
216 if ((ret
= sigemptyset(&sigset
)) < 0) {
217 perror("sigemptyset");
221 sa
.sa_handler
= sighandler
;
224 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
229 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
234 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
246 * Handle the full argv list of a first level command. Will find the command
247 * in the global commands array and call the function callback associated.
249 * If command not found, return -1
250 * else, return function command error code.
252 static int handle_command(int argc
, char **argv
)
255 struct cmd_struct
*cmd
;
263 while (cmd
->func
!= NULL
) {
265 if (strcmp(argv
[0], cmd
->name
) == 0) {
266 ret
= cmd
->func(argc
, (const char**) argv
);
269 WARN("Some command(s) went wrong");
272 ERR("Command error");
275 ERR("Undefined command");
287 /* Command not found */
297 * Spawn a session daemon by forking and execv.
299 static int spawn_sessiond(char *pathname
)
304 MSG("Spawning a session daemon");
305 recv_child_signal
= 0;
309 * Spawn session daemon and tell
310 * it to signal us when ready.
312 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
313 /* execlp only returns if error happened */
314 if (errno
== ENOENT
) {
315 ERR("No session daemon found. Use --sessiond-path.");
319 kill(getppid(), SIGTERM
); /* wake parent */
321 } else if (pid
> 0) {
324 * Wait for lttng-sessiond to start. We need to use a flag to check if
325 * the signal has been sent to us, because the child can be scheduled
326 * before the parent, and thus send the signal before this check. In
327 * the signal handler, we set the recv_child_signal flag, so anytime we
328 * check it after the fork is fine. Note that sleep() is interrupted
329 * before the 1 second delay as soon as the signal is received, so it
330 * will not cause visible delay for the user.
332 while (!recv_child_signal
) {
336 * The signal handler will nullify sessiond_pid on SIGCHLD
355 * Check if the session daemon is available using
356 * the liblttngctl API for the check. If not, try to
359 static int check_sessiond(void)
362 char *pathname
= NULL
, *alloc_pathname
= NULL
;
364 ret
= lttng_session_daemon_alive();
365 if (ret
== 0) { /* not alive */
366 /* Try command line option path */
367 if (opt_sessiond_path
!= NULL
) {
368 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
370 ERR("No such file or access denied: %s", opt_sessiond_path
);
373 pathname
= opt_sessiond_path
;
375 /* Try LTTNG_SESSIOND_PATH env variable */
376 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
379 /* Let's rock and roll */
380 if (pathname
== NULL
) {
381 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/lttng-sessiond");
383 perror("asprintf spawn sessiond");
386 pathname
= alloc_pathname
;
389 ret
= spawn_sessiond(pathname
);
390 free(alloc_pathname
);
392 ERR("Problem occurred when starting %s", pathname
);
402 * Check args for specific options that *must* not trigger a session daemon
405 * Return 1 if match else 0.
407 static int check_args_no_sessiond(int argc
, char **argv
)
411 for (i
= 0; i
< argc
; i
++) {
412 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
413 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
414 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
415 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 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
, "+hnvqg:", long_options
, NULL
)) != -1) {
449 lttng_set_tracing_group(optarg
);
454 case OPT_SESSION_PATH
:
455 opt_sessiond_path
= strdup(optarg
);
457 case OPT_DUMP_OPTIONS
:
458 list_options(stdout
);
461 case OPT_DUMP_COMMANDS
:
462 list_commands(stdout
);
471 /* If both options are specified, quiet wins */
472 if (opt_verbose
&& opt_quiet
) {
476 /* Spawn session daemon if needed */
477 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
478 (check_sessiond() < 0)) {
482 /* No leftovers, print usage and quit */
483 if ((argc
- optind
) == 0) {
489 * Handle leftovers which is a first level command with the trailing
492 ret
= handle_command(argc
- optind
, argv
+ optind
);
497 ERR("%s", lttng_strerror(ret
));
513 int main(int argc
, char *argv
[])
517 progname
= argv
[0] ? argv
[0] : "lttng";
519 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
520 if (strncmp(progname
, "drtrace", 7) == 0 ||
521 strncmp("compudj", getenv("USER"), 7) == 0) {
522 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
525 ret
= set_signal_handler();
530 ret
= parse_args(argc
, argv
);
532 clean_exit(EXIT_FAILURE
);