2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
19 #include <lttng/lttng.h>
20 #include <common/error.h>
21 #include <common/compat/getenv.h>
22 #include <common/utils.h>
27 static const char *help_msg
=
28 #ifdef LTTNG_EMBED_HELP
36 static const char *progname
;
38 char *opt_sessiond_path
;
40 char *opt_relayd_path
;
49 /* Getopt options. No first level command. */
50 static struct option long_options
[] = {
51 {"version", 0, NULL
, 'V'},
52 {"help", 0, NULL
, 'h'},
53 {"group", 1, NULL
, 'g'},
54 {"verbose", 0, NULL
, 'v'},
55 {"quiet", 0, NULL
, 'q'},
57 {"no-sessiond", 0, NULL
, 'n'},
58 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
59 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
60 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
61 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
65 /* First level command */
66 static struct cmd_struct commands
[] = {
67 { "add-context", cmd_add_context
},
68 { "add-trigger", cmd_add_trigger
},
69 { "create", cmd_create
},
70 { "clear", cmd_clear
},
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
},
78 { "list-triggers", cmd_list_triggers
},
80 { "metadata", cmd_metadata
},
81 { "regenerate", cmd_regenerate
},
82 { "remove-trigger", cmd_remove_trigger
},
83 { "rotate", cmd_rotate
},
84 { "enable-rotation", cmd_enable_rotation
},
85 { "disable-rotation", cmd_disable_rotation
},
87 { "set-session", cmd_set_session
},
88 { "snapshot", cmd_snapshot
},
89 { "start", cmd_start
},
90 { "status", cmd_status
},
92 { "track", cmd_track
},
93 { "untrack", cmd_untrack
},
94 { "version", cmd_version
},
96 { NULL
, NULL
} /* Array closure */
99 static void version(FILE *ofp
)
101 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s%s\n",
103 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
,
104 EXTRA_VERSION_NAME
[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME
);
108 * Find the MI output type enum from a string. This function is for the support
109 * of machine interface output.
111 static int mi_output_type(const char *output_type
)
115 if (!strncasecmp("xml", output_type
, 3)) {
118 /* Invalid output format */
119 ERR("MI output format not supported");
120 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
129 * List options line by line. This is mostly for bash auto completion and to
130 * avoid difficult parsing.
132 static void list_options(FILE *ofp
)
135 struct option
*option
= NULL
;
137 option
= &long_options
[i
];
138 while (option
->name
!= NULL
) {
139 fprintf(ofp
, "--%s\n", option
->name
);
141 if (isprint(option
->val
)) {
142 fprintf(ofp
, "-%c\n", option
->val
);
146 option
= &long_options
[i
];
153 __attribute__((noreturn
))
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 bool command_exists(const char *command
)
254 const struct cmd_struct
*cmd
= commands
;
257 while (cmd
->name
!= NULL
) {
258 if (!strcmp(command
, cmd
->name
)) {
269 static void show_basic_help(void)
271 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
272 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
274 puts("Available commands:");
276 puts("Recording sessions:");
277 puts(" create " CONFIG_CMD_DESCR_CREATE
);
278 puts(" clear " CONFIG_CMD_DESCR_CLEAR
);
279 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
280 puts(" load " CONFIG_CMD_DESCR_LOAD
);
281 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
282 puts(" save " CONFIG_CMD_DESCR_SAVE
);
283 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
286 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
287 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
288 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
290 puts("Recording event rules:");
291 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
292 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
295 puts(" list " CONFIG_CMD_DESCR_LIST
);
296 puts(" status " CONFIG_CMD_DESCR_STATUS
);
299 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
300 puts(" start " CONFIG_CMD_DESCR_START
);
301 puts(" stop " CONFIG_CMD_DESCR_STOP
);
303 puts("Recording session rotation:");
304 puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION
);
305 puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION
);
306 puts(" rotate " CONFIG_CMD_DESCR_ROTATE
);
308 puts("Resource tracking:");
309 puts(" track " CONFIG_CMD_DESCR_TRACK
);
310 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
313 puts(" add-trigger " CONFIG_CMD_DESCR_ADD_TRIGGER
);
314 puts(" remove-trigger " CONFIG_CMD_DESCR_REMOVE_TRIGGER
);
315 puts(" list-triggers " CONFIG_CMD_DESCR_LIST_TRIGGERS
);
317 puts("Miscellaneous:");
318 puts(" help " CONFIG_CMD_DESCR_HELP
);
319 puts(" version " CONFIG_CMD_DESCR_VERSION
);
320 puts(" view " CONFIG_CMD_DESCR_VIEW
);
322 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
323 puts("command COMMAND.");
325 puts("See `man lttng` for more help with the lttng command.");
329 * Parse command line arguments.
331 * Return 0 if OK, else -1
333 static int parse_args(int argc
, char **argv
)
337 if (lttng_is_setuid_setgid()) {
338 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
339 clean_exit(EXIT_FAILURE
);
344 clean_exit(EXIT_FAILURE
);
347 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
354 ret
= utils_show_help(1, "lttng", help_msg
);
356 ERR("Cannot show --help for `lttng`");
361 /* There is only 3 possible level of verbosity. (-vvv) */
362 if (lttng_opt_verbose
< 3) {
363 lttng_opt_verbose
+= 1;
370 lttng_opt_mi
= mi_output_type(optarg
);
371 if (lttng_opt_mi
< 0) {
377 lttng_set_tracing_group(optarg
);
382 case OPT_SESSION_PATH
:
383 free(opt_sessiond_path
);
384 opt_sessiond_path
= strdup(optarg
);
385 if (!opt_sessiond_path
) {
390 case OPT_RELAYD_PATH
:
391 free(opt_relayd_path
);
392 opt_relayd_path
= strdup(optarg
);
393 if (!opt_relayd_path
) {
398 case OPT_DUMP_OPTIONS
:
399 list_options(stdout
);
402 case OPT_DUMP_COMMANDS
:
403 list_commands(commands
, stdout
);
412 /* If both options are specified, quiet wins */
413 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
414 lttng_opt_verbose
= 0;
417 /* No leftovers, quit */
418 if ((argc
- optind
) == 0) {
424 * Handle leftovers which is a first level command with the trailing
427 ret
= handle_command(argc
- optind
, argv
+ optind
);
433 if (!command_exists(*(argv
+ optind
))) {
434 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
437 ERR("Unrecognized argument used with \'%s\' command",
442 case CMD_UNSUPPORTED
:
465 int main(int argc
, char *argv
[])
469 progname
= argv
[0] ? argv
[0] : "lttng";
471 ret
= set_signal_handler();
476 ret
= parse_args(argc
, argv
);