X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Flttng.cpp;h=af1d3f4882cfd7fc6894257099a85f785cd5f6b0;hp=2b169cb0ea52ef0db6902b6555aa9205569c371c;hb=HEAD;hpb=21cf9b6b1843774306a76f4dccddddd706b64f79 diff --git a/src/bin/lttng/lttng.cpp b/src/bin/lttng/lttng.cpp index 2b169cb0e..48b02c627 100644 --- a/src/bin/lttng/lttng.cpp +++ b/src/bin/lttng/lttng.cpp @@ -6,6 +6,16 @@ */ #define _LGPL_SOURCE +#include "command.hpp" +#include "version.hpp" + +#include +#include +#include + +#include + +#include #include #include #include @@ -14,23 +24,14 @@ #include #include #include -#include - -#include -#include -#include -#include - -#include "command.h" -#include "version.h" static const char *help_msg = #ifdef LTTNG_EMBED_HELP #include #else -NULL + nullptr #endif -; + ; /* Variables */ static const char *progname; @@ -47,61 +48,60 @@ enum { }; /* Getopt options. No first level command. */ -static struct option long_options[] = { - {"version", 0, NULL, 'V'}, - {"help", 0, NULL, 'h'}, - {"group", 1, NULL, 'g'}, - {"verbose", 0, NULL, 'v'}, - {"quiet", 0, NULL, 'q'}, - {"mi", 1, NULL, 'm'}, - {"no-sessiond", 0, NULL, 'n'}, - {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, - {"relayd-path", 1, NULL, OPT_RELAYD_PATH}, - {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, - {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, - {NULL, 0, NULL, 0} -}; +static struct option long_options[] = { { "version", 0, nullptr, 'V' }, + { "help", 0, nullptr, 'h' }, + { "group", 1, nullptr, 'g' }, + { "verbose", 0, nullptr, 'v' }, + { "quiet", 0, nullptr, 'q' }, + { "mi", 1, nullptr, 'm' }, + { "no-sessiond", 0, nullptr, 'n' }, + { "sessiond-path", 1, nullptr, OPT_SESSION_PATH }, + { "relayd-path", 1, nullptr, OPT_RELAYD_PATH }, + { "list-options", 0, nullptr, OPT_DUMP_OPTIONS }, + { "list-commands", 0, nullptr, OPT_DUMP_COMMANDS }, + { nullptr, 0, nullptr, 0 } }; /* First level command */ -static struct cmd_struct commands[] = { - { "add-context", cmd_add_context}, - { "add-trigger", cmd_add_trigger}, - { "create", cmd_create}, - { "clear", cmd_clear}, - { "destroy", cmd_destroy}, - { "disable-channel", cmd_disable_channels}, - { "disable-event", cmd_disable_events}, - { "enable-channel", cmd_enable_channels}, - { "enable-event", cmd_enable_events}, - { "help", NULL}, - { "list", cmd_list}, - { "list-triggers", cmd_list_triggers}, - { "load", cmd_load}, - { "metadata", cmd_metadata}, - { "regenerate", cmd_regenerate}, - { "remove-trigger", cmd_remove_trigger}, - { "rotate", cmd_rotate}, - { "enable-rotation", cmd_enable_rotation}, - { "disable-rotation", cmd_disable_rotation}, - { "save", cmd_save}, - { "set-session", cmd_set_session}, - { "snapshot", cmd_snapshot}, - { "start", cmd_start}, - { "status", cmd_status}, - { "stop", cmd_stop}, - { "track", cmd_track}, - { "untrack", cmd_untrack}, - { "version", cmd_version}, - { "view", cmd_view}, - { NULL, NULL} /* Array closure */ +static struct cmd_struct commands[] = { + { "add-context", cmd_add_context }, + { "add-trigger", cmd_add_trigger }, + { "create", cmd_create }, + { "clear", cmd_clear }, + { "destroy", cmd_destroy }, + { "disable-channel", cmd_disable_channels }, + { "disable-event", cmd_disable_events }, + { "enable-channel", cmd_enable_channels }, + { "enable-event", cmd_enable_events }, + { "help", nullptr }, + { "list", cmd_list }, + { "list-triggers", cmd_list_triggers }, + { "load", cmd_load }, + { "metadata", cmd_metadata }, + { "regenerate", cmd_regenerate }, + { "remove-trigger", cmd_remove_trigger }, + { "rotate", cmd_rotate }, + { "enable-rotation", cmd_enable_rotation }, + { "disable-rotation", cmd_disable_rotation }, + { "save", cmd_save }, + { "set-session", cmd_set_session }, + { "snapshot", cmd_snapshot }, + { "start", cmd_start }, + { "status", cmd_status }, + { "stop", cmd_stop }, + { "track", cmd_track }, + { "untrack", cmd_untrack }, + { "version", cmd_version }, + { "view", cmd_view }, + { nullptr, nullptr } /* Array closure */ }; static void version(FILE *ofp) { - fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s%s\n", - progname, - GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, - EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); + fprintf(ofp, + "%s (LTTng Trace Control) " VERSION " - " VERSION_NAME "%s%s\n", + progname, + GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION, + EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " EXTRA_VERSION_NAME); } /* @@ -132,10 +132,10 @@ static int mi_output_type(const char *output_type) static void list_options(FILE *ofp) { int i = 0; - struct option *option = NULL; + struct option *option = nullptr; option = &long_options[i]; - while (option->name != NULL) { + while (option->name != nullptr) { fprintf(ofp, "--%s\n", option->name); if (isprint(option->val)) { @@ -150,7 +150,7 @@ static void list_options(FILE *ofp) /* * clean_exit */ -static void clean_exit(int code) +__attribute__((noreturn)) static void clean_exit(int code) { DBG("Clean exit"); exit(code); @@ -164,13 +164,13 @@ static void clean_exit(int code) static void sighandler(int sig) { switch (sig) { - case SIGTERM: - DBG("SIGTERM caught"); - clean_exit(EXIT_FAILURE); - break; - default: - DBG("Unknown signal %d caught", sig); - break; + case SIGTERM: + DBG("SIGTERM caught"); + clean_exit(EXIT_FAILURE); + break; + default: + DBG("Unknown signal %d caught", sig); + break; } return; @@ -181,7 +181,7 @@ static void sighandler(int sig) * * Setup signal handler for SIGCHLD and SIGTERM. */ -static int set_signal_handler(void) +static int set_signal_handler() { int ret = 0; struct sigaction sa; @@ -196,7 +196,7 @@ static int set_signal_handler(void) sa.sa_mask = sigset; sa.sa_flags = 0; - if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { + if ((ret = sigaction(SIGTERM, &sa, nullptr)) < 0) { PERROR("sigaction"); goto end; } @@ -219,22 +219,28 @@ static int handle_command(int argc, char **argv) int i = 0, ret; struct cmd_struct *cmd; - if (*argv == NULL) { + if (*argv == nullptr) { ret = CMD_SUCCESS; goto end; } /* Special case for help command which needs the commands array */ if (strcmp(argv[0], "help") == 0) { - ret = cmd_help(argc, (const char**) argv, commands); + ret = cmd_help(argc, (const char **) argv, commands); goto end; } cmd = &commands[i]; - while (cmd->name != NULL) { + while (cmd->name != nullptr) { /* Find command */ if (strcmp(argv[0], cmd->name) == 0) { - ret = cmd->func(argc, (const char**) argv); + try { + ret = cmd->func(argc, (const char **) argv); + } catch (const std::exception& e) { + ERR_FMT("{}", e.what()); + ret = CMD_ERROR; + } + goto end; } i++; @@ -253,7 +259,7 @@ static bool command_exists(const char *command) const struct cmd_struct *cmd = commands; bool exists = false; - while (cmd->name != NULL) { + while (cmd->name != nullptr) { if (!strcmp(command, cmd->name)) { exists = true; goto end; @@ -265,7 +271,7 @@ end: return exists; } -static void show_basic_help(void) +static void show_basic_help() { puts("Usage: lttng [--group=GROUP] [--mi=TYPE] [--no-sessiond | --sessiond-path=PATH]"); puts(" [--quiet | -v | -vv | -vvv] COMMAND [COMMAND OPTIONS]"); @@ -334,7 +340,8 @@ static int parse_args(int argc, char **argv) int opt, ret; if (lttng_is_setuid_setgid()) { - ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]); + ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", + argv[0]); clean_exit(EXIT_FAILURE); } @@ -343,7 +350,7 @@ static int parse_args(int argc, char **argv) clean_exit(EXIT_FAILURE); } - while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, nullptr)) != -1) { switch (opt) { case 'V': version(stdout); @@ -431,10 +438,9 @@ static int parse_args(int argc, char **argv) case CMD_UNDEFINED: if (!command_exists(*(argv + optind))) { MSG("lttng: %s is not an lttng command. See 'lttng --help'.", - *(argv + optind)); + *(argv + optind)); } else { - ERR("Unrecognized argument used with \'%s\' command", - *(argv + optind)); + ERR("Unrecognized argument used with \'%s\' command", *(argv + optind)); } break; case CMD_FATAL: @@ -457,11 +463,10 @@ error: return ret; } - /* * main */ -int main(int argc, char *argv[]) +static int _main(int argc, char *argv[]) { int ret; @@ -479,3 +484,13 @@ int main(int argc, char *argv[]) return 0; } + +int main(int argc, char **argv) +{ + try { + return _main(argc, argv); + } catch (const std::exception& e) { + ERR_FMT("Unhandled exception caught by client: {}", e.what()); + abort(); + } +}