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>
29 #include <lttng/lttng.h>
30 #include <common/error.h>
31 #include <common/compat/getenv.h>
32 #include <common/utils.h>
36 static const char *help_msg
=
37 #ifdef LTTNG_EMBED_HELP
45 static char *progname
;
47 char *opt_sessiond_path
;
49 char *opt_relayd_path
;
58 /* Getopt options. No first level command. */
59 static struct option long_options
[] = {
60 {"version", 0, NULL
, 'V'},
61 {"help", 0, NULL
, 'h'},
62 {"group", 1, NULL
, 'g'},
63 {"verbose", 0, NULL
, 'v'},
64 {"quiet", 0, NULL
, 'q'},
66 {"no-sessiond", 0, NULL
, 'n'},
67 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
68 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
69 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
70 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
74 /* First level command */
75 static struct cmd_struct commands
[] = {
76 { "add-context", cmd_add_context
},
77 { "create", cmd_create
},
78 { "destroy", cmd_destroy
},
79 { "disable-channel", cmd_disable_channels
},
80 { "disable-event", cmd_disable_events
},
81 { "enable-channel", cmd_enable_channels
},
82 { "enable-event", cmd_enable_events
},
86 { "metadata", cmd_metadata
},
88 { "set-session", cmd_set_session
},
89 { "snapshot", cmd_snapshot
},
90 { "start", cmd_start
},
91 { "status", cmd_status
},
93 { "track", cmd_track
},
94 { "untrack", cmd_untrack
},
96 { "version", cmd_version
},
98 { "regenerate", cmd_regenerate
},
99 { NULL
, NULL
} /* Array closure */
102 static void version(FILE *ofp
)
104 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
106 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
110 * Find the MI output type enum from a string. This function is for the support
111 * of machine interface output.
113 static int mi_output_type(const char *output_type
)
117 if (!strncasecmp("xml", output_type
, 3)) {
120 /* Invalid output format */
121 ERR("MI output format not supported");
122 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
131 * List options line by line. This is mostly for bash auto completion and to
132 * avoid difficult parsing.
134 static void list_options(FILE *ofp
)
137 struct option
*option
= NULL
;
139 option
= &long_options
[i
];
140 while (option
->name
!= NULL
) {
141 fprintf(ofp
, "--%s\n", option
->name
);
143 if (isprint(option
->val
)) {
144 fprintf(ofp
, "-%c\n", option
->val
);
148 option
= &long_options
[i
];
155 static void clean_exit(int code
)
164 * Signal handler for the daemon
166 static void sighandler(int sig
)
170 DBG("SIGTERM caught");
171 clean_exit(EXIT_FAILURE
);
174 DBG("Unknown signal %d caught", sig
);
184 * Setup signal handler for SIGCHLD and SIGTERM.
186 static int set_signal_handler(void)
192 if ((ret
= sigemptyset(&sigset
)) < 0) {
193 PERROR("sigemptyset");
197 sa
.sa_handler
= sighandler
;
201 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
213 * Handle the full argv list of a first level command. Will find the command
214 * in the global commands array and call the function callback associated.
216 * If command not found, return -1
217 * else, return function command error code.
219 static int handle_command(int argc
, char **argv
)
222 struct cmd_struct
*cmd
;
229 /* Special case for help command which needs the commands array */
230 if (strcmp(argv
[0], "help") == 0) {
231 ret
= cmd_help(argc
, (const char**) argv
, commands
);
236 while (cmd
->name
!= NULL
) {
238 if (strcmp(argv
[0], cmd
->name
) == 0) {
239 ret
= cmd
->func(argc
, (const char**) argv
);
246 /* Command not found */
253 static void show_basic_help(void)
255 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
256 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
258 puts("Available commands:");
260 puts("Tracing sessions:");
261 puts(" create " CONFIG_CMD_DESCR_CREATE
);
262 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
263 puts(" load " CONFIG_CMD_DESCR_LOAD
);
264 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
265 puts(" save " CONFIG_CMD_DESCR_SAVE
);
266 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
269 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
270 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
271 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
273 puts("Event rules:");
274 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
275 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
278 puts(" list " CONFIG_CMD_DESCR_LIST
);
279 puts(" status " CONFIG_CMD_DESCR_STATUS
);
282 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
283 puts(" start " CONFIG_CMD_DESCR_START
);
284 puts(" stop " CONFIG_CMD_DESCR_STOP
);
286 puts("Resource tracking:");
287 puts(" track " CONFIG_CMD_DESCR_TRACK
);
288 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
290 puts("Miscellaneous:");
291 puts(" help " CONFIG_CMD_DESCR_HELP
);
292 puts(" version " CONFIG_CMD_DESCR_VERSION
);
293 puts(" view " CONFIG_CMD_DESCR_VIEW
);
295 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
296 puts("command COMMAND.");
298 puts("See `man lttng` for more help with the lttng command.");
302 * Parse command line arguments.
304 * Return 0 if OK, else -1
306 static int parse_args(int argc
, char **argv
)
310 if (lttng_is_setuid_setgid()) {
311 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
312 clean_exit(EXIT_FAILURE
);
317 clean_exit(EXIT_FAILURE
);
320 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
327 ret
= utils_show_help(1, "lttng", help_msg
);
329 ERR("Cannot show --help for `lttng`");
334 /* There is only 3 possible level of verbosity. (-vvv) */
335 if (lttng_opt_verbose
< 3) {
336 lttng_opt_verbose
+= 1;
343 lttng_opt_mi
= mi_output_type(optarg
);
344 if (lttng_opt_mi
< 0) {
350 lttng_set_tracing_group(optarg
);
355 case OPT_SESSION_PATH
:
356 free(opt_sessiond_path
);
357 opt_sessiond_path
= strdup(optarg
);
358 if (!opt_sessiond_path
) {
363 case OPT_RELAYD_PATH
:
364 free(opt_relayd_path
);
365 opt_relayd_path
= strdup(optarg
);
366 if (!opt_relayd_path
) {
371 case OPT_DUMP_OPTIONS
:
372 list_options(stdout
);
375 case OPT_DUMP_COMMANDS
:
376 list_commands(commands
, stdout
);
385 /* If both options are specified, quiet wins */
386 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
387 lttng_opt_verbose
= 0;
390 /* No leftovers, quit */
391 if ((argc
- optind
) == 0) {
397 * Handle leftovers which is a first level command with the trailing
400 ret
= handle_command(argc
- optind
, argv
+ optind
);
403 WARN("Some command(s) went wrong");
406 ERR("Command error");
409 ERR("Undefined command or invalid arguments");
414 case CMD_UNSUPPORTED
:
415 ERR("Unsupported command");
438 int main(int argc
, char *argv
[])
442 progname
= argv
[0] ? argv
[0] : "lttng";
444 ret
= set_signal_handler();
449 ret
= parse_args(argc
, argv
);