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 { NULL
, NULL
} /* Array closure */
77 static void usage(FILE *ofp
)
79 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
80 fprintf(ofp
, "usage: lttng [options] <command>\n");
82 fprintf(ofp
, "Options:\n");
83 fprintf(ofp
, " -h, --help Show this help\n");
84 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
85 fprintf(ofp
, " -v, --verbose Verbose mode\n");
86 fprintf(ofp
, " -q, --quiet Quiet mode\n");
87 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
88 fprintf(ofp
, " --sessiond-path Session daemon full path\n");
89 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
90 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
92 fprintf(ofp
, "Commands:\n");
93 fprintf(ofp
, " add-context Add context to event or/and channel\n");
94 fprintf(ofp
, " create Create tracing session\n");
95 fprintf(ofp
, " destroy Teardown tracing session\n");
96 fprintf(ofp
, " enable-channel Enable tracing channel\n");
97 fprintf(ofp
, " enable-event Enable tracing event\n");
98 fprintf(ofp
, " disable-channel Disable tracing channel\n");
99 fprintf(ofp
, " disable-event Disable tracing event\n");
100 fprintf(ofp
, " list List possible tracing options\n");
101 fprintf(ofp
, " set-session Set current session name\n");
102 fprintf(ofp
, " start Start tracing\n");
103 fprintf(ofp
, " stop Stop tracing\n");
104 fprintf(ofp
, " version Show version information\n");
106 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
107 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
113 * List options line by line. This is mostly for bash auto completion and to
114 * avoid difficult parsing.
116 static void list_options(FILE *ofp
)
119 struct option
*option
= NULL
;
121 option
= &long_options
[i
];
122 while (option
->name
!= NULL
) {
123 fprintf(ofp
, "--%s\n", option
->name
);
125 if (isprint(option
->val
)) {
126 fprintf(ofp
, "-%c\n", option
->val
);
130 option
= &long_options
[i
];
137 * List commands line by line. This is mostly for bash auto completion and to
138 * avoid difficult parsing.
140 static void list_commands(FILE *ofp
)
143 struct cmd_struct
*cmd
= NULL
;
146 while (cmd
->name
!= NULL
) {
147 fprintf(ofp
, "%s\n", cmd
->name
);
156 static void clean_exit(int code
)
165 * Signal handler for the daemon
167 static void sighandler(int sig
)
171 DBG("SIGTERM catched");
172 clean_exit(EXIT_FAILURE
);
176 DBG("SIGCHLD catched");
179 DBG("Unknown signal %d catched", sig
);
189 * Setup signal handler for SIGCHLD and SIGTERM.
191 static int set_signal_handler(void)
197 if ((ret
= sigemptyset(&sigset
)) < 0) {
198 perror("sigemptyset");
202 sa
.sa_handler
= sighandler
;
205 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
210 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
222 * Handle the full argv list of a first level command. Will find the command
223 * in the global commands array and call the function callback associated.
225 * If command not found, return -1
226 * else, return function command error code.
228 static int handle_command(int argc
, char **argv
)
231 struct cmd_struct
*cmd
;
239 while (cmd
->func
!= NULL
) {
241 if (strcmp(argv
[0], cmd
->name
) == 0) {
242 ret
= cmd
->func(argc
, (const char**) argv
);
245 ERR("Command error");
247 case CMD_NOT_IMPLEMENTED
:
248 ERR("Options not implemented");
251 ERR("Undefined command");
263 /* Command not found */
273 * Spawn a session daemon by forking and execv.
275 static int spawn_sessiond(char *pathname
)
280 MSG("Spawning a session daemon");
284 * Spawn session daemon and tell
285 * it to signal us when ready.
287 execlp(pathname
, "ltt-sessiond", "--sig-parent", "--quiet", NULL
);
288 /* execlp only returns if error happened */
289 if (errno
== ENOENT
) {
290 ERR("No session daemon found. Use --sessiond-path.");
294 kill(getppid(), SIGTERM
); /* unpause parent */
296 } else if (pid
> 0) {
297 /* Wait for ltt-sessiond to start */
313 * Check if the session daemon is available using
314 * the liblttngctl API for the check. If not, try to
317 static int check_sessiond(void)
320 char *pathname
= NULL
, *alloc_pathname
= NULL
;
322 ret
= lttng_session_daemon_alive();
323 if (ret
== 0) { /* not alive */
324 /* Try command line option path */
325 if (opt_sessiond_path
!= NULL
) {
326 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
328 ERR("No such file: %s", opt_sessiond_path
);
331 pathname
= opt_sessiond_path
;
333 /* Try LTTNG_SESSIOND_PATH env variable */
334 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
337 /* Let's rock and roll */
338 if (pathname
== NULL
) {
339 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/ltt-sessiond");
341 perror("asprintf spawn sessiond");
344 pathname
= alloc_pathname
;
347 ret
= spawn_sessiond(pathname
);
348 free(alloc_pathname
);
350 ERR("Problem occurs when starting %s", pathname
);
362 * Parse command line arguments.
363 * Return 0 if OK, else -1
365 static int parse_args(int argc
, char **argv
)
371 clean_exit(EXIT_FAILURE
);
374 while ((opt
= getopt_long(argc
, argv
, "+hnvqg:", long_options
, NULL
)) != -1) {
386 lttng_set_tracing_group(optarg
);
391 case OPT_SESSION_PATH
:
392 opt_sessiond_path
= strdup(optarg
);
394 case OPT_DUMP_OPTIONS
:
395 list_options(stdout
);
398 case OPT_DUMP_COMMANDS
:
399 list_commands(stdout
);
408 /* If both options are specified, quiet wins */
409 if (opt_verbose
&& opt_quiet
) {
413 /* Spawn session daemon if needed */
414 if (opt_no_sessiond
== 0 && (check_sessiond() < 0)) {
418 /* No leftovers, print usage and quit */
419 if ((argc
- optind
) == 0) {
425 * Handle leftovers which is a first level command with the trailing
428 ret
= handle_command(argc
- optind
, argv
+ optind
);
433 ERR("%s", lttng_get_readable_code(ret
));
448 int main(int argc
, char *argv
[])
452 progname
= argv
[0] ? argv
[0] : "lttng";
454 /* For Mathieu Desnoyers aka Dr Tracing */
455 if (strncmp(progname
, "drtrace", 7) == 0) {
456 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
459 ret
= set_signal_handler();
464 ret
= parse_args(argc
, argv
);
466 clean_exit(EXIT_FAILURE
);
This page took 0.039799 seconds and 5 git commands to generate.