From: Jérémie Galarneau Date: Wed, 6 Dec 2023 19:35:27 +0000 (-0500) Subject: lttng: enable-channel: move kernel tracer status check to util X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=4d4c8b8e68081f91347458b7e072dcfcac32385b;p=lttng-tools.git lttng: enable-channel: move kernel tracer status check to util In order to re-use the same logic in the enable-event command, move the kernel status checking and printing to a common utility function. Signed-off-by: Jérémie Galarneau Change-Id: I85956298c27fd7073ac02aac901d39e3d82bb280 --- diff --git a/src/bin/lttng/commands/enable_channels.cpp b/src/bin/lttng/commands/enable_channels.cpp index 1638d0d44..c596dcec9 100644 --- a/src/bin/lttng/commands/enable_channels.cpp +++ b/src/bin/lttng/commands/enable_channels.cpp @@ -142,7 +142,6 @@ static int enable_channel(char *session_name, char *channel_list) { struct lttng_channel *channel = nullptr; int ret = CMD_SUCCESS, warn = 0, error = 0, success = 0; - enum lttng_kernel_tracer_status kernel_tracer_status; char *channel_name; struct lttng_domain dom; @@ -285,15 +284,13 @@ static int enable_channel(char *session_name, char *channel_list) ret = lttng_enable_channel(handle, channel); if (ret < 0) { + bool msg_already_printed = false; + success = 0; switch (-ret) { case LTTNG_ERR_KERN_CHAN_EXIST: case LTTNG_ERR_UST_CHAN_EXIST: case LTTNG_ERR_CHAN_EXIST: - WARN("Channel %s: %s (session %s)", - channel_name, - lttng_strerror(ret), - session_name); warn = 1; break; case LTTNG_ERR_INVALID_CHANNEL_NAME: @@ -301,55 +298,27 @@ static int enable_channel(char *session_name, char *channel_list) "Channel names may not start with '.', and " "may not contain '/'.", channel_name); + msg_already_printed = true; error = 1; break; default: - ERR("Channel %s: %s (session %s)", - channel_name, - lttng_strerror(ret), - session_name); error = 1; break; } - /* - * Ask the sessiond for the more details on the status of the kernel tracer. - */ - ret = lttng_get_kernel_tracer_status(&kernel_tracer_status); - if (ret < 0) { - ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret)); - } else { - switch (kernel_tracer_status) { - case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED: - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN: - MSG("\tKernel module loading failed"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING: - MSG("\tMissing one or more required kernel modules"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE: - MSG("\tKernel module signature error prevented loading of one or more required kernel modules"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT: - MSG("\tlttng-sessiond isn't running as root"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER: - MSG("\tFailed to setup notifiers"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG: - MSG("\tlttng-sessiond failed to open proc lttng"); - break; - case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH: - MSG("\tVersion mismatch between kernel tracer and kernel tracer ABI"); - break; - default: - MSG("\tUnknown kernel tracer status (%d)", kernel_tracer_status); - break; - } - MSG("\tConsult lttng-sessiond logs for more information"); + + if (!msg_already_printed) { + LOG(error ? PRINT_ERR : PRINT_WARN, + "Failed to enable channel `%s` under session `%s`: %s", + channel_name, + session_name, + lttng_strerror(ret)); + } + + if (opt_kernel) { + print_kernel_tracer_status_error(); } } else { - MSG("%s channel %s enabled for session %s", + MSG("%s channel `%s` enabled for session `%s`", lttng_domain_type_str(dom.type), channel_name, session_name); diff --git a/src/bin/lttng/utils.cpp b/src/bin/lttng/utils.cpp index df03b1127..e799e2f90 100644 --- a/src/bin/lttng/utils.cpp +++ b/src/bin/lttng/utils.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -739,3 +740,54 @@ lttng::cli::session_list lttng::cli::list_sessions(const struct session_spec& sp return lttng::cli::session_list(); } + +void print_kernel_tracer_status_error() +{ + if (lttng_opt_mi) { + return; + } + + enum lttng_kernel_tracer_status kernel_tracer_status; + const auto ret = lttng_get_kernel_tracer_status(&kernel_tracer_status); + + if (ret < 0) { + ERR("Failed to get kernel tracer status: %s", lttng_strerror(ret)); + } else { + switch (kernel_tracer_status) { + case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED: + return; + case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN: + std::cerr << "\tKernel module loading failed" << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING: + std::cerr << "\tMissing one or more required kernel modules" << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE: + std::cerr + << "\tKernel module signature error prevented loading of one or more required kernel modules" + << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT: + std::cerr << "\tlttng-sessiond isn't running as root" << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER: + std::cerr << "\tFailed to setup notifiers" << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG: + std::cerr << "\tlttng-sessiond failed to open /proc/lttng" << std::endl; + break; + case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH: + std::cerr + << "\tVersion mismatch between kernel tracer and kernel tracer ABI" + << std::endl; + break; + default: + std::cerr << lttng::format("\t\tUnknown kernel tracer status (%d)", + static_cast(kernel_tracer_status)) + << std::endl; + break; + } + + std::cerr << "\tConsult lttng-sessiond logs for more information" << std::endl; + } +} diff --git a/src/bin/lttng/utils.hpp b/src/bin/lttng/utils.hpp index 0f42ef74e..d790e74d4 100644 --- a/src/bin/lttng/utils.hpp +++ b/src/bin/lttng/utils.hpp @@ -178,4 +178,10 @@ int print_trace_archive_location(const struct lttng_trace_archive_location *loca int validate_exclusion_list(const char *event_name, const struct lttng_dynamic_pointer_array *exclusions); +/* + * Ask the sessiond for the more details on the status of the kernel tracer and + * print it to stderr. + */ +void print_kernel_tracer_status_error(); + #endif /* _LTTNG_UTILS_H */