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:45:13 +0000 (10:45 -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/exception.cpp [new file with mode: 0644]
src/bin/lttng/exception.hpp [new file with mode: 0644]

diff --git a/src/bin/lttng/exception.cpp b/src/bin/lttng/exception.cpp
new file mode 100644 (file)
index 0000000..8995d5e
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2024 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#include "exception.hpp"
+
+#include <common/format.hpp>
+#include <common/utils.hpp>
+
+#include <sstream>
+
+lttng::cli::no_default_session_error::no_default_session_error(const char *file_name,
+                                                              const char *function_name,
+                                                              unsigned int line_number) :
+       runtime_error(lttng::format("No default session found in `{}/.lttngrc`",
+                                   utils_get_home_dir() ?: "LTTNG_HOME"),
+                     file_name,
+                     function_name,
+                     line_number)
+{
+}
diff --git a/src/bin/lttng/exception.hpp b/src/bin/lttng/exception.hpp
new file mode 100644 (file)
index 0000000..00448f9
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2024 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef LTTNG_CLI_EXCEPTION_H
+#define LTTNG_CLI_EXCEPTION_H
+
+#include <common/exception.hpp>
+
+#include <lttng/lttng-error.h>
+
+#include <stdexcept>
+#include <string>
+
+#define LTTNG_THROW_CLI_NO_DEFAULT_SESSION() \
+       throw lttng::cli::no_default_session_error(__FILE__, __func__, __LINE__)
+
+namespace lttng {
+namespace cli {
+class no_default_session_error : public runtime_error {
+public:
+       explicit no_default_session_error(const char *file_name,
+                                         const char *function_name,
+                                         unsigned int line_number);
+};
+} /* namespace cli */
+}; /* namespace lttng */
+
+#endif /* LTTNG_CLI_EXCEPTION_H */
This page took 0.025622 seconds and 4 git commands to generate.