lttng: enable-event: replace raw session string by std::string
[lttng-tools.git] / src / bin / lttng / commands / enable_events.cpp
index ef58bfa75ff0f752fc57a63c32168c1ea5faac3f..f95a006102fc23d13922f395fd74e9e82c447ad5 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>
 #include <ctype.h>
 #include <inttypes.h>
 #include <popt.h>
+#include <sstream>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <vector>
 
 /* Mi dependancy */
 #include "../command.hpp"
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
 #endif
 
-static int opt_event_type;
-static const char *opt_loglevel;
-static int opt_loglevel_type;
-static int opt_kernel;
-static char *opt_session_name;
-static int opt_userspace;
-static int opt_jul;
-static int opt_log4j;
-static int opt_python;
-static int opt_enable_all;
-static char *opt_probe;
-static char *opt_userspace_probe;
-static char *opt_function;
-static char *opt_channel_name;
-static char *opt_filter;
-static char *opt_exclude;
+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>;
+using event_rule_patterns = std::vector<std::string>;
+
+int opt_event_type;
+const char *opt_loglevel;
+int opt_loglevel_type;
+int opt_kernel;
+char *opt_session_name;
+int opt_userspace;
+int opt_jul;
+int opt_log4j;
+int opt_python;
+int opt_enable_all;
+char *opt_probe;
+char *opt_userspace_probe;
+char *opt_function;
+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[] =
@@ -73,10 +93,7 @@ enum {
        OPT_EXCLUDE,
 };
 
