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 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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.
27 #include <lttng/lttng.h>
34 static char *progname
;
38 static int opt_no_sessiond
;
39 static char *opt_sessiond_path
;
46 /* Getopt options. No first level command. */
47 static struct option long_options
[] = {
48 {"help", 0, NULL
, 'h'},
49 {"group", 1, NULL
, 'g'},
50 {"verbose", 0, NULL
, 'v'},
51 {"quiet", 0, NULL
, 'q'},
52 {"no-sessiond", 0, NULL
, OPT_NO_SESSIOND
},
53 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
57 /* First level command */
58 static struct cmd_struct commands
[] = {
60 { "create", cmd_create
},
61 { "destroy", cmd_destroy
},
62 { "add-channel", cmd_add_channel
},
63 { "start", cmd_start
},
65 { "enable-event", cmd_enable_events
},
66 { "disable-event", cmd_disable_events
},
67 { "enable-channel", cmd_enable_channels
},
68 { "disable-channel", cmd_disable_channels
},
69 { "add-context", cmd_add_context
},
70 { "set-session", cmd_set_session
},
71 { NULL
, NULL
} /* Array closure */
74 static void usage(FILE *ofp
)
76 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
77 fprintf(ofp
, "usage: lttng [options] <command>\n");
79 fprintf(ofp
, "Options:\n");
80 fprintf(ofp
, " -h, --help Show this help\n");
81 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
82 fprintf(ofp
, " -v, --verbose Verbose mode\n");
83 fprintf(ofp
, " -q, --quiet Quiet mode\n");
84 fprintf(ofp
, " --no-sessiond Don't spawn a session daemon\n");
85 fprintf(ofp
, " --sessiond-path Session daemon full path\n");
87 fprintf(ofp
, "Commands:\n");
88 fprintf(ofp
, " add-channel Add channel to tracer\n");
89 fprintf(ofp
, " add-context Add context to event or/and channel\n");
90 fprintf(ofp
, " create Create tracing session\n");
91 fprintf(ofp
, " destroy Teardown tracing session\n");
92 fprintf(ofp
, " enable-channel Enable tracing channel\n");
93 fprintf(ofp
, " enable-event Enable tracing event\n");
94 fprintf(ofp
, " disable-channel Disable tracing channel\n");
95 fprintf(ofp
, " disable-event Disable tracing event\n");
96 fprintf(ofp
, " list List possible tracing options\n");
97 fprintf(ofp
, " set-session Set current session name\n");
98 fprintf(ofp
, " start Start tracing\n");
99 fprintf(ofp
, " stop Stop tracing\n");
100 fprintf(ofp
, " version Show version information\n");
102 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
103 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
109 static void clean_exit(int code
)
118 * Signal handler for the daemon
120 static void sighandler(int sig
)
124 DBG("SIGTERM catched");
125 clean_exit(EXIT_FAILURE
);
129 DBG("SIGCHLD catched");
132 DBG("Unknown signal %d catched", sig
);
142 * Setup signal handler for SIGCHLD and SIGTERM.
144 static int set_signal_handler(void)
150 if ((ret
= sigemptyset(&sigset
)) < 0) {
151 perror("sigemptyset");
155 sa
.sa_handler
= sighandler
;
158 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
163 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
175 * Handle the full argv list of a first level command. Will find the command
176 * in the global commands array and call the function callback associated.
178 * If command not found, return -1
179 * else, return function command error code.
181 static int handle_command(int argc
, char **argv
)
184 struct cmd_struct
*cmd
;
192 while (cmd
->func
!= NULL
) {
194 if (strcmp(argv
[0], cmd
->name
) == 0) {
195 ret
= cmd
->func(argc
, (const char**) argv
);
198 ERR("Command error");
200 case CMD_NOT_IMPLEMENTED
:
201 ERR("Options not implemented");
204 ERR("Undefined command");
216 /* Command not found */
226 * Spawn a session daemon by forking and execv.
228 static int spawn_sessiond(char *pathname
)
233 MSG("Spawning a session daemon");
237 * Spawn session daemon and tell
238 * it to signal us when ready.
240 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
241 /* execlp only returns if error happened */
242 if (errno
== ENOENT
) {
243 ERR("No session daemon found. Use --sessiond-path.");
247 kill(getppid(), SIGTERM
); /* unpause parent */
249 } else if (pid
> 0) {
250 /* Wait for ltt-sessiond to start */
266 * Check if the session daemon is available using
267 * the liblttngctl API for the check. If not, try to
270 static int check_sessiond(void)
273 char *pathname
= NULL
, *alloc_pathname
= NULL
;
275 ret
= lttng_session_daemon_alive();
276 if (ret
== 0) { /* not alive */
277 /* Try command line option path */
278 if (opt_sessiond_path
!= NULL
) {
279 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
281 ERR("No such file: %s", opt_sessiond_path
);
284 pathname
= opt_sessiond_path
;
286 /* Try LTTNG_SESSIOND_PATH env variable */
287 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
290 /* Let's rock and roll */
291 if (pathname
== NULL
) {
292 ret
= asprintf(&alloc_pathname
, "ltt-sessiond");
296 pathname
= alloc_pathname
;
299 ret
= spawn_sessiond(pathname
);
300 free(alloc_pathname
);
302 ERR("Problem occurs when starting %s", pathname
);
314 * Parse command line arguments.
315 * Return 0 if OK, else -1
317 static int parse_args(int argc
, char **argv
)
323 clean_exit(EXIT_FAILURE
);
326 while ((opt
= getopt_long(argc
, argv
, "+hvqg:", long_options
, NULL
)) != -1) {
338 lttng_set_tracing_group(optarg
);
340 case OPT_NO_SESSIOND
:
343 case OPT_SESSION_PATH
:
344 opt_sessiond_path
= strdup(optarg
);
352 /* If both options are specified, quiet wins */
353 if (opt_verbose
&& opt_quiet
) {
357 /* Spawn session daemon if needed */
358 if (opt_no_sessiond
== 0 && (check_sessiond() < 0)) {
362 /* No leftovers, print usage and quit */
363 if ((argc
- optind
) == 0) {
369 * Handle leftovers which is a first level command with the trailing
372 ret
= handle_command(argc
- optind
, argv
+ optind
);
378 ERR("%s", lttng_get_readable_code(ret
));
392 int main(int argc
, char *argv
[])
396 progname
= argv
[0] ? argv
[0] : "lttng";
398 /* For Mathieu Desnoyers aka Dr Tracing */
399 if (strncmp(progname
, "drtrace", 7) == 0) {
400 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
403 ret
= set_signal_handler();
408 ret
= parse_args(argc
, argv
);
This page took 0.148459 seconds and 5 git commands to generate.