Refactoring: introduce lttng_tracker_ids_serialize
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Thu, 21 Nov 2019 03:02:09 +0000 (22:02 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 20 Dec 2019 05:31:04 +0000 (00:31 -0500)
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Change-Id: I0d43b85241b7097ac722724d9fef0ca352422495
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/lttng/tracker-internal.h
src/bin/lttng-sessiond/client.c
src/common/tracker.c

index 0debf7523e137001d0c8b1d1775a835243374d31..a0a394479b5d684bd1536bbf92cdec61fb679c65 100644 (file)
@@ -18,8 +18,9 @@
 #ifndef LTTNG_TRACKER_INTERNAL_H
 #define LTTNG_TRACKER_INTERNAL_H
 
-#include <lttng/constant.h>
 #include <common/macros.h>
+#include <common/dynamic-buffer.h>
+#include <lttng/constant.h>
 #include <lttng/tracker.h>
 #include <stdbool.h>
 
@@ -71,4 +72,12 @@ LTTNG_HIDDEN
 struct lttng_tracker_id *lttng_tracker_ids_get_pointer_of_index(
                const struct lttng_tracker_ids *list, unsigned int index);
 
+/*
+ * Serialize a ids collection to a lttng_dynamic_buffer.
+ * Return LTTNG_OK on success, negative lttng error code on error.
+ */
+LTTNG_HIDDEN
+int lttng_tracker_ids_serialize(const struct lttng_tracker_ids *ids,
+               struct lttng_dynamic_buffer *buffer);
+
 #endif /* LTTNG_TRACKER_INTERNAL_H */
index ed0498ffa91c96a36cda70f60da9a275e63b968c..f0b0efd3c3d86d89fee6184087baa896e40d62c4 100644 (file)
@@ -28,6 +28,7 @@
 #include <lttng/event-internal.h>
 #include <lttng/session-internal.h>
 #include <lttng/session-descriptor-internal.h>
+#include <lttng/tracker-internal.h>
 
 #include "client.h"
 #include "lttng-sessiond.h"
@@ -1570,7 +1571,7 @@ error_add_context:
                struct lttcomm_tracker_command_header cmd_header;
                struct lttng_tracker_ids *ids = NULL;
                enum lttng_tracker_id_status status;
-               unsigned int nr_ids, i;
+               unsigned int nr_ids;
                struct lttng_dynamic_buffer buf;
 
                ret = cmd_list_tracker_ids(
@@ -1581,77 +1582,28 @@ error_add_context:
                        goto error;
                }
 
+               lttng_dynamic_buffer_init(&buf);
+
                status = lttng_tracker_ids_get_count(ids, &nr_ids);
                if (status != LTTNG_TRACKER_ID_STATUS_OK) {
-                       ret = LTTNG_ERR_INVALID;
+                       ret = -LTTNG_ERR_INVALID;
                        goto error_list_tracker;
                }
 
-               lttng_dynamic_buffer_init(&buf);
-               for (i = 0; i < nr_ids; i++) {
-                       const struct lttng_tracker_id *id;
-                       struct lttcomm_tracker_id_header id_hdr;
-                       size_t var_data_len = 0;
-                       enum lttng_tracker_id_status status;
-                       const char *string;
-                       int value;
-
-                       id = lttng_tracker_ids_get_at_index(ids, i);
-                       if (!id) {
-                               ret = LTTNG_ERR_INVALID;
-                               goto error_list_tracker;
-                       }
-
-                       memset(&id_hdr, 0, sizeof(id_hdr));
-                       id_hdr.type = lttng_tracker_id_get_type(id);
-                       switch (id_hdr.type) {
-                       case LTTNG_ID_ALL:
-                               break;
-                       case LTTNG_ID_VALUE:
-                               status = lttng_tracker_id_get_value(id, &value);
-                               id_hdr.u.value = value;
-                               if (status != LTTNG_TRACKER_ID_STATUS_OK) {
-                                       ret = LTTNG_ERR_INVALID;
-                                       goto error_list_tracker;
-                               }
-                               break;
-                       case LTTNG_ID_STRING:
-                               status = lttng_tracker_id_get_string(
-                                               id, &string);
-                               if (status != LTTNG_TRACKER_ID_STATUS_OK) {
-                                       ret = LTTNG_ERR_INVALID;
-                                       goto error_list_tracker;
-                               }
+               cmd_header.nb_tracker_id = nr_ids;
 
-                               id_hdr.u.var_data_len = var_data_len =
-                                               strlen(string) + 1;
-                               break;
-                       default:
-                               ret = LTTNG_ERR_INVALID;
-                               goto error_list_tracker;
-                       }
-                       ret = lttng_dynamic_buffer_append(
-                                       &buf, &id_hdr, sizeof(id_hdr));
-                       if (ret) {
-                               ret = LTTNG_ERR_NOMEM;
-                               goto error_list_tracker;
-                       }
-                       ret = lttng_dynamic_buffer_append(
-                                       &buf, string, var_data_len);
-                       if (ret) {
-                               ret = LTTNG_ERR_NOMEM;
-                               goto error_list_tracker;
-                       }
+               ret = lttng_tracker_ids_serialize(ids, &buf);
+               if (ret < 0) {
+                       goto error_list_tracker;
                }
 
-               cmd_header.nb_tracker_id = nr_ids;
                ret = setup_lttng_msg(cmd_ctx, buf.data, buf.size, &cmd_header,
                                sizeof(cmd_header));
        error_list_tracker:
                lttng_tracker_ids_destroy(ids);
                lttng_dynamic_buffer_reset(&buf);
                if (ret < 0) {
-                       goto setup_error;
+                       goto error;
                }
 
                ret = LTTNG_OK;
index afc253def71f990f77bb50923adde56bf481880e..ec908081cef84cd75a350cd5394f5672a0935262 100644 (file)
@@ -304,3 +304,74 @@ void lttng_tracker_ids_destroy(struct lttng_tracker_ids *ids)
        free(ids->id_array);
        free(ids);
 }
+
+int lttng_tracker_ids_serialize(const struct lttng_tracker_ids *ids,
+               struct lttng_dynamic_buffer *buffer)
+{
+       int ret;
+       int value;
+       const char *string;
+       unsigned int count;
+       enum lttng_tracker_id_status status;
+       const struct lttng_tracker_id *id;
+
+       status = lttng_tracker_ids_get_count(ids, &count);
+       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       for (unsigned int i = 0; i < count; i++) {
+               struct lttcomm_tracker_id_header id_hdr;
+               size_t var_data_len = 0;
+
+               id = lttng_tracker_ids_get_at_index(ids, i);
+               if (!id) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto error;
+               }
+
+               memset(&id_hdr, 0, sizeof(id_hdr));
+               id_hdr.type = lttng_tracker_id_get_type(id);
+               switch (id_hdr.type) {
+               case LTTNG_ID_ALL:
+                       break;
+               case LTTNG_ID_VALUE:
+                       status = lttng_tracker_id_get_value(id, &value);
+                       id_hdr.u.value = value;
+                       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       break;
+               case LTTNG_ID_STRING:
+                       status = lttng_tracker_id_get_string(
+                                       id, &string);
+                       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+
+                       id_hdr.u.var_data_len = var_data_len =
+                                       strlen(string) + 1;
+                       break;
+               default:
+                       ret = -LTTNG_ERR_INVALID;
+                       goto error;
+               }
+               ret = lttng_dynamic_buffer_append(
+                               buffer, &id_hdr, sizeof(id_hdr));
+               if (ret) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto error;
+               }
+               ret = lttng_dynamic_buffer_append(
+                               buffer, string, var_data_len);
+               if (ret) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto error;
+               }
+       }
+error:
+       return ret;
+}
This page took 0.028873 seconds and 4 git commands to generate.