-static struct lttng_handle *handle;
-static struct mi_writer *writer;
-
-static struct poptOption long_options[] = {
+struct poptOption long_options[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
        { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr },
        { "session", 's', POPT_ARG_STRING, &opt_session_name, 0, nullptr, nullptr },
@@ -109,7 +126,7 @@ static struct poptOption long_options[] = {
 /*
  * Parse probe options.
  */
-static int parse_probe_opts(struct lttng_event *ev, char *opt)
+int parse_probe_opts(struct lttng_event *ev, char *opt)
 {
        int ret = CMD_SUCCESS;
        int match;
@@ -184,12 +201,12 @@ end:
        return ret;
 }
 
-static const char *print_channel_name(const char *name)
+const char *print_channel_name(const char *name)
 {
        return name ?: DEFAULT_CHANNEL_NAME;
 }
 
-static const char *print_raw_channel_name(const char *name)
+const char *print_raw_channel_name(const char *name)
 {
        return name ?: "<default>";
 }
@@ -197,7 +214,7 @@ static const char *print_raw_channel_name(const char *name)
 /*
  * Mi print exlcusion list
  */
-static int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
+int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusions)
 {
        int ret;
        size_t i;
@@ -210,7 +227,7 @@ static int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusio
                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;
        }
@@ -220,14 +237,14 @@ static int mi_print_exclusion(const struct lttng_dynamic_pointer_array *exclusio
                        (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;
@@ -236,7 +253,7 @@ end:
 /*
  * Return allocated string for pretty-printing exclusion names.
  */
-static char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions)
+char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions)
 {
        int length = 0;
        size_t i;
@@ -278,11 +295,11 @@ static char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusio
        return ret;
 }
 
-static int check_exclusion_subsets(const char *event_name, const char *exclusion)
+int check_exclusion_subsets(const char *pattern, const char *exclusion)
 {
        bool warn = false;
        int ret = 0;
-       const char *e = event_name;
+       const char *e = pattern;
        const char *x = exclusion;
 
        /* Scan both the excluder and the event letter by letter */
@@ -300,10 +317,7 @@ static int check_exclusion_subsets(const char *event_name, const char *exclusion
 
                if (*x == '*') {
                        /* Event is a subset of the excluder */
-                       ERR("Event %s: %s excludes all events from %s",
-                           event_name,
-                           exclusion,
-                           event_name);
+                       ERR("Event %s: %s excludes all events from %s", pattern, exclusion, pattern);
                        goto error;
                }
 
@@ -333,63 +347,17 @@ error:
 end:
        if (warn) {
                WARN("Event %s: %s does not exclude any events from %s",
-                    event_name,
+                    pattern,
                     exclusion,
-                    event_name);
-       }
-
-       return ret;
-}
-
-int validate_exclusion_list(const char *event_name,
-                           const struct lttng_dynamic_pointer_array *exclusions)
-{
-       int ret;
-
-       /* Event name must be a valid globbing pattern to allow exclusions. */
-       if (!strutils_is_star_glob_pattern(event_name)) {
-               ERR("Event %s: Exclusions can only be used with a globbing pattern", event_name);
-               goto error;
-       }
-
-       /*
-        * If the event name is a star-at-end only globbing pattern,
-        * then we can validate the individual exclusions. Otherwise
-        * all exclusions are passed to the session daemon.
-        */
-       if (strutils_is_star_at_the_end_only_glob_pattern(event_name)) {
-               size_t i, num_exclusions;
-
-               num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
-
-               for (i = 0; i < num_exclusions; i++) {
-                       const char *exclusion =
-                               (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions,
-                                                                                      i);
-
-                       if (!strutils_is_star_glob_pattern(exclusion) ||
-                           strutils_is_star_at_the_end_only_glob_pattern(exclusion)) {
-                               ret = check_exclusion_subsets(event_name, exclusion);
-                               if (ret) {
-                                       goto error;
-                               }
-                       }
-               }
+                    pattern);
        }
 
-       ret = 0;
-       goto end;
-
-error:
-       ret = -1;
-
-end:
        return ret;
 }
 
-static int create_exclusion_list_and_validate(const char *event_name,
-                                             const char *exclusions_arg,
-                                             struct lttng_dynamic_pointer_array *exclusions)
+int create_exclusion_list_and_validate(const char *pattern,
+                                      const char *exclusions_arg,
+                                      struct lttng_dynamic_pointer_array *exclusions)
 {
        int ret = 0;
 
@@ -399,7 +367,7 @@ static int create_exclusion_list_and_validate(const char *event_name,
                goto error;
        }
 
-       if (validate_exclusion_list(event_name, exclusions) != 0) {
+       if (validate_exclusion_list(pattern, exclusions) != 0) {
                goto error;
        }
 
@@ -413,8 +381,8 @@ end:
        return ret;
 }
 
-static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array *exclusions,
-                                             int *warn)
+void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer_array *exclusions,
+                                      int *warn)
 {
        size_t i;
        const size_t num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
@@ -434,11 +402,11 @@ static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer
  * Enabling event using the lttng API.
  * Note: in case of error only the last error code will be return.
  */
-static int enable_events(char *session_name, char *event_list)
+int enable_events(const std::string& session_name, const event_rule_patterns& patterns)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
        int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
-       char *event_name, *channel_name = nullptr;
+       char *channel_name = nullptr;
        struct lttng_event *ev;
        struct lttng_domain dom = {};
        struct lttng_dynamic_pointer_array exclusions;
@@ -527,7 +495,7 @@ static int enable_events(char *session_name, char *event_list)
 
        channel_name = opt_channel_name;
 
-       handle = lttng_create_handle(session_name, &dom);
+       handle = lttng_create_handle(session_name.c_str(), &dom);
        if (handle == nullptr) {
                ret = -1;
                goto error;
@@ -536,7 +504,7 @@ static 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;
@@ -628,7 +596,7 @@ static int enable_events(char *session_name, char *event_list)
                                case LTTNG_ERR_KERN_EVENT_EXIST:
                                        WARN("Kernel events already enabled (channel %s, session %s)",
                                             print_channel_name(channel_name),
-                                            session_name);
+                                            session_name.c_str());
                                        warn = 1;
                                        break;
                                case LTTNG_ERR_TRACE_ALREADY_STARTED:
@@ -638,7 +606,7 @@ static int enable_events(char *session_name, char *event_list)
                                        ERR("Events: %s (channel %s, session %s)",
                                            msg,
                                            print_channel_name(channel_name),
-                                           session_name);
+                                           session_name.c_str());
                                        error = 1;
                                        break;
                                }
@@ -648,7 +616,7 @@ static int enable_events(char *session_name, char *event_list)
                                            ret == -LTTNG_ERR_NEED_CHANNEL_NAME ?
                                                    print_raw_channel_name(channel_name) :
                                                    print_channel_name(channel_name),
-                                           session_name);
+                                           session_name.c_str());
                                        error = 1;
                                        break;
                                }
@@ -746,7 +714,7 @@ static int enable_events(char *session_name, char *event_list)
                                        WARN("Filter on all events is already enabled"
                                             " (channel %s, session %s)",
                                             print_channel_name(channel_name),
-                                            session_name);
+                                            session_name.c_str());
                                        warn = 1;
                                        break;
                                case LTTNG_ERR_TRACE_ALREADY_STARTED:
