X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fdisable_events.cpp;h=f78222d9725032c42ef609bb44c14d16ee09f2da;hb=66cefebdc240cbae0bc79594305f509b0779fa98;hp=b269f138cbb4060f2812a7b35cc05b94e5fc45e5;hpb=21cf9b6b1843774306a76f4dccddddd706b64f79;p=lttng-tools.git diff --git a/src/bin/lttng/commands/disable_events.cpp b/src/bin/lttng/commands/disable_events.cpp index b269f138c..f78222d97 100644 --- a/src/bin/lttng/commands/disable_events.cpp +++ b/src/bin/lttng/commands/disable_events.cpp @@ -6,6 +6,12 @@ */ #define _LGPL_SOURCE +#include "../command.hpp" + +#include + +#include + #include #include #include @@ -14,12 +20,6 @@ #include #include -#include -#include - -#include "../command.h" - -static char *opt_event_list; static int opt_kernel; static char *opt_channel_name; static char *opt_session_name; @@ -33,7 +33,7 @@ static int opt_event_type; #ifdef LTTNG_EMBED_HELP static const char help_msg[] = #include -; + ; #endif enum { @@ -51,38 +51,35 @@ static struct mi_writer *writer; static struct poptOption long_options[] = { /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ - {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, - {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, - {"all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, 0, 0}, - {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, - {"jul", 'j', POPT_ARG_VAL, &opt_jul, 1, 0, 0}, - {"log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, 0, 0}, - {"python", 'p', POPT_ARG_VAL, &opt_python, 1, 0, 0}, - {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, - {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0}, - {"syscall", 0, POPT_ARG_NONE, 0, OPT_TYPE_SYSCALL, 0, 0}, - {"probe", 0, POPT_ARG_NONE, 0, OPT_TYPE_PROBE, 0, 0}, - {"tracepoint", 0, POPT_ARG_NONE, 0, OPT_TYPE_TRACEPOINT, 0, 0}, - {"function", 0, POPT_ARG_NONE, 0, OPT_TYPE_FUNCTION, 0, 0}, - {"all", 0, POPT_ARG_NONE, 0, OPT_TYPE_ALL, 0, 0}, - {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, - {0, 0, 0, 0, 0, 0, 0} + { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, + { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr }, + { "all-events", 'a', POPT_ARG_VAL, &opt_disable_all, 1, nullptr, nullptr }, + { "channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, nullptr, nullptr }, + { "jul", 'j', POPT_ARG_VAL, &opt_jul, 1, nullptr, nullptr }, + { "log4j", 'l', POPT_ARG_VAL, &opt_log4j, 1, nullptr, nullptr }, + { "python", 'p', POPT_ARG_VAL, &opt_python, 1, nullptr, nullptr }, + { "kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, nullptr, nullptr }, + { "userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, nullptr, nullptr }, + { "syscall", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_SYSCALL, nullptr, nullptr }, + { "probe", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_PROBE, nullptr, nullptr }, + { "tracepoint", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_TRACEPOINT, nullptr, nullptr }, + { "function", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_FUNCTION, nullptr, nullptr }, + { "all", 0, POPT_ARG_NONE, nullptr, OPT_TYPE_ALL, nullptr, nullptr }, + { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, + { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } }; -static -const char *print_channel_name(const char *name) +static const char *print_channel_name(const char *name) { - return name ? : DEFAULT_CHANNEL_NAME; + return name ?: DEFAULT_CHANNEL_NAME; } -static -const char *print_raw_channel_name(const char *name) +static const char *print_raw_channel_name(const char *name) { - return name ? : ""; + return name ?: ""; } -static -const char *print_event_type(const enum lttng_event_type ev_type) +static const char *print_event_type(const enum lttng_event_type ev_type) { switch (ev_type) { case LTTNG_EVENT_ALL: @@ -120,22 +117,19 @@ static int mi_print_event(const char *event_name, int enabled, int success) } /* Print the name of event */ - ret = mi_lttng_writer_write_element_string(writer, - config_element_name, event_name); + ret = mi_lttng_writer_write_element_string(writer, config_element_name, event_name); if (ret) { goto end; } /* Print enabled ? */ - ret = mi_lttng_writer_write_element_bool(writer, - config_element_enabled, enabled); + ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled, enabled); if (ret) { goto end; } /* Success ? */ - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); + ret = mi_lttng_writer_write_element_bool(writer, mi_lttng_element_command_success, success); if (ret) { goto end; } @@ -151,11 +145,11 @@ end: * * Disabling event using the lttng API. */ -static int disable_events(char *session_name) +static int disable_events(char *session_name, char *event_list) { - int ret = CMD_SUCCESS, warn = 0, command_ret = CMD_SUCCESS; - int enabled = 1, success = 1; - char *event_name, *channel_name = NULL; + enum cmd_error_code ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; + bool enabled = true, success = true, warn = false; + char *event_name, *channel_name = nullptr; struct lttng_domain dom; struct lttng_event event; @@ -180,29 +174,29 @@ static int disable_events(char *session_name) channel_name = opt_channel_name; handle = lttng_create_handle(session_name, &dom); - if (handle == NULL) { - ret = -1; + if (handle == nullptr) { + ret = CMD_ERROR; goto error; } /* Mi print the channel and open the events element */ if (lttng_opt_mi) { - ret = mi_lttng_writer_open_element(writer, config_element_channel); - if (ret) { + int mi_ret = mi_lttng_writer_open_element(writer, config_element_channel); + if (mi_ret) { ret = CMD_ERROR; goto end; } - ret = mi_lttng_writer_write_element_string(writer, - config_element_name, print_channel_name(channel_name)); - if (ret) { + mi_ret = mi_lttng_writer_write_element_string( + writer, config_element_name, print_channel_name(channel_name)); + if (mi_ret) { ret = CMD_ERROR; goto end; } /* Open events element */ - ret = mi_lttng_writer_open_element(writer, config_element_events); - if (ret) { + mi_ret = mi_lttng_writer_open_element(writer, config_element_events); + if (mi_ret) { ret = CMD_ERROR; goto end; } @@ -216,82 +210,88 @@ static int disable_events(char *session_name) event.type = (lttng_event_type) opt_event_type; if (opt_disable_all) { - command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL); - if (command_ret < 0) { - ERR("%s", lttng_strerror(command_ret)); - enabled = 1; - success = 0; + const int disable_ret = lttng_disable_event_ext(handle, &event, channel_name, nullptr); + if (disable_ret < 0) { + ERR("%s", lttng_strerror(command_ret)); + command_ret = CMD_ERROR; + enabled = true; + success = false; } else { - enabled = 0; - success = 1; + enabled = false; + success = true; MSG("All %s events of type %s are disabled in channel %s", - lttng_domain_type_str(dom.type), - print_event_type((lttng_event_type) opt_event_type), - print_channel_name(channel_name)); + lttng_domain_type_str(dom.type), + print_event_type((lttng_event_type) opt_event_type), + print_channel_name(channel_name)); } if (lttng_opt_mi) { - ret = mi_print_event("*", enabled, success); - if (ret) { + const int mi_ret = mi_print_event("*", enabled, success); + + if (mi_ret) { ret = CMD_ERROR; goto error; } } } else { /* Strip event list */ - event_name = strtok(opt_event_list, ","); - while (event_name != NULL) { + event_name = strtok(event_list, ","); + while (event_name != nullptr) { DBG("Disabling event %s", event_name); strncpy(event.name, event_name, sizeof(event.name)); event.name[sizeof(event.name) - 1] = '\0'; - command_ret = lttng_disable_event_ext(handle, &event, channel_name, NULL); - if (command_ret < 0) { + const int disable_ret = + lttng_disable_event_ext(handle, &event, channel_name, nullptr); + if (disable_ret < 0) { ERR("%s of type %s : %s (channel %s, session %s)", - event_name, - print_event_type((lttng_event_type) opt_event_type), - lttng_strerror(command_ret), - command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME - ? print_raw_channel_name(channel_name) - : print_channel_name(channel_name), - session_name); - warn = 1; - success = 0; + event_name, + print_event_type((lttng_event_type) opt_event_type), + lttng_strerror(command_ret), + command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ? + print_raw_channel_name(channel_name) : + print_channel_name(channel_name), + session_name); + warn = true; + success = false; /* * If an error occurred we assume that the event is still * enabled. */ - enabled = 1; + enabled = true; + command_ret = CMD_ERROR; } else { MSG("%s %s of type %s disabled in channel %s for session %s", - lttng_domain_type_str(dom.type), - event_name, - print_event_type((lttng_event_type) opt_event_type), - print_channel_name(channel_name), - session_name); - success = 1; - enabled = 0; + lttng_domain_type_str(dom.type), + event_name, + print_event_type((lttng_event_type) opt_event_type), + print_channel_name(channel_name), + session_name); + success = true; + enabled = false; } if (lttng_opt_mi) { - ret = mi_print_event(event_name, enabled, success); - if (ret) { + const int mi_ret = mi_print_event(event_name, enabled, success); + + if (mi_ret) { ret = CMD_ERROR; goto error; } } /* Next event */ - event_name = strtok(NULL, ","); + event_name = strtok(nullptr, ","); } } end: if (lttng_opt_mi) { /* Close events element and channel element */ - ret = mi_lttng_close_multi_element(writer, 2); - if (ret) { + const int mi_ret = mi_lttng_close_multi_element(writer, 2); + + if (mi_ret) { ret = CMD_ERROR; } } @@ -317,11 +317,13 @@ int cmd_disable_events(int argc, const char **argv) { int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; static poptContext pc; - char *session_name = NULL; - const char *leftover = NULL; + char *session_name = nullptr; + char *event_list = nullptr; + const char *arg_event_list = nullptr; + const char *leftover = nullptr; int event_type = -1; - pc = poptGetContext(NULL, argc, argv, long_options, 0); + pc = poptGetContext(nullptr, argc, argv, long_options, 0); poptReadDefaultConfig(pc, 0); /* Default event type */ @@ -368,29 +370,36 @@ int cmd_disable_events(int argc, const char **argv) } ret = print_missing_or_multiple_domains( - opt_kernel + opt_userspace + opt_jul + opt_log4j + - opt_python, - true); + opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python, true); if (ret) { ret = CMD_ERROR; goto end; } /* Ust and agent only support ALL event type */ - if ((opt_userspace || opt_jul || opt_log4j || opt_python) - && opt_event_type != LTTNG_EVENT_ALL) { + if ((opt_userspace || opt_jul || opt_log4j || opt_python) && + opt_event_type != LTTNG_EVENT_ALL) { ERR("Disabling userspace and agent (-j | -l | -p) event(s) based on instrumentation type is not supported.\n"); ret = CMD_ERROR; goto end; } - opt_event_list = (char*) poptGetArg(pc); - if (opt_event_list == NULL && opt_disable_all == 0) { + arg_event_list = poptGetArg(pc); + if (arg_event_list == nullptr && opt_disable_all == 0) { ERR("Missing event name(s).\n"); ret = CMD_ERROR; goto end; } + if (opt_disable_all == 0) { + event_list = strdup(arg_event_list); + if (event_list == nullptr) { + PERROR("Failed to copy event name(s)"); + ret = CMD_ERROR; + goto end; + } + } + leftover = poptGetArg(pc); if (leftover) { ERR("Unknown argument: %s", leftover); @@ -400,7 +409,7 @@ int cmd_disable_events(int argc, const char **argv) if (!opt_session_name) { session_name = get_session_name(); - if (session_name == NULL) { + if (session_name == nullptr) { ret = CMD_ERROR; goto end; } @@ -417,23 +426,21 @@ int cmd_disable_events(int argc, const char **argv) } /* Open command element */ - ret = mi_lttng_writer_command_open(writer, - mi_lttng_element_command_disable_event); + ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_disable_event); if (ret) { ret = CMD_ERROR; goto end; } /* Open output element */ - ret = mi_lttng_writer_open_element(writer, - mi_lttng_element_command_output); + ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); if (ret) { ret = CMD_ERROR; goto end; } } - command_ret = disable_events(session_name); + command_ret = disable_events(session_name, event_list); if (command_ret) { success = 0; } @@ -447,8 +454,8 @@ int cmd_disable_events(int argc, const char **argv) goto end; } - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); + ret = mi_lttng_writer_write_element_bool( + writer, mi_lttng_element_command_success, success); if (ret) { ret = CMD_ERROR; goto end; @@ -467,6 +474,8 @@ end: free(session_name); } + free(event_list); + /* Mi clean-up */ if (writer && mi_lttng_writer_destroy(writer)) { /* Preserve original error code */