From f19f5c968f63a83a83c47390020e6ff0dd7c85aa Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 20 Nov 2019 22:02:09 -0500 Subject: [PATCH] Refactoring: introduce lttng_tracker_ids_serialize MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jonathan Rajotte Change-Id: I0d43b85241b7097ac722724d9fef0ca352422495 Signed-off-by: Jérémie Galarneau --- include/lttng/tracker-internal.h | 11 ++++- src/bin/lttng-sessiond/client.c | 68 +++++------------------------- src/common/tracker.c | 71 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 59 deletions(-) diff --git a/include/lttng/tracker-internal.h b/include/lttng/tracker-internal.h index 0debf7523..a0a394479 100644 --- a/include/lttng/tracker-internal.h +++ b/include/lttng/tracker-internal.h @@ -18,8 +18,9 @@ #ifndef LTTNG_TRACKER_INTERNAL_H #define LTTNG_TRACKER_INTERNAL_H -#include #include +#include +#include #include #include @@ -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 */ diff --git a/src/bin/lttng-sessiond/client.c b/src/bin/lttng-sessiond/client.c index ed0498ffa..f0b0efd3c 100644 --- a/src/bin/lttng-sessiond/client.c +++ b/src/bin/lttng-sessiond/client.c @@ -28,6 +28,7 @@ #include #include #include +#include #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; diff --git a/src/common/tracker.c b/src/common/tracker.c index afc253def..ec908081c 100644 --- a/src/common/tracker.c +++ b/src/common/tracker.c @@ -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; +} -- 2.34.1