@@ -756,7 +724,7 @@ static int enable_events(char *session_name, char *event_list)
                                        ERR("All events: %s (channel %s, session %s, filter \'%s\')",
                                            msg,
                                            print_channel_name(channel_name),
-                                           session_name,
+                                           session_name.c_str(),
                                            opt_filter);
                                        error = 1;
                                        break;
@@ -767,7 +735,7 @@ static int enable_events(char *session_name, char *event_list)
                                            command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ?
                                                    print_raw_channel_name(channel_name) :
                                                    print_channel_name(channel_name),
-                                           session_name,
+                                           session_name.c_str(),
                                            opt_filter);
                                        error = 1;
                                        break;
@@ -796,7 +764,7 @@ static 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;
@@ -811,14 +779,14 @@ static 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;
@@ -829,23 +797,22 @@ static int enable_events(char *session_name, char *event_list)
        }
 
        /* Strip event list */
-       event_name = strtok(event_list, ",");
-       while (event_name != nullptr) {
+       for (const auto& pattern : patterns) {
                /* Copy name and type of the event */
-               strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
+               strncpy(ev->name, pattern.c_str(), LTTNG_SYMBOL_NAME_LEN);
                ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                ev->type = (lttng_event_type) opt_event_type;
 
                /* Kernel tracer action */
                if (opt_kernel) {
-                       DBG("Enabling kernel event %s for channel %s",
-                           event_name,
-                           print_channel_name(channel_name));
+                       DBG_FMT("Enabling kernel event: pattern=`{}`, channel_name=`{}`",
+                               pattern,
+                               print_channel_name(channel_name));
 
                        switch (opt_event_type) {
                        case LTTNG_EVENT_ALL: /* Enable tracepoints and syscalls */
                                /* If event name differs from *, select tracepoint. */
-                               if (strcmp(ev->name, "*")) {
+                               if (strcmp(ev->name, "*") != 0) {
                                        ev->type = LTTNG_EVENT_TRACEPOINT;
                                }
                                break;
@@ -910,7 +877,7 @@ static int enable_events(char *session_name, char *event_list)
                        ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
                } else if (opt_userspace) { /* User-space tracer action */
                        DBG("Enabling UST event %s for channel %s, loglevel %s",
-                           event_name,
+                           pattern.c_str(),
                            print_channel_name(channel_name),
                            opt_loglevel ?: "<all>");
 
@@ -920,7 +887,7 @@ static int enable_events(char *session_name, char *event_list)
                        case LTTNG_EVENT_TRACEPOINT:
                                /* Copy name and type of the event */
                                ev->type = LTTNG_EVENT_TRACEPOINT;
-                               strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
+                               strncpy(ev->name, pattern.c_str(), LTTNG_SYMBOL_NAME_LEN);
                                ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                                break;
                        case LTTNG_EVENT_PROBE:
@@ -944,7 +911,7 @@ static int enable_events(char *session_name, char *event_list)
                                /* Free previously allocated items. */
                                lttng_dynamic_pointer_array_reset(&exclusions);
                                ret = create_exclusion_list_and_validate(
-                                       event_name, opt_exclude, &exclusions);
+                                       pattern.c_str(), opt_exclude, &exclusions);
                                if (ret) {
                                        ret = CMD_ERROR;
                                        goto error;
@@ -1017,7 +984,7 @@ static int enable_events(char *session_name, char *event_list)
                                }
                        }
                        ev->type = LTTNG_EVENT_TRACEPOINT;
-                       strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
+                       strncpy(ev->name, pattern.c_str(), LTTNG_SYMBOL_NAME_LEN);
                        ev->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                } else {
                        abort();
@@ -1044,10 +1011,10 @@ static int enable_events(char *session_name, char *event_list)
                                switch (-command_ret) {
                                case LTTNG_ERR_KERN_EVENT_EXIST:
                                        WARN("Kernel event %s%s already enabled (channel %s, session %s)",
-                                            event_name,
+                                            pattern.c_str(),
                                             exclusion_string,
                                             print_channel_name(channel_name),
-                                            session_name);
+                                            session_name.c_str());
                                        warn = 1;
                                        break;
                                case LTTNG_ERR_TRACE_ALREADY_STARTED:
@@ -1055,30 +1022,30 @@ static int enable_events(char *session_name, char *event_list)
                                        const char *msg =
                                                "The command tried to enable an event in a new domain for a session that has already been started once.";
                                        ERR("Event %s%s: %s (channel %s, session %s)",
-                                           event_name,
+                                           pattern.c_str(),
                                            exclusion_string,
                                            msg,
                                            print_channel_name(channel_name),
-                                           session_name);
+                                           session_name.c_str());
                                        error = 1;
                                        break;
                                }
                                case LTTNG_ERR_SDT_PROBE_SEMAPHORE:
                                        ERR("SDT probes %s guarded by semaphores are not supported (channel %s, session %s)",
-                                           event_name,
+                                           pattern.c_str(),
                                            print_channel_name(channel_name),
-                                           session_name);
+                                           session_name.c_str());
                                        error = 1;
                                        break;
                                default:
                                        ERR("Event %s%s: %s (channel %s, session %s)",
-                                           event_name,
+                                           pattern.c_str(),
                                            exclusion_string,
                                            lttng_strerror(command_ret),
                                            command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ?
                                                    print_raw_channel_name(channel_name) :
                                                    print_channel_name(channel_name),
-                                           session_name);
+                                           session_name.c_str());
                                        error = 1;
                                        break;
                                }
