2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include "version.hpp"
12 #include <common/compat/getenv.hpp>
13 #include <common/error.hpp>
14 #include <common/utils.hpp>
16 #include <lttng/lttng.h>
24 #include <sys/types.h>
28 static const char *help_msg
=
29 #ifdef LTTNG_EMBED_HELP
37 static const 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
[] = { { "version", 0, nullptr, 'V' },
52 { "help", 0, nullptr, 'h' },
53 { "group", 1, nullptr, 'g' },
54 { "verbose", 0, nullptr, 'v' },
55 { "quiet", 0, nullptr, 'q' },
56 { "mi", 1, nullptr, 'm' },
57 { "no-sessiond", 0, nullptr, 'n' },
58 { "sessiond-path", 1, nullptr, OPT_SESSION_PATH
},
59 { "relayd-path", 1, nullptr, OPT_RELAYD_PATH
},
60 { "list-options", 0, nullptr, OPT_DUMP_OPTIONS
},
61 { "list-commands", 0, nullptr, OPT_DUMP_COMMANDS
},
62 { nullptr, 0, nullptr, 0 } };
64 /* First level command */
65 static struct cmd_struct commands
[] = {
66 { "add-context", cmd_add_context
},
67 { "add-trigger", cmd_add_trigger
},
68 { "create", cmd_create
},
69 { "clear", cmd_clear
},
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
},
77 { "list-triggers", cmd_list_triggers
},
79 { "metadata", cmd_metadata
},
80 { "regenerate", cmd_regenerate
},
81 { "remove-trigger", cmd_remove_trigger
},
82 { "rotate", cmd_rotate
},
83 { "enable-rotation", cmd_enable_rotation
},
84 { "disable-rotation", cmd_disable_rotation
},
86 { "set-session", cmd_set_session
},
87 { "snapshot", cmd_snapshot
},
88 { "start", cmd_start
},
89 { "status", cmd_status
},
91 { "track", cmd_track
},
92 { "untrack", cmd_untrack
},
93 { "version", cmd_version
},
95 { nullptr, nullptr } /* Array closure */
98 static void version(FILE *ofp
)
101 "%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
= nullptr;
137 option
= &long_options
[i
];
138 while (option
->name
!= nullptr) {
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
)) static void clean_exit(int code
)
162 * Signal handler for the daemon
164 static void sighandler(int sig
)
168 DBG("SIGTERM caught");
169 clean_exit(EXIT_FAILURE
);
172 DBG("Unknown signal %d caught", sig
);
182 * Setup signal handler for SIGCHLD and SIGTERM.
184 static int set_signal_handler()
190 if ((ret
= sigemptyset(&sigset
)) < 0) {
191 PERROR("sigemptyset");
195 sa
.sa_handler
= sighandler
;
199 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
211 * Handle the full argv list of a first level command. Will find the command
212 * in the global commands array and call the function callback associated.
214 * If command not found, return -1
215 * else, return function command error code.
217 static int handle_command(int argc
, char **argv
)
220 struct cmd_struct
*cmd
;
222 if (*argv
== nullptr) {
227 /* Special case for help command which needs the commands array */
228 if (strcmp(argv
[0], "help") == 0) {
229 ret
= cmd_help(argc
, (const char **) argv
, commands
);
234 while (cmd
->name
!= nullptr) {
236 if (strcmp(argv
[0], cmd
->name
) == 0) {
238 ret
= cmd
->func(argc
, (const char **) argv
);
239 } catch (const std::exception
& e
) {
240 ERR_FMT("{}", e
.what());
250 /* Command not found */
257 static bool command_exists(const char *command
)
259 const struct cmd_struct
*cmd
= commands
;
262 while (cmd
->name
!= nullptr) {
263 if (!strcmp(command
, cmd
->name
)) {
274 static void show_basic_help()
276 puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]");
277 puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]");
279 puts("Available commands:");
281 puts("Recording sessions:");
282 puts(" create " CONFIG_CMD_DESCR_CREATE
);
283 puts(" clear " CONFIG_CMD_DESCR_CLEAR
);
284 puts(" destroy " CONFIG_CMD_DESCR_DESTROY
);
285 puts(" load " CONFIG_CMD_DESCR_LOAD
);
286 puts(" regenerate " CONFIG_CMD_DESCR_REGENERATE
);
287 puts(" save " CONFIG_CMD_DESCR_SAVE
);
288 puts(" set-session " CONFIG_CMD_DESCR_SET_SESSION
);
291 puts(" add-context " CONFIG_CMD_DESCR_ADD_CONTEXT
);
292 puts(" disable-channel " CONFIG_CMD_DESCR_DISABLE_CHANNEL
);
293 puts(" enable-channel " CONFIG_CMD_DESCR_ENABLE_CHANNEL
);
295 puts("Recording event rules:");
296 puts(" disable-event " CONFIG_CMD_DESCR_DISABLE_EVENT
);
297 puts(" enable-event " CONFIG_CMD_DESCR_ENABLE_EVENT
);
300 puts(" list " CONFIG_CMD_DESCR_LIST
);
301 puts(" status " CONFIG_CMD_DESCR_STATUS
);
304 puts(" snapshot " CONFIG_CMD_DESCR_SNAPSHOT
);
305 puts(" start " CONFIG_CMD_DESCR_START
);
306 puts(" stop " CONFIG_CMD_DESCR_STOP
);
308 puts("Recording session rotation:");
309 puts(" disable-rotation " CONFIG_CMD_DESCR_DISABLE_ROTATION
);
310 puts(" enable-rotation " CONFIG_CMD_DESCR_ENABLE_ROTATION
);
311 puts(" rotate " CONFIG_CMD_DESCR_ROTATE
);
313 puts("Resource tracking:");
314 puts(" track " CONFIG_CMD_DESCR_TRACK
);
315 puts(" untrack " CONFIG_CMD_DESCR_UNTRACK
);
318 puts(" add-trigger " CONFIG_CMD_DESCR_ADD_TRIGGER
);
319 puts(" remove-trigger " CONFIG_CMD_DESCR_REMOVE_TRIGGER
);
320 puts(" list-triggers " CONFIG_CMD_DESCR_LIST_TRIGGERS
);
322 puts("Miscellaneous:");
323 puts(" help " CONFIG_CMD_DESCR_HELP
);
324 puts(" version " CONFIG_CMD_DESCR_VERSION
);
325 puts(" view " CONFIG_CMD_DESCR_VIEW
);
327 puts("Run `lttng help COMMAND` or `lttng COMMAND --help` to get help with");
328 puts("command COMMAND.");
330 puts("See `man lttng` for more help with the lttng command.");
334 * Parse command line arguments.
336 * Return 0 if OK, else -1
338 static int parse_args(int argc
, char **argv
)
342 if (lttng_is_setuid_setgid()) {
343 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.",
345 clean_exit(EXIT_FAILURE
);
350 clean_exit(EXIT_FAILURE
);
353 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, nullptr)) != -1) {
360 ret
= utils_show_help(1, "lttng", help_msg
);
362 ERR("Cannot show --help for `lttng`");
367 /* There is only 3 possible level of verbosity. (-vvv) */
368 if (lttng_opt_verbose
< 3) {
369 lttng_opt_verbose
+= 1;
376 lttng_opt_mi
= mi_output_type(optarg
);
377 if (lttng_opt_mi
< 0) {
383 lttng_set_tracing_group(optarg
);
388 case OPT_SESSION_PATH
:
389 free(opt_sessiond_path
);
390 opt_sessiond_path
= strdup(optarg
);
391 if (!opt_sessiond_path
) {
396 case OPT_RELAYD_PATH
:
397 free(opt_relayd_path
);
398 opt_relayd_path
= strdup(optarg
);
399 if (!opt_relayd_path
) {
404 case OPT_DUMP_OPTIONS
:
405 list_options(stdout
);
408 case OPT_DUMP_COMMANDS
:
409 list_commands(commands
, stdout
);
418 /* If both options are specified, quiet wins */
419 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
420 lttng_opt_verbose
= 0;
423 /* No leftovers, quit */
424 if ((argc
- optind
) == 0) {
430 * Handle leftovers which is a first level command with the trailing
433 ret
= handle_command(argc
- optind
, argv
+ optind
);
439 if (!command_exists(*(argv
+ optind
))) {
440 MSG("lttng: %s is not an lttng command. See 'lttng --help'.",
443 ERR("Unrecognized argument used with \'%s\' command", *(argv
+ optind
));
447 case CMD_UNSUPPORTED
:
469 static int _main(int argc
, char *argv
[])
473 progname
= argv
[0] ? argv
[0] : "lttng";
475 ret
= set_signal_handler();
480 ret
= parse_args(argc
, argv
);
488 int main(int argc
, char **argv
)
491 return _main(argc
, argv
);
492 } catch (const std::exception
& e
) {
493 ERR_FMT("Unhandled exception caught by client: %s", e
.what());