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.
27 #include <lttng/lttng.h>
34 static char *progname
;
38 static int opt_no_sessiond
;
39 static char *opt_sessiond_path
;
47 /* Getopt options. No first level command. */
48 static struct option long_options
[] = {
49 {"help", 0, NULL
, 'h'},
50 {"group", 1, NULL
, 'g'},
51 {"verbose", 0, NULL
, 'v'},
52 {"quiet", 0, NULL
, 'q'},
53 {"no-sessiond", 0, NULL
, 'n'},
54 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
55 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
56 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
60 /* First level command */
61 static struct cmd_struct commands
[] = {
63 { "create", cmd_create
},
64 { "destroy", cmd_destroy
},
65 { "start", cmd_start
},
67 { "enable-event", cmd_enable_events
},
68 { "disable-event", cmd_disable_events
},
69 { "enable-channel", cmd_enable_channels
},
70 { "disable-channel", cmd_disable_channels
},
71 { "add-context", cmd_add_context
},
72 { "set-session", cmd_set_session
},
73 { "version", cmd_version
},
74 { "calibrate", cmd_calibrate
},
75 { NULL
, NULL
} /* Array closure */
78 static void usage(FILE *ofp
)
80 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
81 fprintf(ofp
, "usage: lttng [options] <command>\n");
83 fprintf(ofp
, "Options:\n");
84 fprintf(ofp
, " -h, --help Show this help\n");
85 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
86 fprintf(ofp
, " -v, --verbose Verbose mode\n");
87 fprintf(ofp
, " -q, --quiet Quiet mode\n");
88 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
89 fprintf(ofp
, " --sessiond-path Session daemon full path\n");
90 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
91 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
93 fprintf(ofp
, "Commands:\n");
94 fprintf(ofp
, " add-context Add context to event or/and channel\n");
95 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
96 fprintf(ofp
, " create Create tracing session\n");
97 fprintf(ofp
, " destroy Teardown tracing session\n");
98 fprintf(ofp
, " enable-channel Enable tracing channel\n");
99 fprintf(ofp
, " enable-event Enable tracing event\n");
100 fprintf(ofp
, " disable-channel Disable tracing channel\n");
101 fprintf(ofp
, " disable-event Disable tracing event\n");
102 fprintf(ofp
, " list List possible tracing options\n");
103 fprintf(ofp
, " set-session Set current session name\n");
104 fprintf(ofp
, " start Start tracing\n");
105 fprintf(ofp
, " stop Stop tracing\n");
106 fprintf(ofp
, " version Show version information\n");
108 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
109 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
115 * List options line by line. This is mostly for bash auto completion and to
116 * avoid difficult parsing.
118 static void list_options(FILE *ofp
)
121 struct option
*option
= NULL
;
123 option
= &long_options
[i
];
124 while (option
->name
!= NULL
) {
125 fprintf(ofp
, "--%s\n", option
->name
);
127 if (isprint(option
->val
)) {
128 fprintf(ofp
, "-%c\n", option
->val
);
132 option
= &long_options
[i
];
139 * List commands line by line. This is mostly for bash auto completion and to
140 * avoid difficult parsing.
142 static void list_commands(FILE *ofp
)
145 struct cmd_struct
*cmd
= NULL
;
148 while (cmd
->name
!= NULL
) {
149 fprintf(ofp
, "%s\n", cmd
->name
);
158 static void clean_exit(int code
)
167 * Signal handler for the daemon
169 static void sighandler(int sig
)
173 DBG("SIGTERM catched");
174 clean_exit(EXIT_FAILURE
);
178 DBG("SIGCHLD catched");
181 DBG("Unknown signal %d catched", sig
);
191 * Setup signal handler for SIGCHLD and SIGTERM.
193 static int set_signal_handler(void)
199 if ((ret
= sigemptyset(&sigset
)) < 0) {
200 perror("sigemptyset");
204 sa
.sa_handler
= sighandler
;
207 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
212 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
224 * Handle the full argv list of a first level command. Will find the command
225 * in the global commands array and call the function callback associated.
227 * If command not found, return -1
228 * else, return function command error code.
230 static int handle_command(int argc
, char **argv
)
233 struct cmd_struct
*cmd
;
241 while (cmd
->func
!= NULL
) {
243 if (strcmp(argv
[0], cmd
->name
) == 0) {
244 ret
= cmd
->func(argc
, (const char**) argv
);
247 ERR("Command error");
249 case CMD_NOT_IMPLEMENTED
:
250 ERR("Options not implemented");
253 ERR("Undefined command");
265 /* Command not found */
275 * Spawn a session daemon by forking and execv.
277 static int spawn_sessiond(char *pathname
)
282 MSG("Spawning a session daemon");
286 * Spawn session daemon and tell
287 * it to signal us when ready.
289 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
290 /* execlp only returns if error happened */
291 if (errno
== ENOENT
) {
292 ERR("No session daemon found. Use --sessiond-path.");
296 kill(getppid(), SIGTERM
); /* unpause parent */
298 } else if (pid
> 0) {
299 /* Wait for ltt-sessiond to start */
315 * Check if the session daemon is available using
316 * the liblttngctl API for the check. If not, try to
319 static int check_sessiond(void)
322 char *pathname
= NULL
, *alloc_pathname
= NULL
;
324 ret
= lttng_session_daemon_alive();
325 if (ret
== 0) { /* not alive */
326 /* Try command line option path */
327 if (opt_sessiond_path
!= NULL
) {
328 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
330 ERR("No such file: %s", opt_sessiond_path
);
333 pathname
= opt_sessiond_path
;
335 /* Try LTTNG_SESSIOND_PATH env variable */
336 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
339 /* Let's rock and roll */
340 if (pathname
== NULL
) {
341 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/ltt-sessiond");
343 perror("asprintf spawn sessiond");
346 pathname
= alloc_pathname
;
349 ret
= spawn_sessiond(pathname
);
350 free(alloc_pathname
);
352 ERR("Problem occurs when starting %s", pathname
);
364 * Parse command line arguments.
365 * Return 0 if OK, else -1
367 static int parse_args(int argc
, char **argv
)
373 clean_exit(EXIT_FAILURE
);
376 while ((opt
= getopt_long(argc
, argv
, "+hnvqg:", long_options
, NULL
)) != -1) {
388 lttng_set_tracing_group(optarg
);
393 case OPT_SESSION_PATH
:
394 opt_sessiond_path
= strdup(optarg
);
396 case OPT_DUMP_OPTIONS
:
397 list_options(stdout
);
400 case OPT_DUMP_COMMANDS
:
401 list_commands(stdout
);
410 /* If both options are specified, quiet wins */
411 if (opt_verbose
&& opt_quiet
) {
415 /* Spawn session daemon if needed */
416 if (opt_no_sessiond
== 0 && (check_sessiond() < 0)) {
420 /* No leftovers, print usage and quit */
421 if ((argc
- optind
) == 0) {
427 * Handle leftovers which is a first level command with the trailing
430 ret
= handle_command(argc
- optind
, argv
+ optind
);
435 ERR("%s", lttng_get_readable_code(ret
));
450 int main(int argc
, char *argv
[])
454 progname
= argv
[0] ? argv
[0] : "lttng";
456 /* For Mathieu Desnoyers aka Dr Tracing */
457 if (strncmp(progname
, "drtrace", 7) == 0) {
458 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
461 ret
= set_signal_handler();
466 ret
= parse_args(argc
, argv
);
468 clean_exit(EXIT_FAILURE
);
This page took 0.06114 seconds and 4 git commands to generate.