From b52d6eacd08dcc766fa075d4f9e856b059d0e1ae Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 13 Apr 2022 13:49:00 -0400 Subject: [PATCH] Add trace format descriptor to session descriptor Default to ctf1 for now. Signed-off-by: Jonathan Rajotte Change-Id: I6acdfd43995a52b9160429378e614497a3d87f4c --- include/lttng/session-descriptor-internal.hpp | 5 +- include/lttng/session-descriptor.h | 1 + src/bin/lttng-sessiond/client.cpp | 10 +- src/common/session-descriptor.cpp | 124 +++++++++++++++--- src/lib/lttng-ctl/lttng-ctl.cpp | 17 ++- 5 files changed, 121 insertions(+), 36 deletions(-) diff --git a/include/lttng/session-descriptor-internal.hpp b/include/lttng/session-descriptor-internal.hpp index c132f5600..ab61d91f4 100644 --- a/include/lttng/session-descriptor-internal.hpp +++ b/include/lttng/session-descriptor-internal.hpp @@ -15,6 +15,8 @@ #include #include +struct lttng_payload; + /* Note that these enums are used as part of the lttnctl protocol. */ enum lttng_session_descriptor_type { LTTNG_SESSION_DESCRIPTOR_TYPE_UNKNOWN = -1, @@ -38,8 +40,7 @@ ssize_t lttng_session_descriptor_create_from_buffer( struct lttng_session_descriptor **descriptor); int lttng_session_descriptor_serialize( - const struct lttng_session_descriptor *descriptor, - struct lttng_dynamic_buffer *buffer); + const struct lttng_session_descriptor *descriptor, lttng_payload *payload); enum lttng_session_descriptor_type lttng_session_descriptor_get_type( diff --git a/include/lttng/session-descriptor.h b/include/lttng/session-descriptor.h index 5db04be7e..cbbf10d9e 100644 --- a/include/lttng/session-descriptor.h +++ b/include/lttng/session-descriptor.h @@ -15,6 +15,7 @@ extern "C" { #endif struct lttng_session_descriptor; +struct lttng_trace_format_descriptor; /* * Session descriptor API. diff --git a/src/bin/lttng-sessiond/client.cpp b/src/bin/lttng-sessiond/client.cpp index 3ec4c0d0a..a153228fc 100644 --- a/src/bin/lttng-sessiond/client.cpp +++ b/src/bin/lttng-sessiond/client.cpp @@ -2017,10 +2017,10 @@ skip_domain: } case LTTNG_CREATE_SESSION_EXT: { - struct lttng_dynamic_buffer payload; + struct lttng_payload payload; struct lttng_session_descriptor *return_descriptor = NULL; - lttng_dynamic_buffer_init(&payload); + lttng_payload_init(&payload); ret = cmd_create_session(cmd_ctx, *sock, &return_descriptor); if (ret != LTTNG_OK) { goto error; @@ -2034,14 +2034,14 @@ skip_domain: ret = LTTNG_ERR_NOMEM; goto error; } - ret = setup_lttng_msg_no_cmd_header(cmd_ctx, payload.data, - payload.size); + ret = setup_lttng_msg_no_cmd_header( + cmd_ctx, payload.buffer.data, payload.buffer.size); if (ret) { lttng_session_descriptor_destroy(return_descriptor); ret = LTTNG_ERR_NOMEM; goto error; } - lttng_dynamic_buffer_reset(&payload); + lttng_payload_reset(&payload); lttng_session_descriptor_destroy(return_descriptor); ret = LTTNG_OK; break; diff --git a/src/common/session-descriptor.cpp b/src/common/session-descriptor.cpp index 1171b2930..058783785 100644 --- a/src/common/session-descriptor.cpp +++ b/src/common/session-descriptor.cpp @@ -4,13 +4,18 @@ * SPDX-License-Identifier: LGPL-2.1-only */ -#include -#include -#include #include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include namespace { struct lttng_session_descriptor_network_location { @@ -32,6 +37,8 @@ struct lttng_session_descriptor { struct lttng_session_descriptor_network_location network; struct lttng_uri *local; } output; + + lttng::trace_format_descriptor *format_descriptor; }; namespace { @@ -55,8 +62,11 @@ struct lttng_session_descriptor_comm { uint8_t output_type; /* Includes trailing null. */ uint32_t name_len; - /* Name follows, followed by URIs */ + /* Number of URIs */ uint8_t uri_count; + /* Size of lttng_trace_format_descriptor */ + uint32_t trace_format_len; + /* Name follows, followed by URIs, followed by lttng_trace_format */ } LTTNG_PACKED; struct lttng_session_descriptor_live_comm { @@ -223,6 +233,15 @@ lttng_session_descriptor_create(const char *name) if (lttng_session_descriptor_set_session_name(descriptor, name)) { goto error; } + + /* Default to ctf1 trace format */ + try { + descriptor->format_descriptor = new lttng::trace_format_descriptor_ctf1(); + } catch (const std::exception& ex) { + descriptor->format_descriptor = nullptr; + goto error; + } + return descriptor; error: lttng_session_descriptor_destroy(descriptor); @@ -350,6 +369,15 @@ _lttng_session_descriptor_snapshot_create(const char *name) name)) { goto error; } + + /* Default to ctf1 trace format */ + try { + descriptor->base.format_descriptor = new lttng::trace_format_descriptor_ctf1(); + } catch (const std::exception& ex) { + descriptor->base.format_descriptor = nullptr; + goto error; + } + return descriptor; error: lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL); @@ -489,12 +517,21 @@ _lttng_session_descriptor_live_create(const char *name, descriptor->base.type = LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE; descriptor->base.output_type = LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE; + descriptor->live_timer_us = live_timer_interval_us; if (lttng_session_descriptor_set_session_name(&descriptor->base, name)) { goto error; } + /* Default to ctf1 trace format */ + try { + descriptor->base.format_descriptor = new lttng::trace_format_descriptor_ctf1(); + } catch (const std::exception& ex) { + descriptor->base.format_descriptor = nullptr; + goto error; + } + return descriptor; error: lttng_session_descriptor_destroy(descriptor ? &descriptor->base : NULL); @@ -597,6 +634,7 @@ void lttng_session_descriptor_destroy( abort(); } + delete (descriptor->format_descriptor); free(descriptor->name); free(descriptor); } @@ -608,6 +646,7 @@ ssize_t lttng_session_descriptor_create_from_buffer( int i; ssize_t offset = 0, ret; struct lttng_buffer_view current_view; + const char *name = NULL; const struct lttng_session_descriptor_comm *base_header; size_t max_expected_uri_count; @@ -615,6 +654,7 @@ ssize_t lttng_session_descriptor_create_from_buffer( struct lttng_uri *uris[2] = {}; enum lttng_session_descriptor_type type; enum lttng_session_descriptor_output_type output_type; + lttng::trace_format_descriptor::uptr trace_format; current_view = lttng_buffer_view_from_view(payload, offset, sizeof(*base_header)); @@ -722,6 +762,23 @@ skip_name: offset += sizeof(*uri); } + /* Deserialize trace format */ + { + struct lttng_payload_view current_payload_view = + lttng_payload_view_from_buffer_view( + payload, offset, base_header->trace_format_len); + if (!lttng_buffer_view_is_valid(¤t_view)) { + ret = -1; + goto end; + } + ret = lttng::trace_format_descriptor::create_from_payload( + ¤t_payload_view, trace_format); + if (ret < 0 || ret != base_header->trace_format_len) { + ret = -1; + goto end; + } + } + switch (type) { case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: switch (output_type) { @@ -798,6 +855,8 @@ skip_name: goto end; } + (*descriptor)->format_descriptor = trace_format.release(); + ret = offset; end: free(uris[0]); @@ -806,25 +865,31 @@ end: } int lttng_session_descriptor_serialize( - const struct lttng_session_descriptor *descriptor, - struct lttng_dynamic_buffer *buffer) + const struct lttng_session_descriptor *descriptor, lttng_payload *payload) { int ret, i; + size_t header_offset, size_before_payload; /* There are, at most, two URIs to serialize. */ struct lttng_uri *uris[2] = {}; size_t uri_count = 0; /* The live header is a superset of all headers. */ struct lttng_session_descriptor_live_comm header = { - .base = { - .type = (uint8_t) descriptor->type, - .output_type = (uint8_t) descriptor->output_type, - .name_len = (uint32_t) (descriptor->name ? - strlen(descriptor->name) + 1 : 0), - .uri_count = 0, - }, - .live_timer_us = 0, + .base = + { + .type = (uint8_t) descriptor->type, + .output_type = (uint8_t) descriptor + ->output_type, + .name_len = (uint32_t) (descriptor->name ? + strlen(descriptor->name) + + 1 : + 0), + .uri_count = 0, + .trace_format_len = 0, + }, + .live_timer_us = 0, }; + struct lttng_session_descriptor_comm *base_header; const void *header_ptr = NULL; size_t header_size; @@ -858,25 +923,36 @@ int lttng_session_descriptor_serialize( header_size = sizeof(header.base); } - ret = lttng_dynamic_buffer_append(buffer, header_ptr, header_size); + header_offset = payload->buffer.size; + ret = lttng_dynamic_buffer_append(&payload->buffer, header_ptr, header_size); if (ret) { goto end; } if (header.base.name_len) { - ret = lttng_dynamic_buffer_append(buffer, descriptor->name, - header.base.name_len); + ret = lttng_dynamic_buffer_append( + &payload->buffer, descriptor->name, header.base.name_len); if (ret) { goto end; } } for (i = 0; i < uri_count; i++) { - ret = lttng_dynamic_buffer_append(buffer, uris[i], - sizeof(struct lttng_uri)); + ret = lttng_dynamic_buffer_append( + &payload->buffer, uris[i], sizeof(struct lttng_uri)); if (ret) { goto end; } } + + /* Serialize trace format */ + size_before_payload = payload->buffer.size; + ret = reinterpret_cast(descriptor->format_descriptor) + ->serialize(payload); + if (ret) { + goto end; + } + base_header = (typeof(base_header)) (payload->buffer.data + header_offset); + base_header->trace_format_len = payload->buffer.size - size_before_payload; end: return ret; } @@ -1127,6 +1203,14 @@ int lttng_session_descriptor_assign( if (ret) { goto end; } + + try { + dst->format_descriptor = src->format_descriptor->clone().release(); + } catch (...) { + ret = -1; + goto end; + } + switch (dst->output_type) { case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: free(dst->output.local); diff --git a/src/lib/lttng-ctl/lttng-ctl.cpp b/src/lib/lttng-ctl/lttng-ctl.cpp index 9dff22a2b..c6b2e2f35 100644 --- a/src/lib/lttng-ctl/lttng-ctl.cpp +++ b/src/lib/lttng-ctl/lttng-ctl.cpp @@ -1909,12 +1909,12 @@ enum lttng_error_code lttng_create_session_ext( struct lttng_buffer_view reply_view; int reply_ret; bool sessiond_must_generate_ouput; - struct lttng_dynamic_buffer payload; + struct lttng_payload payload; int ret; size_t descriptor_size; struct lttng_session_descriptor *descriptor_reply = NULL; - lttng_dynamic_buffer_init(&payload); + lttng_payload_init(&payload); if (!session_descriptor) { ret_code = LTTNG_ERR_INVALID; goto end; @@ -1933,27 +1933,26 @@ enum lttng_error_code lttng_create_session_ext( } lsm.u.create_session.home_dir_size = (uint16_t) home_dir_len; - ret = lttng_dynamic_buffer_append(&payload, home_dir, - home_dir_len); + ret = lttng_dynamic_buffer_append(&payload.buffer, home_dir, home_dir_len); if (ret) { ret_code = LTTNG_ERR_NOMEM; goto end; } } - descriptor_size = payload.size; + descriptor_size = payload.buffer.size; ret = lttng_session_descriptor_serialize(session_descriptor, &payload); if (ret) { ret_code = LTTNG_ERR_INVALID; goto end; } - descriptor_size = payload.size - descriptor_size; + descriptor_size = payload.buffer.size - descriptor_size; lsm.u.create_session.session_descriptor_size = descriptor_size; /* Command returns a session descriptor on success. */ - reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header(&lsm, payload.data, - payload.size, &reply); + reply_ret = lttng_ctl_ask_sessiond_varlen_no_cmd_header( + &lsm, payload.buffer.data, payload.buffer.size, &reply); if (reply_ret < 0) { ret_code = (lttng_error_code) -reply_ret; goto end; @@ -1974,7 +1973,7 @@ enum lttng_error_code lttng_create_session_ext( lttng_session_descriptor_assign(session_descriptor, descriptor_reply); end: free(reply); - lttng_dynamic_buffer_reset(&payload); + lttng_payload_reset(&payload); lttng_session_descriptor_destroy(descriptor_reply); return ret_code; } -- 2.34.1