lttng: enable-event: wrap mi_writer use in a unique_ptr
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 11 Dec 2023 18:48:42 +0000 (13:48 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 28 Feb 2024 15:39:45 +0000 (10:39 -0500)
To allow further clean-ups and simplify the use of STL containers, wrap
the manually managed mi_writer instance.

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

src/bin/lttng/commands/enable_events.cpp
src/bin/lttng/lttng.cpp

index 4f50083b74a7cd1a46e85342710a19a83916a715..e6f6274fb140c9b2b56c7db3f1b78703b09acc51 100644 (file)
@@ -8,6 +8,7 @@
 #define _LGPL_SOURCE
 #include <common/compat/getenv.hpp>
 #include <common/compat/string.hpp>
+#include <common/make-unique-wrapper.hpp>
 #include <common/sessiond-comm/sessiond-comm.hpp>
 #include <common/string-utils/string-utils.hpp>
 #include <common/utils.hpp>
 #endif
 
 namespace {
+void _mi_lttng_writer_deleter_func(mi_writer *writer)
+{
+       if (writer && mi_lttng_writer_destroy(writer)) {
+               LTTNG_THROW_ERROR("Failed to destroy mi_writer instance");
+       }
+}
+
+using mi_writer_uptr = std::unique_ptr<
+       mi_writer,
+       lttng::memory::create_deleter_class<mi_writer, _mi_lttng_writer_deleter_func>::deleter>;
 
 int opt_event_type;
 const char *opt_loglevel;
@@ -54,6 +65,9 @@ char *opt_channel_name;
 char *opt_filter;
 char *opt_exclude;
 
+struct lttng_handle *handle;
+mi_writer_uptr writer;
+
 #ifdef LTTNG_EMBED_HELP
 static const char help_msg[] =
 #include <lttng-enable-event.1.h>
@@ -75,9 +89,6 @@ enum {
        OPT_EXCLUDE,
 };
 
-struct lttng_handle *handle;
-struct mi_writer *writer;
-
 struct poptOption long_options[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
        { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
@@ -212,7 +223,7 @@ int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
                goto end;
        }
 
-       ret = mi_lttng_writer_open_element(writer, config_element_exclusions);
+       ret = mi_lttng_writer_open_element(writer.get(), config_element_exclusions);
        if (ret) {
                goto end;
        }
@@ -222,14 +233,14 @@ int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
                        (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions, i);
 
                ret = mi_lttng_writer_write_element_string(
-                       writer, config_element_exclusion, exclusion);
+                       writer.get(), config_element_exclusion, exclusion);
                if (ret) {
                        goto end;
                }
        }
 
        /* Close exclusions element */
-       ret = mi_lttng_writer_close_element(writer);
+       ret = mi_lttng_writer_close_element(writer.get());
 
 end:
        return ret;
@@ -492,7 +503,7 @@ int enable_events(char *session_name, char *event_list)
        /* Prepare Mi */
        if (lttng_opt_mi) {
                /* Open a events element */
-               ret = mi_lttng_writer_open_element(writer, config_element_events);
+               ret = mi_lttng_writer_open_element(writer.get(), config_element_events);
                if (ret) {
                        ret = CMD_ERROR;
                        goto error;
@@ -752,7 +763,7 @@ int enable_events(char *session_name, char *event_list)
                                ev->enabled = 0;
                                success = 0;
                        }
-                       ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
+                       ret = mi_lttng_event(writer.get(), ev, 1, handle->domain.type);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
@@ -767,14 +778,14 @@ int enable_events(char *session_name, char *event_list)
 
                        /* Success ? */
                        ret = mi_lttng_writer_write_element_bool(
-                               writer, mi_lttng_element_command_success, success);
+                               writer.get(), mi_lttng_element_command_success, success);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
                        }
 
                        /* Close event element */
-                       ret = mi_lttng_writer_close_element(writer);
+                       ret = mi_lttng_writer_close_element(writer.get());
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
@@ -1144,7 +1155,7 @@ int enable_events(char *session_name, char *event_list)
                                ev->enabled = 1;
                        }
 
-                       ret = mi_lttng_event(writer, ev, 1, handle->domain.type);
+                       ret = mi_lttng_event(writer.get(), ev, 1, handle->domain.type);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
@@ -1159,14 +1170,14 @@ int enable_events(char *session_name, char *event_list)
 
                        /* Success ? */
                        ret = mi_lttng_writer_write_element_bool(
-                               writer, mi_lttng_element_command_success, success);
+                               writer.get(), mi_lttng_element_command_success, success);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto end;
                        }
 
                        /* Close event element */
-                       ret = mi_lttng_writer_close_element(writer);
+                       ret = mi_lttng_writer_close_element(writer.get());
                        if (ret) {
                                ret = CMD_ERROR;
                                goto end;
@@ -1183,7 +1194,7 @@ end:
        /* Close Mi */
        if (lttng_opt_mi) {
                /* Close events element */
-               ret = mi_lttng_writer_close_element(writer);
+               ret = mi_lttng_writer_close_element(writer.get());
                if (ret) {
                        ret = CMD_ERROR;
                        goto error;
@@ -1340,21 +1351,22 @@ int cmd_enable_events(int argc, const char **argv)
 
        /* Mi check */
        if (lttng_opt_mi) {
-               writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
+               writer = mi_writer_uptr(mi_lttng_writer_create(fileno(stdout), lttng_opt_mi));
                if (!writer) {
                        ret = -LTTNG_ERR_NOMEM;
                        goto end;
                }
 
                /* Open command element */
-               ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_enable_event);
+               ret = mi_lttng_writer_command_open(writer.get(),
+                                                  mi_lttng_element_command_enable_event);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                /* Open output element */
-               ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output);
+               ret = mi_lttng_writer_open_element(writer.get(), mi_lttng_element_command_output);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
@@ -1405,21 +1417,21 @@ mi_closing:
        /* Mi closing */
        if (lttng_opt_mi) {
                /* Close  output element */
-               ret = mi_lttng_writer_close_element(writer);
+               ret = mi_lttng_writer_close_element(writer.get());
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                ret = mi_lttng_writer_write_element_bool(
-                       writer, mi_lttng_element_command_success, success);
+                       writer.get(), mi_lttng_element_command_success, success);
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
                }
 
                /* Command element close */
-               ret = mi_lttng_writer_command_close(writer);
+               ret = mi_lttng_writer_command_close(writer.get());
                if (ret) {
                        ret = CMD_ERROR;
                        goto end;
@@ -1427,12 +1439,6 @@ mi_closing:
        }
 
 end:
-       /* Mi clean-up */
-       if (writer && mi_lttng_writer_destroy(writer)) {
-               /* Preserve original error code */
-               ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
-       }
-
        if (opt_session_name == nullptr) {
                free(session_name);
        }
index b84fe3407069613d0c345956876d6dce73f76008..093a9809ccdfe512150f843356f58c0319a54ed6 100644 (file)
@@ -234,7 +234,13 @@ static int handle_command(int argc, char **argv)
        while (cmd->name != nullptr) {
                /* Find command */
                if (strcmp(argv[0], cmd->name) == 0) {
-                       ret = cmd->func(argc, (const char **) argv);
+                       try {
+                               ret = cmd->func(argc, (const char **) argv);
+                       } catch (const std::exception& e) {
+                               ERR_FMT("{}", e.what());
+                               ret = CMD_ERROR;
+                       }
+
                        goto end;
                }
                i++;
This page took 0.031056 seconds and 4 git commands to generate.