@@ -1089,7 +1056,7 @@ static int enable_events(char *session_name, char *event_list)
                                case LTTNG_DOMAIN_UST:
                                        MSG("%s event %s%s created in channel %s",
                                            lttng_domain_type_str(dom.type),
-                                           event_name,
+                                           pattern.c_str(),
                                            exclusion_string,
                                            print_channel_name(channel_name));
                                        break;
@@ -1102,7 +1069,7 @@ static int enable_events(char *session_name, char *event_list)
                                         */
                                        MSG("%s event %s%s enabled",
                                            lttng_domain_type_str(dom.type),
-                                           event_name,
+                                           pattern.c_str(),
                                            exclusion_string);
                                        break;
                                default:
@@ -1136,10 +1103,10 @@ static int enable_events(char *session_name, char *event_list)
                                case LTTNG_ERR_FILTER_EXIST:
                                        WARN("Filter on event %s%s is already enabled"
                                             " (channel %s, session %s)",
-                                            event_name,
+                                            pattern.c_str(),
                                             exclusion_string,
                                             print_channel_name(channel_name),
-                                            session_name);
+                                            session_name.c_str());
                                        warn = 1;
                                        break;
                                case LTTNG_ERR_TRACE_ALREADY_STARTED:
@@ -1151,7 +1118,7 @@ static int enable_events(char *session_name, char *event_list)
                                            exclusion_string,
                                            msg,
                                            print_channel_name(channel_name),
-                                           session_name,
+                                           session_name.c_str(),
                                            opt_filter);
                                        error = 1;
                                        break;
@@ -1164,7 +1131,7 @@ static int enable_events(char *session_name, char *event_list)
                                            command_ret == -LTTNG_ERR_NEED_CHANNEL_NAME ?
                                                    print_raw_channel_name(channel_name) :
                                                    print_channel_name(channel_name),
-                                           session_name,
+                                           session_name.c_str(),
                                            opt_filter);
                                        error = 1;
                                        break;
@@ -1173,7 +1140,7 @@ static int enable_events(char *session_name, char *event_list)
 
                        } else {
                                MSG("Event %s%s: Filter '%s' successfully set",
-                                   event_name,
+                                   pattern.c_str(),
                                    exclusion_string,
                                    opt_filter);
                        }
@@ -1188,7 +1155,7 @@ static 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;
@@ -1203,22 +1170,20 @@ static 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;
                        }
                }
 
-               /* Next event */
-               event_name = strtok(nullptr, ",");
                /* Reset warn, error and success */
                success = 1;
        }
@@ -1227,7 +1192,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;
@@ -1253,26 +1218,80 @@ error:
        return ret;
 }
 
