lttng: enable-channel: move kernel tracer status check to util
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 6 Dec 2023 19:35:27 +0000 (14:35 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 28 Feb 2024 15:39:28 +0000 (10:39 -0500)
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 <jeremie.galarneau@efficios.com>
Change-Id: I85956298c27fd7073ac02aac901d39e3d82bb280

src/bin/lttng/commands/enable_channels.cpp
src/bin/lttng/utils.cpp
src/bin/lttng/utils.hpp

index 1638d0d446b88108aa2d96e595f622a2831e6298..c596dcec9cf388a21c1dbf6e64dd41a9b0253f29 100644 (file)
@@ -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);
index df03b1127ed96be5b96614d57389d3c60dc719d8..e799e2f906e5e6fed14311dfc704b5472a9d775c 100644 (file)
@@ -20,6 +20,7 @@
 #include <ctype.h>
 #include <fnmatch.h>
 #include <inttypes.h>
+#include <iostream>
 #include <limits.h>
 #include <netinet/in.h>
 #include <signal.h>
@@ -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<int>(kernel_tracer_status))
+                                 << std::endl;
+                       break;
+               }
+
+               std::cerr << "\tConsult lttng-sessiond logs for more information" << std::endl;
+       }
+}
index 0f42ef74e1269803a2d3aff262830f1b29ea790b..d790e74d4f8bed24b89cbe768656c202df311266 100644 (file)
@@ -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 */
This page took 0.028824 seconds and 4 git commands to generate.