From 0729ea5558053d5a1a0d7aea51febf63ee1f3b3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 21 Mar 2024 15:31:25 -0400 Subject: [PATCH] Fix: lttng-destroy: string formating error when default session is unset MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 ) 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 Change-Id: I33b4f38a424f22dfa9d3628cf12441b59df53f12 --- src/bin/lttng/Makefile.am | 3 ++- src/bin/lttng/commands/destroy.cpp | 9 +++++++++ src/bin/lttng/commands/start.cpp | 9 +++++++++ src/bin/lttng/commands/stop.cpp | 9 +++++++++ src/bin/lttng/utils.cpp | 14 +++++++------- src/common/exception.hpp | 2 +- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/bin/lttng/Makefile.am b/src/bin/lttng/Makefile.am index 09c5171d8..e37183746 100644 --- a/src/bin/lttng/Makefile.am +++ b/src/bin/lttng/Makefile.am @@ -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) diff --git a/src/bin/lttng/commands/destroy.cpp b/src/bin/lttng/commands/destroy.cpp index 5e19d4189..ef4b914d3 100644 --- a/src/bin/lttng/commands/destroy.cpp +++ b/src/bin/lttng/commands/destroy.cpp @@ -7,6 +7,7 @@ #define _LGPL_SOURCE #include "../command.hpp" +#include "../exception.hpp" #include #include @@ -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 {}; } }(); diff --git a/src/bin/lttng/commands/start.cpp b/src/bin/lttng/commands/start.cpp index f78d3e708..f00ce973e 100644 --- a/src/bin/lttng/commands/start.cpp +++ b/src/bin/lttng/commands/start.cpp @@ -7,6 +7,7 @@ #define _LGPL_SOURCE #include "../command.hpp" +#include "../exception.hpp" #include "../utils.hpp" #include @@ -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 {}; } }(); diff --git a/src/bin/lttng/commands/stop.cpp b/src/bin/lttng/commands/stop.cpp index af42d98db..9d5533a21 100644 --- a/src/bin/lttng/commands/stop.cpp +++ b/src/bin/lttng/commands/stop.cpp @@ -7,6 +7,7 @@ #define _LGPL_SOURCE #include "../command.hpp" +#include "../exception.hpp" #include "../utils.hpp" #include @@ -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 {}; } }(); diff --git a/src/bin/lttng/utils.cpp b/src/bin/lttng/utils.cpp index 403ee0185..45fe256a5 100644 --- a/src/bin/lttng/utils.cpp +++ b/src/bin/lttng/utils.cpp @@ -8,6 +8,7 @@ #define _LGPL_SOURCE #include "command.hpp" #include "conf.hpp" +#include "exception.hpp" #include "utils.hpp" #include @@ -715,15 +716,14 @@ lttng::cli::session_list lttng::cli::list_sessions(const struct session_spec& sp lttng::make_unique_wrapper( 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( diff --git a/src/common/exception.hpp b/src/common/exception.hpp index e43094bb6..1880978e7 100644 --- a/src/common/exception.hpp +++ b/src/common/exception.hpp @@ -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) \ -- 2.34.1