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
},
87 { "regenerate", cmd_regenerate
},
88 { "rotate", cmd_rotate
},
89 { "enable-rotation", cmd_enable_rotation
},
90 { "disable-rotation", cmd_disable_rotation
},
92 { "set-session", cmd_set_session
},
93 { "snapshot", cmd_snapshot
},
94 { "start", cmd_start
},
95 { "status", cmd_status
},
97 { "track", cmd_track
},
98 { "untrack", cmd_untrack
},
99 { "version", cmd_version
},
101 { NULL
, NULL
} /* Array closure */
104 static void version(FILE *ofp
)
106 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
108 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
112 * Find the MI output type enum from a string. This function is for the support
113 * of machine interface output.
115 static int mi_output_type(const char *output_type
)
119 if (!strncasecmp("xml", output_type
, 3)) {
122 /* Invalid output format */
123 ERR("MI output format not supported");
124 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
133 * List options line by line. This is mostly for bash auto completion and to
134 * avoid difficult parsing.
136 static void list_options(FILE *ofp
)
139 struct option
*option
= NULL
;
141 option
= &long_options
[i
];
142 while (option
->name
!= NULL
) {
143 fprintf(ofp
, "--%s\n", option
->name
);
145 if (isprint(option
->val
)) {
146 fprintf(ofp
, "-%c\n", option
->val
);
150 option
= &long_options
[i
];
157 static void clean_exit(int code
)
166 * Signal handler for the daemon
168 static void sighandler(int sig
)
172 DBG("SIGTERM caught");
173 clean_exit(EXIT_FAILURE
);
176 DBG("Unknown signal %d caught", sig
);
186 * Setup signal handler for SIGCHLD and SIGTERM.
188 static int set_signal_handler(void)
194 if ((ret
= sigemptyset(&sigset
)) < 0) {
195 PERROR("sigemptyset");
199 sa
.sa_handler
= sighandler
;
203 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
215 * Handle the full argv list of a first level command. Will find the command
216 * in the global commands array and call the function callback associated.
218 * If command not found, return -1
219 * else, return function command error code.
221 static int handle_command(int argc
, char **argv
)
224 struct cmd_struct
*cmd
;
231 /* Special case for help command which needs the commands array */
232 if (strcmp(argv
[0], "help") == 0) {
233 ret
= cmd_help(argc
, (const char**) argv
, commands
);
238 while (cmd
->name
!= NULL
) {
240 if (strcmp(argv
[0], cmd
->name
) == 0) {
241 ret
= cmd
->func(argc
, (const char**) argv
);
248 /* Command not found */
255 static void show_basic_help(void)
257 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
258 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
260 puts("Available commands:");
262 puts("Tracing sessions:");
263 puts(" create " CONFIG_CMD_DESCR_CREATE
);
264 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
265 puts(" load " CONFIG_CMD_DESCR_LOAD
);
266 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
267 puts(" save " CONFIG_CMD_DESCR_SAVE
);
268 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
271 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
272 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
273 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
275 puts("Event rules:");
276 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
277 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
280 puts(" list " CONFIG_CMD_DESCR_LIST
);
281 puts(" status " CONFIG_CMD_DESCR_STATUS
);
284 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
285 puts(" start " CONFIG_CMD_DESCR_START
);
286 puts(" stop " CONFIG_CMD_DESCR_STOP
);
288 puts("Resource tracking:");
289 puts(" track " CONFIG_CMD_DESCR_TRACK
);
290 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
292 puts("Miscellaneous:");
293 puts(" help " CONFIG_CMD_DESCR_HELP
);
294 puts(" version " CONFIG_CMD_DESCR_VERSION
);
295 puts(" view " CONFIG_CMD_DESCR_VIEW
);
297 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
298 puts("command COMMAND.");
300 puts("See `man lttng` for more help with the lttng command.");
304 * Parse command line arguments.
306 * Return 0 if OK, else -1
308 static int parse_args(int argc
, char **argv
)
312 if (lttng_is_setuid_setgid()) {
313 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
314 clean_exit(EXIT_FAILURE
);
319 clean_exit(EXIT_FAILURE
);
322 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
329 ret
= utils_show_help(1, "lttng", help_msg
);
331 ERR("Cannot show --help for `lttng`");
336 /* There is only 3 possible level of verbosity. (-vvv) */
337 if (lttng_opt_verbose
< 3) {
338 lttng_opt_verbose
+= 1;
345 lttng_opt_mi
= mi_output_type(optarg
);
346 if (lttng_opt_mi
< 0) {
352 lttng_set_tracing_group(optarg
);
357 case OPT_SESSION_PATH
:
358 free(opt_sessiond_path
);
359 opt_sessiond_path
= strdup(optarg
);
360 if (!opt_sessiond_path
) {
365 case OPT_RELAYD_PATH
:
366 free(opt_relayd_path
);
367 opt_relayd_path
= strdup(optarg
);
368 if (!opt_relayd_path
) {
373 case OPT_DUMP_OPTIONS
:
374 list_options(stdout
);
377 case OPT_DUMP_COMMANDS
:
378 list_commands(commands
, stdout
);
387 /* If both options are specified, quiet wins */
388 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
389 lttng_opt_verbose
= 0;
392 /* No leftovers, quit */
393 if ((argc
- optind
) == 0) {
399 * Handle leftovers which is a first level command with the trailing
402 ret
= handle_command(argc
- optind
, argv
+ optind
);
405 WARN("Some command(s) went wrong");
408 ERR("Command error");
411 ERR("Undefined command or invalid arguments");
416 case CMD_UNSUPPORTED
:
417 ERR("Unsupported command");
440 int main(int argc
, char *argv
[])
444 progname
= argv
[0] ? argv
[0] : "lttng";
446 ret
= set_signal_handler();
451 ret
= parse_args(argc
, argv
);