From 989ddaeb7b0315a92d797d9f4fe05bf4ced0ccfe Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Mon, 25 Apr 2022 13:43:45 -0400 Subject: [PATCH] CLI: Add lttng_trace_format_descriptor to lttng_session_extended Signed-off-by: Jonathan Rajotte Change-Id: Ib51f94036df5137419797a1b1a484ac4c7f2fc59 --- include/lttng/session-internal.hpp | 8 +++ include/lttng/session.h | 18 +++++ src/bin/lttng-sessiond/cmd.cpp | 10 +++ src/common/session.cpp | 109 +++++++++++++++++++++++++++-- src/lib/lttng-ctl/liblttng-ctl.sym | 1 + 5 files changed, 139 insertions(+), 7 deletions(-) diff --git a/include/lttng/session-internal.hpp b/include/lttng/session-internal.hpp index b56ab214c..52edf4d3d 100644 --- a/include/lttng/session-internal.hpp +++ b/include/lttng/session-internal.hpp @@ -13,12 +13,18 @@ #include #include #include +#include struct lttng_session; struct lttng_payload_view; struct lttng_session_extended { LTTNG_OPTIONAL(uint64_t) creation_time; + /* + * This is done this way to satisfy the API memory restriction for lttng_session on listing. + * The caller must be able to simply call free on the return object and all memory be freed. + */ + struct lttng_buffer_view serialized_trace_format_descriptor; }; struct lttng_session_comm { @@ -27,12 +33,14 @@ struct lttng_session_comm { uint8_t enabled; uint32_t snapshot_mode; uint32_t live_timer_interval; + uint32_t trace_format_descriptor_len; /* lttng_session_extended data */ LTTNG_OPTIONAL(uint64_t) LTTNG_PACKED creation_time; /* * Dynamic payload: * - name[name_len] * - path[path_len] + * - trace_format_descriptor */ char payload[]; } LTTNG_PACKED; diff --git a/include/lttng/session.h b/include/lttng/session.h index 153a37b03..7ef03e54e 100644 --- a/include/lttng/session.h +++ b/include/lttng/session.h @@ -208,6 +208,24 @@ LTTNG_EXPORT extern enum lttng_error_code lttng_session_get_creation_time( LTTNG_EXPORT extern int lttng_set_session_shm_path(const char *session_name, const char *shm_path); +/* + * Get the trace format descriptor of an lttng_session object on the session daemon. + * + * This function must only be used with lttng_session objects returned + * by lttng_list_sessions() or lttng_session_create(). + * + * The returned object is a copy of the session trace_format_descriptor for + * backward compatibility purpose. + * + * The returned object must be freed by using lttng_trace_format_descriptor_delete. + * + * Returns LTTNG_OK on success. See lttng-error.h for the meaning of the other + * return codes. + */ +LTTNG_EXPORT extern enum lttng_error_code lttng_session_get_trace_format_descriptor( + const struct lttng_session *session, + struct lttng_trace_format_descriptor **p_lttng_trace_format_descriptor); + #ifdef __cplusplus } #endif diff --git a/src/bin/lttng-sessiond/cmd.cpp b/src/bin/lttng-sessiond/cmd.cpp index b519f9e70..214085c55 100644 --- a/src/bin/lttng-sessiond/cmd.cpp +++ b/src/bin/lttng-sessiond/cmd.cpp @@ -4055,10 +4055,13 @@ enum lttng_error_code cmd_list_lttng_sessions( cds_list_for_each_entry(session, &list->head, list) { struct lttng_session tmp_session = {}; struct lttng_session_extended tmp_extended = {}; + struct lttng_payload trace_format_buffer; + lttng_payload_init(&trace_format_buffer); tmp_session.extended.ptr = &tmp_extended; if (!session_get(session)) { + lttng_payload_reset(&trace_format_buffer); continue; } /* @@ -4067,6 +4070,7 @@ enum lttng_error_code cmd_list_lttng_sessions( if (!session_access_ok(session, uid) || session->destroyed) { session_put(session); + lttng_payload_reset(&trace_format_buffer); continue; } @@ -4088,13 +4092,19 @@ enum lttng_error_code cmd_list_lttng_sessions( continue; } + session->trace_format->serialize(&trace_format_buffer); + strncpy(tmp_session.name, session->name, NAME_MAX); tmp_session.name[NAME_MAX - 1] = '\0'; tmp_session.enabled = session->active; tmp_session.snapshot_mode = session->snapshot_mode; tmp_session.live_timer_interval = session->live_timer; LTTNG_OPTIONAL_SET(&tmp_extended.creation_time, (uint64_t) session->creation_time); + tmp_extended.serialized_trace_format_descriptor = + lttng_buffer_view_from_dynamic_buffer(&trace_format_buffer.buffer, + 0, trace_format_buffer.buffer.size); ret = lttng_session_serialize(&tmp_session, reply_payload); + lttng_payload_reset(&trace_format_buffer); if (ret) { ret_code = LTTNG_ERR_FATAL; goto error; diff --git a/src/common/session.cpp b/src/common/session.cpp index 0af62f7a7..dbd77fad8 100644 --- a/src/common/session.cpp +++ b/src/common/session.cpp @@ -63,6 +63,8 @@ int lttng_session_serialize(const struct lttng_session *session, lttng_payload * session_comm.enabled = (uint8_t) session->enabled; session_comm.snapshot_mode = session->snapshot_mode; session_comm.live_timer_interval = session->live_timer_interval; + session_comm.trace_format_descriptor_len = + extended->serialized_trace_format_descriptor.size; if (extended->creation_time.is_set) { LTTNG_OPTIONAL_SET(&session_comm.creation_time, LTTNG_OPTIONAL_GET(extended->creation_time)); @@ -88,6 +90,14 @@ int lttng_session_serialize(const struct lttng_session *session, lttng_payload * goto end; } + /* Serialized trace_format_descriptor */ + ret = lttng_dynamic_buffer_append(&payload->buffer, + extended->serialized_trace_format_descriptor.data, + extended->serialized_trace_format_descriptor.size); + if (ret) { + goto end; + } + end: return ret; } @@ -117,8 +127,9 @@ ssize_t lttng_session_create_from_payload( offset += sizeof(*session_comm); } - local_session = zmalloc( - sizeof(struct lttng_session) + sizeof(struct lttng_session_extended)); + local_session = zmalloc(sizeof(struct lttng_session) + + sizeof(struct lttng_session_extended) + + (size_t) session_comm->trace_format_descriptor_len); if (local_session == nullptr) { ret = -1; goto end; @@ -131,6 +142,12 @@ ssize_t lttng_session_create_from_payload( goto end; } + /* Set the view for serialized_trace_format_descriptor */ + local_extended->serialized_trace_format_descriptor = + lttng_buffer_view_init(reinterpret_cast(local_extended), + sizeof(struct lttng_session_extended), + (size_t) session_comm->trace_format_descriptor_len); + local_session->extended.ptr = local_extended; local_session->enabled = session_comm->enabled; @@ -192,6 +209,20 @@ ssize_t lttng_session_create_from_payload( offset += session_comm->path_len; } + { + const struct lttng_buffer_view trace_format_view = lttng_buffer_view_from_view( + &view->buffer, offset, session_comm->trace_format_descriptor_len); + + if (!lttng_buffer_view_is_valid(&trace_format_view)) { + ret = -1; + goto end; + } + + memcpy((void *) local_extended->serialized_trace_format_descriptor.data, + trace_format_view.data, trace_format_view.size); + + offset += session_comm->trace_format_descriptor_len; + } /* Transfer ownership to the caller. */ *out_session = local_session; local_session = nullptr; @@ -206,7 +237,7 @@ static enum lttng_error_code compute_flattened_size( struct lttng_dynamic_pointer_array *sessions, size_t *size) { enum lttng_error_code ret_code; - size_t storage_req, session_count; + size_t storage_req, session_count, i; assert(size); assert(sessions); @@ -219,6 +250,15 @@ static enum lttng_error_code compute_flattened_size( /* The struct lttng_session_extended */ storage_req += session_count * sizeof(struct lttng_session_extended); + for (i = 0; i < session_count; i++) { + const struct session_list_element *element = (const struct session_list_element *) + lttng_dynamic_pointer_array_get_pointer(sessions, i); + + storage_req += ((const struct lttng_session_extended *) (element->session->extended + .ptr)) + ->serialized_trace_format_descriptor.size; + } + *size = storage_req; ret_code = LTTNG_OK; @@ -282,20 +322,35 @@ static enum lttng_error_code flatten_lttng_sessions(struct lttng_dynamic_pointer struct lttng_session_extended *session_extended = (struct lttng_session_extended *) (local_flattened_sessions.data + local_flattened_sessions.size); + size_t currentSize; assert(element); /* Insert struct lttng_session_extended. */ - ret = lttng_dynamic_buffer_set_size(&local_flattened_sessions, - local_flattened_sessions.size + sizeof(*session_extended)); + ret = lttng_dynamic_buffer_append(&local_flattened_sessions, + element->session->extended.ptr, sizeof(*session_extended)); if (ret) { ret_code = LTTNG_ERR_NOMEM; goto end; } + /* Update the location of the extended protion in the base lttng_session object */ session->extended.ptr = session_extended; - memcpy(session_extended, element->session->extended.ptr, - sizeof(struct lttng_session_extended)); + /* Insert serialize_trace_format_descriptor */ + currentSize = local_flattened_sessions.size; + ret = lttng_dynamic_buffer_append(&local_flattened_sessions, + session_extended->serialized_trace_format_descriptor.data, + session_extended->serialized_trace_format_descriptor.size); + if (ret) { + ret_code = LTTNG_ERR_NOMEM; + goto end; + } + + /* Update the view to point at the flattened memory location for + * trace_format_descriptor */ + session_extended->serialized_trace_format_descriptor = lttng_buffer_view_init( + local_flattened_sessions.data, currentSize, + session_extended->serialized_trace_format_descriptor.size); } /* Don't reset local_flattened_sessions buffer as we return its content. */ @@ -390,3 +445,43 @@ end: lttng_dynamic_pointer_array_reset(&local_sessions); return ret; } +enum lttng_error_code lttng_session_get_trace_format_descriptor(const struct lttng_session *session, + struct lttng_trace_format_descriptor **p_lttng_trace_format_descriptor) +{ + lttng_error_code ret_code; + const struct lttng_session_extended *extended = nullptr; + struct lttng_trace_format_descriptor *local_descriptor = nullptr; + ssize_t deserialiazed = 0; + lttng::trace_format_descriptor::uptr p_trace_format; + + if (!session || !session->extended.ptr) { + ret_code = LTTNG_ERR_INVALID; + goto error; + } + + extended = (struct lttng_session_extended *) session->extended.ptr; + + { + struct lttng_payload_view view = lttng_payload_view_from_buffer_view( + &extended->serialized_trace_format_descriptor, 0, + extended->serialized_trace_format_descriptor.size); + deserialiazed = lttng::trace_format_descriptor::create_from_payload( + &view, p_trace_format); + if (deserialiazed < 0 || + deserialiazed != + extended->serialized_trace_format_descriptor.size) { + ret_code = LTTNG_ERR_INVALID; + goto error; + } + } + + local_descriptor = reinterpret_cast( + p_trace_format.release()); + + *p_lttng_trace_format_descriptor = local_descriptor; + local_descriptor = nullptr; + ret_code = LTTNG_OK; +error: + lttng_trace_format_descriptor_destroy(local_descriptor); + return ret_code; +} diff --git a/src/lib/lttng-ctl/liblttng-ctl.sym b/src/lib/lttng-ctl/liblttng-ctl.sym index c0092e418..1866c4801 100644 --- a/src/lib/lttng-ctl/liblttng-ctl.sym +++ b/src/lib/lttng-ctl/liblttng-ctl.sym @@ -468,6 +468,7 @@ lttng_session_descriptor_snapshot_create lttng_session_descriptor_snapshot_local_create lttng_session_descriptor_snapshot_network_create lttng_session_get_creation_time +lttng_session_get_trace_format_descriptor lttng_session_get_tracker_handle lttng_session_list_rotation_schedules lttng_session_remove_rotation_schedule -- 2.39.5