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
},
89 { "set-session", cmd_set_session
},
90 { "snapshot", cmd_snapshot
},
91 { "start", cmd_start
},
92 { "status", cmd_status
},
94 { "track", cmd_track
},
95 { "untrack", cmd_untrack
},
96 { "version", cmd_version
},
98 { NULL
, NULL
} /* Array closure */
101 static void version(FILE *ofp
)
103 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
105 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
109 * Find the MI output type enum from a string. This function is for the support
110 * of machine interface output.
112 static int mi_output_type(const char *output_type
)
116 if (!strncasecmp("xml", output_type
, 3)) {
119 /* Invalid output format */
120 ERR("MI output format not supported");
121 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
130 * List options line by line. This is mostly for bash auto completion and to
131 * avoid difficult parsing.
133 static void list_options(FILE *ofp
)
136 struct option
*option
= NULL
;
138 option
= &long_options
[i
];
139 while (option
->name
!= NULL
) {
140 fprintf(ofp
, "--%s\n", option
->name
);
142 if (isprint(option
->val
)) {
143 fprintf(ofp
, "-%c\n", option
->val
);
147 option
= &long_options
[i
];
154 static void clean_exit(int code
)
163 * Signal handler for the daemon
165 static void sighandler(int sig
)
169 DBG("SIGTERM caught");
170 clean_exit(EXIT_FAILURE
);
173 DBG("Unknown signal %d caught", sig
);
183 * Setup signal handler for SIGCHLD and SIGTERM.
185 static int set_signal_handler(void)
191 if ((ret
= sigemptyset(&sigset
)) < 0) {
192 PERROR("sigemptyset");
196 sa
.sa_handler
= sighandler
;
200 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
212 * Handle the full argv list of a first level command. Will find the command
213 * in the global commands array and call the function callback associated.
215 * If command not found, return -1
216 * else, return function command error code.
218 static int handle_command(int argc
, char **argv
)
221 struct cmd_struct
*cmd
;
228 /* Special case for help command which needs the commands array */
229 if (strcmp(argv
[0], "help") == 0) {
230 ret
= cmd_help(argc
, (const char**) argv
, commands
);
235 while (cmd
->name
!= NULL
) {
237 if (strcmp(argv
[0], cmd
->name
) == 0) {
238 ret
= cmd
->func(argc
, (const char**) argv
);
245 /* Command not found */
252 static void show_basic_help(void)
254 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
255 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
257 puts("Available commands:");
259 puts("Tracing sessions:");
260 puts(" create " CONFIG_CMD_DESCR_CREATE
);
261 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
262 puts(" load " CONFIG_CMD_DESCR_LOAD
);
263 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
264 puts(" save " CONFIG_CMD_DESCR_SAVE
);
265 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
268 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
269 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
270 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
272 puts("Event rules:");
273 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
274 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
277 puts(" list " CONFIG_CMD_DESCR_LIST
);
278 puts(" status " CONFIG_CMD_DESCR_STATUS
);
281 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
282 puts(" start " CONFIG_CMD_DESCR_START
);
283 puts(" stop " CONFIG_CMD_DESCR_STOP
);
285 puts("Resource tracking:");
286 puts(" track " CONFIG_CMD_DESCR_TRACK
);
287 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
289 puts("Miscellaneous:");
290 puts(" help " CONFIG_CMD_DESCR_HELP
);
291 puts(" version " CONFIG_CMD_DESCR_VERSION
);
292 puts(" view " CONFIG_CMD_DESCR_VIEW
);
294 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
295 puts("command COMMAND.");
297 puts("See `man lttng` for more help with the lttng command.");
301 * Parse command line arguments.
303 * Return 0 if OK, else -1
305 static int parse_args(int argc
, char **argv
)
309 if (lttng_is_setuid_setgid()) {
310 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
311 clean_exit(EXIT_FAILURE
);
316 clean_exit(EXIT_FAILURE
);
319 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
326 ret
= utils_show_help(1, "lttng", help_msg
);
328 ERR("Cannot show --help for `lttng`");
333 /* There is only 3 possible level of verbosity. (-vvv) */
334 if (lttng_opt_verbose
< 3) {
335 lttng_opt_verbose
+= 1;
342 lttng_opt_mi
= mi_output_type(optarg
);
343 if (lttng_opt_mi
< 0) {
349 lttng_set_tracing_group(optarg
);
354 case OPT_SESSION_PATH
:
355 free(opt_sessiond_path
);
356 opt_sessiond_path
= strdup(optarg
);
357 if (!opt_sessiond_path
) {
362 case OPT_RELAYD_PATH
:
363 free(opt_relayd_path
);
364 opt_relayd_path
= strdup(optarg
);
365 if (!opt_relayd_path
) {
370 case OPT_DUMP_OPTIONS
:
371 list_options(stdout
);
374 case OPT_DUMP_COMMANDS
:
375 list_commands(commands
, stdout
);
384 /* If both options are specified, quiet wins */
385 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
386 lttng_opt_verbose
= 0;
389 /* No leftovers, quit */
390 if ((argc
- optind
) == 0) {
396 * Handle leftovers which is a first level command with the trailing
399 ret
= handle_command(argc
- optind
, argv
+ optind
);
402 WARN("Some command(s) went wrong");
405 ERR("Command error");
408 ERR("Undefined command or invalid arguments");
413 case CMD_UNSUPPORTED
:
414 ERR("Unsupported command");
437 int main(int argc
, char *argv
[])
441 progname
= argv
[0] ? argv
[0] : "lttng";
443 ret
= set_signal_handler();
448 ret
= parse_args(argc
, argv
);