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 { "calibrate", cmd_calibrate
},
70 { "create", cmd_create
},
71 { "destroy", cmd_destroy
},
72 { "disable-channel", cmd_disable_channels
},
73 { "disable-event", cmd_disable_events
},
74 { "enable-channel", cmd_enable_channels
},
75 { "enable-event", cmd_enable_events
},
79 { "metadata", cmd_metadata
},
81 { "set-session", cmd_set_session
},
82 { "snapshot", cmd_snapshot
},
83 { "start", cmd_start
},
84 { "status", cmd_status
},
86 { "track", cmd_track
},
87 { "untrack", cmd_untrack
},
89 { "version", cmd_version
},
91 { "regenerate", cmd_regenerate
},
92 { NULL
, NULL
} /* Array closure */
95 static void version(FILE *ofp
)
97 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
99 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
103 * Find the MI output type enum from a string. This function is for the support
104 * of machine interface output.
106 static int mi_output_type(const char *output_type
)
110 if (!strncasecmp("xml", output_type
, 3)) {
113 /* Invalid output format */
114 ERR("MI output format not supported");
115 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
124 * List options line by line. This is mostly for bash auto completion and to
125 * avoid difficult parsing.
127 static void list_options(FILE *ofp
)
130 struct option
*option
= NULL
;
132 option
= &long_options
[i
];
133 while (option
->name
!= NULL
) {
134 fprintf(ofp
, "--%s\n", option
->name
);
136 if (isprint(option
->val
)) {
137 fprintf(ofp
, "-%c\n", option
->val
);
141 option
= &long_options
[i
];
148 static void clean_exit(int code
)
157 * Signal handler for the daemon
159 static void sighandler(int sig
)
163 DBG("SIGTERM caught");
164 clean_exit(EXIT_FAILURE
);
167 DBG("Unknown signal %d caught", sig
);
177 * Setup signal handler for SIGCHLD and SIGTERM.
179 static int set_signal_handler(void)
185 if ((ret
= sigemptyset(&sigset
)) < 0) {
186 PERROR("sigemptyset");
190 sa
.sa_handler
= sighandler
;
194 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
206 * Handle the full argv list of a first level command. Will find the command
207 * in the global commands array and call the function callback associated.
209 * If command not found, return -1
210 * else, return function command error code.
212 static int handle_command(int argc
, char **argv
)
215 struct cmd_struct
*cmd
;
222 /* Special case for help command which needs the commands array */
223 if (strcmp(argv
[0], "help") == 0) {
224 ret
= cmd_help(argc
, (const char**) argv
, commands
);
229 while (cmd
->name
!= NULL
) {
231 if (strcmp(argv
[0], cmd
->name
) == 0) {
232 ret
= cmd
->func(argc
, (const char**) argv
);
239 /* Command not found */
246 static void show_basic_help(void)
248 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
249 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
251 puts("Available commands:");
253 puts("Tracing sessions:");
254 puts(" create " CONFIG_CMD_DESCR_CREATE
);
255 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
256 puts(" load " CONFIG_CMD_DESCR_LOAD
);
257 puts(" metadata " CONFIG_CMD_DESCR_METADATA
);
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(" calibrate " CONFIG_CMD_DESCR_CALIBRATE
);
285 puts(" help " CONFIG_CMD_DESCR_HELP
);
286 puts(" version " CONFIG_CMD_DESCR_VERSION
);
287 puts(" view " CONFIG_CMD_DESCR_VIEW
);
289 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
290 puts("command COMMAND.");
292 puts("See `man lttng` for more help with the lttng command.");
296 * Parse command line arguments.
298 * Return 0 if OK, else -1
300 static int parse_args(int argc
, char **argv
)
304 if (lttng_is_setuid_setgid()) {
305 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
306 clean_exit(EXIT_FAILURE
);
311 clean_exit(EXIT_FAILURE
);
314 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
321 ret
= utils_show_man_page(1, "lttng");
324 ERR("Cannot view man page lttng(1)");
329 /* There is only 3 possible level of verbosity. (-vvv) */
330 if (lttng_opt_verbose
< 3) {
331 lttng_opt_verbose
+= 1;
338 lttng_opt_mi
= mi_output_type(optarg
);
339 if (lttng_opt_mi
< 0) {
345 lttng_set_tracing_group(optarg
);
350 case OPT_SESSION_PATH
:
351 free(opt_sessiond_path
);
352 opt_sessiond_path
= strdup(optarg
);
353 if (!opt_sessiond_path
) {
358 case OPT_RELAYD_PATH
:
359 free(opt_relayd_path
);
360 opt_relayd_path
= strdup(optarg
);
361 if (!opt_relayd_path
) {
366 case OPT_DUMP_OPTIONS
:
367 list_options(stdout
);
370 case OPT_DUMP_COMMANDS
:
371 list_commands(commands
, stdout
);
380 /* If both options are specified, quiet wins */
381 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
382 lttng_opt_verbose
= 0;
385 /* No leftovers, quit */
386 if ((argc
- optind
) == 0) {
392 * Handle leftovers which is a first level command with the trailing
395 ret
= handle_command(argc
- optind
, argv
+ optind
);
398 WARN("Some command(s) went wrong");
401 ERR("Command error");
404 ERR("Undefined command or invalid arguments");
409 case CMD_UNSUPPORTED
:
410 ERR("Unsupported command");
433 int main(int argc
, char *argv
[])
437 progname
= argv
[0] ? argv
[0] : "lttng";
439 ret
= set_signal_handler();
444 ret
= parse_args(argc
, argv
);