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 char *progname
;
39 char *opt_sessiond_path
;
41 char *opt_relayd_path
;
50 /* Getopt options. No first level command. */
51 static struct option long_options
[] = {
52 {"version", 0, NULL
, 'V'},
53 {"help", 0, NULL
, 'h'},
54 {"group", 1, NULL
, 'g'},
55 {"verbose", 0, NULL
, 'v'},
56 {"quiet", 0, NULL
, 'q'},
58 {"no-sessiond", 0, NULL
, 'n'},
59 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
60 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
61 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
62 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
66 /* First level command */
67 static struct cmd_struct commands
[] = {
68 { "add-context", cmd_add_context
},
69 { "create", cmd_create
},
70 { "destroy", cmd_destroy
},
71 { "disable-channel", cmd_disable_channels
},
72 { "disable-event", cmd_disable_events
},
73 { "enable-channel", cmd_enable_channels
},
74 { "enable-event", cmd_enable_events
},
78 { "metadata", cmd_metadata
},
80 { "set-session", cmd_set_session
},
81 { "snapshot", cmd_snapshot
},
82 { "start", cmd_start
},
83 { "status", cmd_status
},
85 { "track", cmd_track
},
86 { "untrack", cmd_untrack
},
88 { "version", cmd_version
},
90 { "regenerate", cmd_regenerate
},
91 { NULL
, NULL
} /* Array closure */
94 static void version(FILE *ofp
)
96 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
98 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
102 * Find the MI output type enum from a string. This function is for the support
103 * of machine interface output.
105 static int mi_output_type(const char *output_type
)
109 if (!strncasecmp("xml", output_type
, 3)) {
112 /* Invalid output format */
113 ERR("MI output format not supported");
114 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
123 * List options line by line. This is mostly for bash auto completion and to
124 * avoid difficult parsing.
126 static void list_options(FILE *ofp
)
129 struct option
*option
= NULL
;
131 option
= &long_options
[i
];
132 while (option
->name
!= NULL
) {
133 fprintf(ofp
, "--%s\n", option
->name
);
135 if (isprint(option
->val
)) {
136 fprintf(ofp
, "-%c\n", option
->val
);
140 option
= &long_options
[i
];
147 static void clean_exit(int code
)
156 * Signal handler for the daemon
158 static void sighandler(int sig
)
162 DBG("SIGTERM caught");
163 clean_exit(EXIT_FAILURE
);
166 DBG("Unknown signal %d caught", sig
);
176 * Setup signal handler for SIGCHLD and SIGTERM.
178 static int set_signal_handler(void)
184 if ((ret
= sigemptyset(&sigset
)) < 0) {
185 PERROR("sigemptyset");
189 sa
.sa_handler
= sighandler
;
193 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
205 * Handle the full argv list of a first level command. Will find the command
206 * in the global commands array and call the function callback associated.
208 * If command not found, return -1
209 * else, return function command error code.
211 static int handle_command(int argc
, char **argv
)
214 struct cmd_struct
*cmd
;
221 /* Special case for help command which needs the commands array */
222 if (strcmp(argv
[0], "help") == 0) {
223 ret
= cmd_help(argc
, (const char**) argv
, commands
);
228 while (cmd
->name
!= NULL
) {
230 if (strcmp(argv
[0], cmd
->name
) == 0) {
231 ret
= cmd
->func(argc
, (const char**) argv
);
238 /* Command not found */
245 static void show_basic_help(void)
247 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
248 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
250 puts("Available commands:");
252 puts("Tracing sessions:");
253 puts(" create " CONFIG_CMD_DESCR_CREATE
);
254 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
255 puts(" load " CONFIG_CMD_DESCR_LOAD
);
256 puts(" metadata " CONFIG_CMD_DESCR_METADATA
);
257 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
258 puts(" save " CONFIG_CMD_DESCR_SAVE
);
259 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
262 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
263 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
264 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
266 puts("Event rules:");
267 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
268 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
271 puts(" list " CONFIG_CMD_DESCR_LIST
);
272 puts(" status " CONFIG_CMD_DESCR_STATUS
);
275 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
276 puts(" start " CONFIG_CMD_DESCR_START
);
277 puts(" stop " CONFIG_CMD_DESCR_STOP
);
279 puts("Resource tracking:");
280 puts(" track " CONFIG_CMD_DESCR_TRACK
);
281 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
283 puts("Miscellaneous:");
284 puts(" help " CONFIG_CMD_DESCR_HELP
);
285 puts(" version " CONFIG_CMD_DESCR_VERSION
);
286 puts(" view " CONFIG_CMD_DESCR_VIEW
);
288 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
289 puts("command COMMAND.");
291 puts("See `man lttng` for more help with the lttng command.");
295 * Parse command line arguments.
297 * Return 0 if OK, else -1
299 static int parse_args(int argc
, char **argv
)
303 if (lttng_is_setuid_setgid()) {
304 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
305 clean_exit(EXIT_FAILURE
);
310 clean_exit(EXIT_FAILURE
);
313 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
320 ret
= utils_show_man_page(1, "lttng");
323 ERR("Cannot view man page lttng(1)");
328 /* There is only 3 possible level of verbosity. (-vvv) */
329 if (lttng_opt_verbose
< 3) {
330 lttng_opt_verbose
+= 1;
337 lttng_opt_mi
= mi_output_type(optarg
);
338 if (lttng_opt_mi
< 0) {
344 lttng_set_tracing_group(optarg
);
349 case OPT_SESSION_PATH
:
350 free(opt_sessiond_path
);
351 opt_sessiond_path
= strdup(optarg
);
352 if (!opt_sessiond_path
) {
357 case OPT_RELAYD_PATH
:
358 free(opt_relayd_path
);
359 opt_relayd_path
= strdup(optarg
);
360 if (!opt_relayd_path
) {
365 case OPT_DUMP_OPTIONS
:
366 list_options(stdout
);
369 case OPT_DUMP_COMMANDS
:
370 list_commands(commands
, stdout
);
379 /* If both options are specified, quiet wins */
380 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
381 lttng_opt_verbose
= 0;
384 /* No leftovers, quit */
385 if ((argc
- optind
) == 0) {
391 * Handle leftovers which is a first level command with the trailing
394 ret
= handle_command(argc
- optind
, argv
+ optind
);
397 WARN("Some command(s) went wrong");
400 ERR("Command error");
403 ERR("Undefined command or invalid arguments");
408 case CMD_UNSUPPORTED
:
409 ERR("Unsupported command");
432 int main(int argc
, char *argv
[])
436 progname
= argv
[0] ? argv
[0] : "lttng";
438 ret
= set_signal_handler();
443 ret
= parse_args(argc
, argv
);