Fix: lttng-destroy: string formating error when default session is unset
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 21 Mar 2024 19:31:25 +0000 (15:31 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 22 Mar 2024 14:56:00 +0000 (10:56 -0400)
Using `lttng destroy` when no default session is set in .lttngrc results
in the following print-out:

  Error: Can't find valid lttng config /root/lttng-build/home/.lttngrc
  Did you create a session? (lttng create <my_session>)
  Error: Failed to format string: string pointer is null

This is because the client attempts to format the following message:
  ERR_FMT("Session `{}` not found", spec.value);

When no default session could be found in .lttngrc, spec.value is left
at nullptr and it is assumed that the listing succeeded.

A new CLI-specific exception, no_default_session_error, is added to the
project and thrown when the session listing fails. This allows the
calling code to mark the listing as having failed.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I33b4f38a424f22dfa9d3628cf12441b59df53f12

src/bin/lttng/Makefile.am
src/bin/lttng/commands/destroy.cpp
src/bin/lttng/commands/start.cpp
src/bin/lttng/commands/stop.cpp
src/bin/lttng/utils.cpp
src/common/exception.hpp

index 09c5171d8f99f7b055b4bf7c7f25a1d57426c3fa..e3718374637deda24acccd4bc1ccc5c0cd7443ae 100644 (file)
@@ -34,7 +34,8 @@ lttng_SOURCES = command.hpp conf.cpp conf.hpp commands/start.cpp \
                                commands/list_triggers.cpp \
                                commands/remove_trigger.cpp \
                                utils.cpp utils.hpp lttng.cpp \
-                               uprobe.cpp uprobe.hpp
+                               uprobe.cpp uprobe.hpp \
+                               exception.hpp exception.cpp
 
 lttng_CXXFLAGS = $(AM_CXXFLAGS) $(POPT_CFLAGS)
 
index 5e19d4189025181682d2040d503290cc9d0172cf..ef4b914d38bd39f124debce1dfb31600ba572a50 100644 (file)
@@ -7,6 +7,7 @@
 
 #define _LGPL_SOURCE
 #include "../command.hpp"
+#include "../exception.hpp"
 
 #include <common/exception.hpp>
 #include <common/make-unique-wrapper.hpp>
@@ -272,6 +273,14 @@ cmd_error_code destroy_sessions(const lttng::cli::session_spec& spec)
                                lttng_strerror(-ctl_exception.code()));
                        listing_failed = true;
                        return {};
+               } catch (const lttng::cli::no_default_session_error& cli_exception) {
+                       /*
+                        * The retrieval of the default session name already logs
+                        * an error when it fails. There is no value in printing
+                        * anything about this exception.
+                        */
+                       listing_failed = true;
+                       return {};
                }
        }();
 
index f78d3e708aa80c0fd05865cd36ef965ada3790e8..f00ce973e39c59034dec8c3c270d7bd229ab496f 100644 (file)
@@ -7,6 +7,7 @@
 
 #define _LGPL_SOURCE
 #include "../command.hpp"
+#include "../exception.hpp"
 #include "../utils.hpp"
 
 #include <common/exception.hpp>
@@ -117,6 +118,14 @@ cmd_error_code start_tracing(const lttng::cli::session_spec& spec)
                                lttng_strerror(-ctl_exception.code()));
                        listing_failed = true;
                        return {};
+               } catch (const lttng::cli::no_default_session_error& cli_exception) {
+                       /*
+                        * The retrieval of the default session name already logs
+                        * an error when it fails. There is no value in printing
+                        * anything about this exception.
+                        */
+                       listing_failed = true;
+                       return {};
                }
        }();
 
index af42d98db1f291f12d615976b29d3646cd573316..9d5533a2175c361be78f159a56628517afed9c13 100644 (file)
@@ -7,6 +7,7 @@
 
 #define _LGPL_SOURCE
 #include "../command.hpp"
+#include "../exception.hpp"
 #include "../utils.hpp"
 
 #include <common/exception.hpp>
@@ -146,6 +147,14 @@ cmd_error_code stop_tracing(const lttng::cli::session_spec& spec)
                                lttng_strerror(-ctl_exception.code()));
                        listing_failed = true;
                        return {};
+               } catch (const lttng::cli::no_default_session_error& cli_exception) {
+                       /*
+                        * The retrieval of the default session name already logs
+                        * an error when it fails. There is no value in printing
+                        * anything about this exception.
+                        */
+                       listing_failed = true;
+                       return {};
                }
        }();
 
index 403ee018579f755819877ce5d25109b7f94f24a4..45fe256a579d092b4a1d92ddd81e26459b315a1e 100644 (file)
@@ -8,6 +8,7 @@
 #define _LGPL_SOURCE
 #include "command.hpp"
 #include "conf.hpp"
+#include "exception.hpp"
 #include "utils.hpp"
 
 #include <common/defaults.hpp>
@@ -715,15 +716,14 @@ lttng::cli::session_list lttng::cli::list_sessions(const struct session_spec& sp
                                lttng::make_unique_wrapper<char, lttng::memory::free>(
                                        get_session_name());
 
-                       if (configured_name) {
-                               const struct lttng::cli::session_spec new_spec(
-                                       lttng::cli::session_spec::type::NAME,
-                                       configured_name.get());
-
-                               return list_sessions(new_spec);
+                       if (!configured_name) {
+                               LTTNG_THROW_CLI_NO_DEFAULT_SESSION();
                        }
 
-                       return lttng::cli::session_list();
+                       const struct lttng::cli::session_spec new_spec(
+                               lttng::cli::session_spec::type::NAME, configured_name.get());
+
+                       return list_sessions(new_spec);
                }
 
                return get_sessions(
index e43094bb6e3a0baae83466295b4f41abba1d3047..1880978e76997c3ee9df65f556516fd5f16b9581 100644 (file)
@@ -20,7 +20,7 @@
        throw lttng::posix_error(msg, errno_code, __FILE__, __func__, __LINE__)
 #define LTTNG_THROW_ERROR(msg) throw lttng::runtime_error(msg, __FILE__, __func__, __LINE__)
 #define LTTNG_THROW_UNSUPPORTED_ERROR(msg) \
-       throw lttng::runtime_error(msg, __FILE__, __func__, __LINE__)
+       throw lttng::unsupported_error(msg, __FILE__, __func__, __LINE__)
 #define LTTNG_THROW_COMMUNICATION_ERROR(msg) \
        throw lttng::communication_error(msg, __FILE__, __func__, __LINE__)
 #define LTTNG_THROW_PROTOCOL_ERROR(msg) \
This page took 0.039901 seconds and 4 git commands to generate.