+void _poptContextFree_deleter_func(poptContext ctx)
+{
+       poptFreeContext(ctx);
+}
+
+} /* namespace */
+
+int validate_exclusion_list(const char *pattern,
+                           const struct lttng_dynamic_pointer_array *exclusions)
+{
+       int ret;
+
+       /* Event name must be a valid globbing pattern to allow exclusions. */
+       if (!strutils_is_star_glob_pattern(pattern)) {
+               ERR("Event %s: Exclusions can only be used with a globbing pattern", pattern);
+               goto error;
+       }
+
+       /*
+        * If the event name is a star-at-end only globbing pattern,
+        * then we can validate the individual exclusions. Otherwise
+        * all exclusions are passed to the session daemon.
+        */
+       if (strutils_is_star_at_the_end_only_glob_pattern(pattern)) {
+               size_t i, num_exclusions;
+
+               num_exclusions = lttng_dynamic_pointer_array_get_count(exclusions);
+
+               for (i = 0; i < num_exclusions; i++) {
+                       const char *exclusion =
+                               (const char *) lttng_dynamic_pointer_array_get_pointer(exclusions,
+                                                                                      i);
+
+                       if (!strutils_is_star_glob_pattern(exclusion) ||
+                           strutils_is_star_at_the_end_only_glob_pattern(exclusion)) {
+                               ret = check_exclusion_subsets(pattern, exclusion);
+                               if (ret) {
+                                       goto error;
+                               }
+                       }
+               }
+       }
+
+       ret = 0;
+       goto end;
+
+error:
+       ret = -1;
+
+end:
+       return ret;
+}
+
 /*
  * Add event to trace session
  */
 int cmd_enable_events(int argc, const char **argv)
 {
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
-       static poptContext pc;
-       char *session_name = nullptr;
-       char *event_list = nullptr;
+       std::string session_name;
        const char *arg_event_list = nullptr;
        const char *leftover = nullptr;
        int event_type = -1;
+       event_rule_patterns patterns;
+       std::stringstream event_list_arg_stream(arg_event_list);
 
-       pc = poptGetContext(nullptr, argc, argv, long_options, 0);
-       poptReadDefaultConfig(pc, 0);
+       auto pc = lttng::make_unique_wrapper<poptContext_s, _poptContextFree_deleter_func>(
+               poptGetContext(nullptr, argc, argv, long_options, 0));
+       poptReadDefaultConfig(pc.get(), 0);
 
        /* Default event type */
        opt_event_type = LTTNG_EVENT_ALL;
 
-       while ((opt = poptGetNextOpt(pc)) != -1) {
+       while ((opt = poptGetNextOpt(pc.get())) != -1) {
                switch (opt) {
                case OPT_HELP:
                        SHOW_HELP();
@@ -1297,11 +1316,11 @@ int cmd_enable_events(int argc, const char **argv)
                        break;
                case OPT_LOGLEVEL:
                        opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
-                       opt_loglevel = poptGetOptArg(pc);
+                       opt_loglevel = poptGetOptArg(pc.get());
                        break;
                case OPT_LOGLEVEL_ONLY:
                        opt_loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
-                       opt_loglevel = poptGetOptArg(pc);
+                       opt_loglevel = poptGetOptArg(pc.get());
                        break;
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
@@ -1336,44 +1355,40 @@ 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;
                }
        }
 
-       arg_event_list = poptGetArg(pc);
+       arg_event_list = poptGetArg(pc.get());
        if (arg_event_list == nullptr && opt_enable_all == 0) {
                ERR("Missing event name(s).");
                ret = CMD_ERROR;
                goto end;
        }
 
-       if (opt_enable_all == 0) {
-               event_list = strdup(arg_event_list);
-               if (event_list == nullptr) {
-                       PERROR("Failed to copy event name(s)");
-                       ret = CMD_ERROR;
-                       goto end;
-               }
+       for (std::string line; std::getline(event_list_arg_stream, line, ',');) {
+               patterns.emplace_back(std::move(line));
        }
 
-       leftover = poptGetArg(pc);
+       leftover = poptGetArg(pc.get());
        if (leftover) {
                ERR("Unknown argument: %s", leftover);
                ret = CMD_ERROR;
@@ -1381,17 +1396,21 @@ int cmd_enable_events(int argc, const char **argv)
        }
 
        if (!opt_session_name) {
-               session_name = get_session_name();
-               if (session_name == nullptr) {
+               const auto rc_file_session_name =
+                       lttng::make_unique_wrapper<char, lttng::free>(get_session_name());
+
+               if (!rc_file_session_name) {
                        command_ret = CMD_ERROR;
                        success = 0;
                        goto mi_closing;
                }
+
+               session_name = rc_file_session_name.get();
        } else {
                session_name = opt_session_name;
        }
 
-       command_ret = enable_events(session_name, event_list);
+       command_ret = enable_events(session_name, patterns);
        if (command_ret) {
                success = 0;
                goto mi_closing;
@@ -1401,21 +1420,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;
@@ -1423,21 +1442,7 @@ 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);
-       }
-
-       free(event_list);
-
        /* Overwrite ret if an error occurred in enable_events */
        ret = command_ret ? command_ret : ret;
-
-       poptFreeContext(pc);
        return ret;
 }
This page took 0.035618 seconds and 4 git commands to generate.