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>
37 static const char *help_msg
=
38 #ifdef LTTNG_EMBED_HELP
46 static char *progname
;
48 char *opt_sessiond_path
;
50 char *opt_relayd_path
;
59 /* Getopt options. No first level command. */
60 static struct option long_options
[] = {
61 {"version", 0, NULL
, 'V'},
62 {"help", 0, NULL
, 'h'},
63 {"group", 1, NULL
, 'g'},
64 {"verbose", 0, NULL
, 'v'},
65 {"quiet", 0, NULL
, 'q'},
67 {"no-sessiond", 0, NULL
, 'n'},
68 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
69 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
70 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
71 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
75 /* First level command */
76 static struct cmd_struct commands
[] = {
77 { "add-context", cmd_add_context
},
78 { "create", cmd_create
},
79 { "destroy", cmd_destroy
},
80 { "disable-channel", cmd_disable_channels
},
81 { "disable-event", cmd_disable_events
},
82 { "enable-channel", cmd_enable_channels
},
83 { "enable-event", cmd_enable_events
},
87 { "metadata", cmd_metadata
},
88 { "regenerate", cmd_regenerate
},
89 { "rotate", cmd_rotate
},
90 { "enable-rotation", cmd_enable_rotation
},
91 { "disable-rotation", cmd_disable_rotation
},
93 { "set-session", cmd_set_session
},
94 { "snapshot", cmd_snapshot
},
95 { "start", cmd_start
},
96 { "status", cmd_status
},
98 { "track", cmd_track
},
99 { "untrack", cmd_untrack
},
100 { "version", cmd_version
},
102 { NULL
, NULL
} /* Array closure */
105 static void version(FILE *ofp
)
107 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
109 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
113 * Find the MI output type enum from a string. This function is for the support
114 * of machine interface output.
116 static int mi_output_type(const char *output_type
)
120 if (!strncasecmp("xml", output_type
, 3)) {
123 /* Invalid output format */
124 ERR("MI output format not supported");
125 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
134 * List options line by line. This is mostly for bash auto completion and to
135 * avoid difficult parsing.
137 static void list_options(FILE *ofp
)
140 struct option
*option
= NULL
;
142 option
= &long_options
[i
];
143 while (option
->name
!= NULL
) {
144 fprintf(ofp
, "--%s\n", option
->name
);
146 if (isprint(option
->val
)) {
147 fprintf(ofp
, "-%c\n", option
->val
);
151 option
= &long_options
[i
];
158 static void clean_exit(int code
)
167 * Signal handler for the daemon
169 static void sighandler(int sig
)
173 DBG("SIGTERM caught");
174 clean_exit(EXIT_FAILURE
);
177 DBG("Unknown signal %d caught", sig
);
187 * Setup signal handler for SIGCHLD and SIGTERM.
189 static int set_signal_handler(void)
195 if ((ret
= sigemptyset(&sigset
)) < 0) {
196 PERROR("sigemptyset");
200 sa
.sa_handler
= sighandler
;
204 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
216 * Handle the full argv list of a first level command. Will find the command
217 * in the global commands array and call the function callback associated.
219 * If command not found, return -1
220 * else, return function command error code.
222 static int handle_command(int argc
, char **argv
)
225 struct cmd_struct
*cmd
;
232 /* Special case for help command which needs the commands array */
233 if (strcmp(argv
[0], "help") == 0) {
234 ret
= cmd_help(argc
, (const char**) argv
, commands
);
239 while (cmd
->name
!= NULL
) {
241 if (strcmp(argv
[0], cmd
->name
) == 0) {
242 ret
= cmd
->func(argc
, (const char**) argv
);
249 /* Command not found */
256 static void show_basic_help(void)
258 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
259 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
261 puts("Available commands:");
263 puts("Tracing sessions:");
264 puts(" create " CONFIG_CMD_DESCR_CREATE
);
265 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
266 puts(" load " CONFIG_CMD_DESCR_LOAD
);
267 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
268 puts(" save " CONFIG_CMD_DESCR_SAVE
);
269 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
272 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
273 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
274 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
276 puts("Event rules:");
277 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
278 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
281 puts(" list " CONFIG_CMD_DESCR_LIST
);
282 puts(" status " CONFIG_CMD_DESCR_STATUS
);
285 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
286 puts(" start " CONFIG_CMD_DESCR_START
);
287 puts(" stop " CONFIG_CMD_DESCR_STOP
);
289 puts("Resource tracking:");
290 puts(" track " CONFIG_CMD_DESCR_TRACK
);
291 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
293 puts("Miscellaneous:");
294 puts(" help " CONFIG_CMD_DESCR_HELP
);
295 puts(" version " CONFIG_CMD_DESCR_VERSION
);
296 puts(" view " CONFIG_CMD_DESCR_VIEW
);
298 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
299 puts("command COMMAND.");
301 puts("See `man lttng` for more help with the lttng command.");
305 * Parse command line arguments.
307 * Return 0 if OK, else -1
309 static int parse_args(int argc
, char **argv
)
313 if (lttng_is_setuid_setgid()) {
314 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
315 clean_exit(EXIT_FAILURE
);
320 clean_exit(EXIT_FAILURE
);
323 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
330 ret
= utils_show_help(1, "lttng", help_msg
);
332 ERR("Cannot show --help for `lttng`");
337 /* There is only 3 possible level of verbosity. (-vvv) */
338 if (lttng_opt_verbose
< 3) {
339 lttng_opt_verbose
+= 1;
346 lttng_opt_mi
= mi_output_type(optarg
);
347 if (lttng_opt_mi
< 0) {
353 lttng_set_tracing_group(optarg
);
358 case OPT_SESSION_PATH
:
359 free(opt_sessiond_path
);
360 opt_sessiond_path
= strdup(optarg
);
361 if (!opt_sessiond_path
) {
366 case OPT_RELAYD_PATH
:
367 free(opt_relayd_path
);
368 opt_relayd_path
= strdup(optarg
);
369 if (!opt_relayd_path
) {
374 case OPT_DUMP_OPTIONS
:
375 list_options(stdout
);
378 case OPT_DUMP_COMMANDS
:
379 list_commands(commands
, stdout
);
388 /* If both options are specified, quiet wins */
389 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
390 lttng_opt_verbose
= 0;
393 /* No leftovers, quit */
394 if ((argc
- optind
) == 0) {
400 * Handle leftovers which is a first level command with the trailing
403 ret
= handle_command(argc
- optind
, argv
+ optind
);
406 WARN("Some command(s) went wrong");
409 ERR("Command error");
412 ERR("Undefined command or invalid arguments");
417 case CMD_UNSUPPORTED
:
418 ERR("Unsupported command");
441 int main(int argc
, char *argv
[])
445 progname
= argv
[0] ? argv
[0] : "lttng";
447 ret
= set_signal_handler();
452 ret
= parse_args(argc
, argv
);