2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "ust-field-convert.hpp"
10 #include <common/exception.hpp>
11 #include <common/make-unique.hpp>
13 #include <unordered_map>
15 namespace lst
= lttng::sessiond::trace
;
16 namespace lsu
= lttng::sessiond::ust
;
20 * Type enclosing the session information that may be required during the decoding
21 * of the lttng_ust_ctl_field array provided by applications on registration of
24 class session_attributes
{
26 using registry_enum_getter_fn
=
27 std::function
<lsu::registry_enum::const_rcu_protected_reference(const char *name
,
30 session_attributes(registry_enum_getter_fn reg_enum_getter
,
31 lst::byte_order native_trace_byte_order
) :
32 get_registry_enum
{ reg_enum_getter
},
33 _native_trace_byte_order
{ native_trace_byte_order
}
37 const registry_enum_getter_fn get_registry_enum
;
38 const lst::byte_order _native_trace_byte_order
;
41 /* Used to publish fields on which a field being decoded has an implicit dependency. */
42 using publish_field_fn
= std::function
<void(lst::field::uptr
)>;
44 /* Look-up field from a field location. */
45 using lookup_field_fn
= std::function
<const lst::field
&(const lst::field_location
&)>;
48 create_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
49 const lttng_ust_ctl_field
*end
,
50 const session_attributes
& session_attributes
,
51 const lttng_ust_ctl_field
**next_ust_ctl_field
,
52 publish_field_fn publish_field
,
53 lookup_field_fn lookup_field
,
54 lst::field_location::root lookup_root
,
55 lst::field_location::elements
& current_field_location_elements
,
56 lsu::ctl_field_quirks quirks
);
58 void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
59 const lttng_ust_ctl_field
*end
,
60 const session_attributes
& session_attributes
,
61 const lttng_ust_ctl_field
**next_ust_ctl_field
,
62 publish_field_fn publish_field
,
63 lookup_field_fn lookup_field
,
64 lst::field_location::root lookup_root
,
65 lst::field_location::elements
& current_field_location_elements
,
66 lsu::ctl_field_quirks quirks
);
68 template <class UstCtlEncodingType
>
69 enum lst::null_terminated_string_type::encoding
70 ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding
)
72 static const std::unordered_map
<UstCtlEncodingType
,
73 enum lst::null_terminated_string_type::encoding
>
74 encoding_conversion_map
= {
75 { (UstCtlEncodingType
) lttng_ust_ctl_encode_ASCII
,
76 lst::null_terminated_string_type::encoding::ASCII
},
77 { (UstCtlEncodingType
) lttng_ust_ctl_encode_UTF8
,
78 lst::null_terminated_string_type::encoding::UTF8
},
81 const auto encoding_it
= encoding_conversion_map
.find(encoding
);
82 if (encoding_it
== encoding_conversion_map
.end()) {
83 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
84 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
88 return encoding_it
->second
;
91 template <class UstCtlBaseType
>
92 enum lst::integer_type::base
ust_ctl_base_to_integer_field_base(UstCtlBaseType base
)
94 static const std::unordered_map
<UstCtlBaseType
, enum lst::integer_type::base
>
95 base_conversion_map
= { { 2, lst::integer_type::base::BINARY
},
96 { 8, lst::integer_type::base::OCTAL
},
97 { 10, lst::integer_type::base::DECIMAL
},
98 { 16, lst::integer_type::base::HEXADECIMAL
} };
100 const auto base_it
= base_conversion_map
.find(base
);
101 if (base_it
== base_conversion_map
.end()) {
102 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
103 "Unknown integer base value `{}` encountered when decoding integer field",
107 return base_it
->second
;
111 create_integer_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
112 const lttng_ust_ctl_field
*end
,
113 const session_attributes
& session_attributes
,
114 const lttng_ust_ctl_field
**next_ust_ctl_field
,
115 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
117 if (current
>= end
) {
118 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
119 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
122 const auto base
= ust_ctl_base_to_integer_field_base(current
->type
.u
.integer
.base
);
123 const auto signedness
= current
->type
.u
.integer
.signedness
?
124 lst::integer_type::signedness::SIGNED
:
125 lst::integer_type::signedness::UNSIGNED
;
126 const auto byte_order
= current
->type
.u
.integer
.reverse_byte_order
?
127 lst::type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
128 session_attributes
._native_trace_byte_order
;
130 *next_ust_ctl_field
= current
+ 1;
132 return lttng::make_unique
<const lst::integer_type
>(current
->type
.u
.integer
.alignment
,
134 current
->type
.u
.integer
.size
,
140 create_floating_point_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
141 const lttng_ust_ctl_field
*end
,
142 const session_attributes
& session_attributes
,
143 const lttng_ust_ctl_field
**next_ust_ctl_field
,
144 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
146 if (current
>= end
) {
147 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
148 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
151 *next_ust_ctl_field
= current
+ 1;
153 const auto byte_order
= current
->type
.u
._float
.reverse_byte_order
?
154 lst::type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
155 session_attributes
._native_trace_byte_order
;
158 return lttng::make_unique
<const lst::floating_point_type
>(
159 current
->type
.u
._float
.alignment
,
161 current
->type
.u
._float
.exp_dig
,
162 current
->type
.u
._float
.mant_dig
);
163 } catch (lttng::invalid_argument_error
& ex
) {
164 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
165 "Invalid floating point attribute in {}: {}", typeid(*current
), ex
.what()));
170 create_enumeration_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
171 const lttng_ust_ctl_field
*end
,
172 const session_attributes
& session_attributes
,
173 const lttng_ust_ctl_field
**next_ust_ctl_field
,
174 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
176 if (current
>= end
) {
177 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
178 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
181 uint64_t enumeration_id
;
182 const auto& enum_uctl_field
= *current
;
183 const char *enumeration_name
;
184 const auto *enum_container_uctl_type
=
185 ¤t
->type
.u
.legacy
.basic
.enumeration
.container_type
;
187 if (enum_uctl_field
.type
.atype
== lttng_ust_ctl_atype_enum_nestable
) {
188 /* Nestable enumeration fields are followed by their container type. */
190 if (current
>= end
) {
191 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
192 "Array of {} is too short to contain nestable enumeration's container",
196 if (current
->type
.atype
!= lttng_ust_ctl_atype_integer
) {
197 LTTNG_THROW_PROTOCOL_ERROR(
198 fmt::format("Invalid type of nestable enum container: type id = {}",
199 current
->type
.atype
));
202 enum_container_uctl_type
= ¤t
->type
.u
.integer
;
203 enumeration_id
= enum_uctl_field
.type
.u
.enum_nestable
.id
;
204 enumeration_name
= enum_uctl_field
.type
.u
.enum_nestable
.name
;
206 enumeration_id
= enum_uctl_field
.type
.u
.legacy
.basic
.enumeration
.id
;
207 enumeration_name
= enum_uctl_field
.type
.u
.legacy
.basic
.enumeration
.name
;
210 *next_ust_ctl_field
= current
+ 1;
212 const auto base
= ust_ctl_base_to_integer_field_base(enum_container_uctl_type
->base
);
213 const auto byte_order
= enum_container_uctl_type
->reverse_byte_order
?
214 lst::integer_type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
215 session_attributes
._native_trace_byte_order
;
216 const auto signedness
= enum_container_uctl_type
->signedness
?
217 lst::integer_type::signedness::SIGNED
:
218 lst::integer_type::signedness::UNSIGNED
;
220 if (signedness
== lst::integer_type::signedness::SIGNED
) {
221 const auto& enum_registry
= static_cast<const lsu::registry_signed_enum
&>(
222 *session_attributes
.get_registry_enum(enumeration_name
, enumeration_id
));
224 return lttng::make_unique
<const lst::signed_enumeration_type
>(
225 enum_container_uctl_type
->alignment
,
227 enum_container_uctl_type
->size
,
229 enum_registry
._mappings
);
231 const auto& enum_registry
= static_cast<const lsu::registry_unsigned_enum
&>(
232 *session_attributes
.get_registry_enum(enumeration_name
, enumeration_id
));
234 return lttng::make_unique
<const lst::unsigned_enumeration_type
>(
235 enum_container_uctl_type
->alignment
,
237 enum_container_uctl_type
->size
,
239 enum_registry
._mappings
);
244 create_string_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
245 const lttng_ust_ctl_field
*end
,
246 const session_attributes
& session_attributes
247 __attribute__((unused
)),
248 const lttng_ust_ctl_field
**next_ust_ctl_field
,
249 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
251 if (current
>= end
) {
252 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
253 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
256 const auto& string_uctl_field
= *current
;
257 *next_ust_ctl_field
= current
+ 1;
259 const auto encoding
=
260 ust_ctl_encoding_to_string_field_encoding(string_uctl_field
.type
.u
.string
.encoding
);
262 return lttng::make_unique
<const lst::null_terminated_string_type
>(1, encoding
);
266 create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type
& type
,
267 const session_attributes
& session_attributes
)
269 /* Checked by caller. */
270 LTTNG_ASSERT(type
.atype
== lttng_ust_ctl_atype_integer
);
272 const auto byte_order
= type
.u
.basic
.integer
.reverse_byte_order
?
273 lst::integer_type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
274 session_attributes
._native_trace_byte_order
;
275 const auto signedness
= type
.u
.basic
.integer
.signedness
?
276 lst::integer_type::signedness::SIGNED
:
277 lst::integer_type::signedness::UNSIGNED
;
278 const auto base
= ust_ctl_base_to_integer_field_base(type
.u
.basic
.integer
.base
);
279 const auto size
= type
.u
.basic
.integer
.size
;
280 const auto alignment
= type
.u
.basic
.integer
.alignment
;
282 return lttng::make_unique
<const lst::integer_type
>(
283 alignment
, byte_order
, size
, signedness
, base
);
287 create_array_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
288 const lttng_ust_ctl_field
*end
,
289 const session_attributes
& session_attributes
,
290 const lttng_ust_ctl_field
**next_ust_ctl_field
,
291 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
293 if (current
>= end
) {
294 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
295 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
298 const auto& array_uctl_field
= *current
;
299 uint32_t array_alignment
, array_length
;
300 lst::type::cuptr element_type
;
301 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
303 array_length
= array_uctl_field
.type
.u
.legacy
.array
.length
;
306 const auto& element_uctl_type
= array_uctl_field
.type
.u
.legacy
.array
.elem_type
;
307 if (element_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
308 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
309 "Unexpected legacy array element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
310 element_uctl_type
.atype
,
311 lttng_ust_ctl_atype_integer
));
315 create_integer_type_from_ust_ctl_basic_type(element_uctl_type
, session_attributes
);
316 if (element_uctl_type
.atype
== lttng_ust_ctl_atype_integer
&&
317 element_uctl_type
.u
.basic
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
318 /* Element represents a text character. */
319 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
320 element_uctl_type
.u
.basic
.integer
.encoding
);
323 *next_ust_ctl_field
= current
+ 1;
325 if (element_encoding
) {
326 const auto integer_element_size
=
327 static_cast<const lst::integer_type
&>(*element_type
).size
;
329 if (integer_element_size
!= 8) {
330 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
331 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
332 integer_element_size
));
335 /* Array is a static-length string. */
336 return lttng::make_unique
<lst::static_length_string_type
>(
337 array_alignment
, *element_encoding
, array_length
);
340 return lttng::make_unique
<lst::static_length_array_type
>(
341 array_alignment
, std::move(element_type
), array_length
);
344 lst::type::cuptr
create_array_nestable_type_from_ust_ctl_fields(
345 const lttng_ust_ctl_field
*current
,
346 const lttng_ust_ctl_field
*end
,
347 const session_attributes
& session_attributes
,
348 const lttng_ust_ctl_field
**next_ust_ctl_field
,
349 publish_field_fn publish_field
,
350 lookup_field_fn lookup_field
,
351 lst::field_location::root lookup_root
,
352 lst::field_location::elements
& current_field_location_elements
,
353 lsu::ctl_field_quirks quirks
)
355 if (current
>= end
) {
356 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
357 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
360 const auto& array_uctl_field
= *current
;
361 uint32_t array_alignment
, array_length
;
362 lst::type::cuptr element_type
;
363 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
365 array_length
= array_uctl_field
.type
.u
.array_nestable
.length
;
366 array_alignment
= array_uctl_field
.type
.u
.array_nestable
.alignment
;
368 /* Nestable array fields are followed by their element type. */
369 const auto& element_uctl_field
= *(current
+ 1);
371 /* next_ust_ctl_field is updated as needed. */
372 element_type
= create_type_from_ust_ctl_fields(&element_uctl_field
,
379 current_field_location_elements
,
381 if (element_uctl_field
.type
.atype
== lttng_ust_ctl_atype_integer
&&
382 element_uctl_field
.type
.u
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
383 /* Element represents a text character. */
384 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
385 element_uctl_field
.type
.u
.integer
.encoding
);
388 if (element_encoding
) {
389 const auto integer_element_size
=
390 static_cast<const lst::integer_type
&>(*element_type
).size
;
392 if (integer_element_size
!= 8) {
393 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
394 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
395 integer_element_size
));
398 /* Array is a static-length string. */
399 return lttng::make_unique
<lst::static_length_string_type
>(
400 array_alignment
, *element_encoding
, array_length
);
403 return lttng::make_unique
<lst::static_length_array_type
>(
404 array_alignment
, std::move(element_type
), array_length
);
408 * For legacy sequence types, LTTng-UST expresses both the sequence and sequence
409 * length as part of the same lttng_ust_ctl_field entry.
411 lst::type::cuptr
create_sequence_type_from_ust_ctl_fields(
412 const lttng_ust_ctl_field
*current
,
413 const lttng_ust_ctl_field
*end
,
414 const session_attributes
& session_attributes
,
415 const lttng_ust_ctl_field
**next_ust_ctl_field
,
416 publish_field_fn publish_field
,
417 lst::field_location::root lookup_root
,
418 lst::field_location::elements
& current_field_location_elements
,
419 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
421 if (current
>= end
) {
422 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
423 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
426 const auto& sequence_uctl_field
= *current
;
427 const auto& element_uctl_type
= sequence_uctl_field
.type
.u
.legacy
.sequence
.elem_type
;
428 const auto& length_uctl_type
= sequence_uctl_field
.type
.u
.legacy
.sequence
.length_type
;
429 const auto sequence_alignment
= 0U;
431 if (element_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
432 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
433 "Unexpected legacy sequence element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
434 element_uctl_type
.atype
,
435 lttng_ust_ctl_atype_integer
));
438 if (length_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
439 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
440 "Unexpected legacy sequence length field type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
441 length_uctl_type
.atype
,
442 lttng_ust_ctl_atype_integer
));
445 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
446 if (element_uctl_type
.atype
== lttng_ust_ctl_atype_integer
&&
447 element_uctl_type
.u
.basic
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
448 /* Element represents a text character. */
449 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
450 element_uctl_type
.u
.basic
.integer
.encoding
);
453 const auto length_field_name
= fmt::format("_{}_length", sequence_uctl_field
.name
);
455 create_integer_type_from_ust_ctl_basic_type(element_uctl_type
, session_attributes
);
457 create_integer_type_from_ust_ctl_basic_type(length_uctl_type
, session_attributes
);
459 lst::field_location::elements length_field_location_elements
=
460 current_field_location_elements
;
461 length_field_location_elements
.emplace_back(length_field_name
);
463 const lst::field_location length_field_location
{
464 lookup_root
, std::move(length_field_location_elements
)
467 /* Publish an implicit length field _before_ the sequence field. */
468 publish_field(lttng::make_unique
<lst::field
>(std::move(length_field_name
),
469 std::move(length_type
)));
471 *next_ust_ctl_field
= current
+ 1;
473 if (element_encoding
) {
474 const auto integer_element_size
=
475 static_cast<const lst::integer_type
&>(*element_type
).size
;
477 if (integer_element_size
!= 8) {
478 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
479 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
480 integer_element_size
));
483 /* Sequence is a dynamic-length string. */
484 return lttng::make_unique
<lst::dynamic_length_string_type
>(
485 sequence_alignment
, *element_encoding
, std::move(length_field_location
));
488 return lttng::make_unique
<lst::dynamic_length_array_type
>(
489 sequence_alignment
, std::move(element_type
), std::move(length_field_location
));
492 lst::type::cuptr
create_sequence_nestable_type_from_ust_ctl_fields(
493 const lttng_ust_ctl_field
*current
,
494 const lttng_ust_ctl_field
*end
,
495 const session_attributes
& session_attributes
,
496 const lttng_ust_ctl_field
**next_ust_ctl_field
,
497 publish_field_fn publish_field
,
498 lookup_field_fn lookup_field
,
499 lst::field_location::root lookup_root
,
500 lst::field_location::elements
& current_field_location_elements
,
501 lsu::ctl_field_quirks quirks
)
503 if (current
>= end
) {
504 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
505 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
508 const auto& sequence_uctl_field
= *current
;
509 const auto sequence_alignment
= sequence_uctl_field
.type
.u
.sequence_nestable
.alignment
;
510 const auto *length_field_name
= sequence_uctl_field
.type
.u
.sequence_nestable
.length_name
;
512 /* Nestable sequence fields are followed by their element type. */
513 const auto& element_uctl_field
= *(current
+ 1);
515 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
516 if (element_uctl_field
.type
.atype
== lttng_ust_ctl_atype_integer
&&
517 element_uctl_field
.type
.u
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
518 /* Element represents a text character. */
519 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
520 element_uctl_field
.type
.u
.integer
.encoding
);
523 /* next_ust_ctl_field is updated as needed. */
524 auto element_type
= create_type_from_ust_ctl_fields(&element_uctl_field
,
531 current_field_location_elements
,
534 if (lttng_strnlen(sequence_uctl_field
.type
.u
.sequence_nestable
.length_name
,
535 sizeof(sequence_uctl_field
.type
.u
.sequence_nestable
.length_name
)) ==
536 sizeof(sequence_uctl_field
.type
.u
.sequence_nestable
.length_name
)) {
537 LTTNG_THROW_PROTOCOL_ERROR("Sequence length field name is not null terminated");
540 lst::field_location::elements length_field_location_elements
=
541 current_field_location_elements
;
542 length_field_location_elements
.emplace_back(std::move(length_field_name
));
544 const lst::field_location length_field_location
{
545 lookup_root
, std::move(length_field_location_elements
)
548 /* Validate existence of length field (throws if not found). */
549 const auto& length_field
= lookup_field(length_field_location
);
550 const auto *integer_selector_field
=
551 dynamic_cast<const lst::integer_type
*>(&length_field
.get_type());
552 if (!integer_selector_field
) {
553 LTTNG_THROW_PROTOCOL_ERROR(
554 "Invalid selector field type referenced from sequence: expected integer or enumeration");
557 if (element_encoding
) {
558 const auto integer_element_size
=
559 static_cast<const lst::integer_type
&>(*element_type
).size
;
561 if (integer_element_size
!= 8) {
562 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
563 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
564 integer_element_size
));
567 /* Sqeuence is a dynamic-length string. */
568 return lttng::make_unique
<lst::dynamic_length_string_type
>(
569 sequence_alignment
, *element_encoding
, std::move(length_field_location
));
572 return lttng::make_unique
<lst::dynamic_length_array_type
>(
573 sequence_alignment
, std::move(element_type
), std::move(length_field_location
));
577 create_structure_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
578 const lttng_ust_ctl_field
*end
,
579 const session_attributes
& session_attributes
580 __attribute__((unused
)),
581 const lttng_ust_ctl_field
**next_ust_ctl_field
,
582 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
584 if (current
>= end
) {
585 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
586 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
589 uint32_t field_count
;
591 const auto& structure_uctl_field
= *current
;
593 if (structure_uctl_field
.type
.atype
== lttng_ust_ctl_atype_struct
) {
594 field_count
= structure_uctl_field
.type
.u
.legacy
._struct
.nr_fields
;
597 field_count
= structure_uctl_field
.type
.u
.struct_nestable
.nr_fields
;
598 alignment
= structure_uctl_field
.type
.u
.struct_nestable
.alignment
;
601 if (field_count
!= 0) {
602 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
603 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
607 *next_ust_ctl_field
= current
+ 1;
608 return lttng::make_unique
<lst::structure_type
>(alignment
, lst::structure_type::fields());
611 template <class VariantSelectorMappingIntegerType
>
612 typename
lst::variant_type
<VariantSelectorMappingIntegerType
>::choices
613 create_typed_variant_choices(const lttng_ust_ctl_field
*current
,
614 const lttng_ust_ctl_field
*end
,
615 const session_attributes
& session_attributes
,
616 const lttng_ust_ctl_field
**next_ust_ctl_field
,
617 lookup_field_fn lookup_field
,
618 lst::field_location::root lookup_root
,
619 lst::field_location::elements
& current_field_location_elements
,
620 unsigned int choice_count
,
621 const lst::field
& selector_field
,
622 lsu::ctl_field_quirks quirks
)
624 typename
lst::variant_type
<VariantSelectorMappingIntegerType
>::choices choices
;
625 const auto& typed_enumeration
=
626 static_cast<const lst::typed_enumeration_type
<VariantSelectorMappingIntegerType
>&>(
627 selector_field
.get_type());
629 for (unsigned int i
= 0; i
< choice_count
; i
++) {
630 create_field_from_ust_ctl_fields(
635 [&choices
, typed_enumeration
, &selector_field
, quirks
](
636 lst::field::uptr field
) {
638 * Find the enumeration mapping that matches the
641 const auto mapping_it
= std::find_if(
642 typed_enumeration
.mappings_
->begin(),
643 typed_enumeration
.mappings_
->end(),
645 quirks
](const typename
std::remove_reference
<
646 decltype(typed_enumeration
)>::type::mapping
&
648 if (static_cast<bool>(
650 lsu::ctl_field_quirks::
651 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
)) {
653 * Check if they match with
654 * a prepended underscore
655 * and, if not, perform the
658 if ((std::string("_") + field
->name
) ==
664 return mapping
.name
== field
->name
;
667 if (mapping_it
== typed_enumeration
.mappings_
->end()) {
668 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
669 "Invalid variant choice: `{}` does not match any mapping in `{}` enumeration",
671 selector_field
.name
));
674 choices
.emplace_back(*mapping_it
, field
->move_type());
678 current_field_location_elements
,
681 current
= *next_ust_ctl_field
;
687 lst::type::cuptr
create_variant_field_from_ust_ctl_fields(
688 const lttng_ust_ctl_field
*current
,
689 const lttng_ust_ctl_field
*end
,
690 const session_attributes
& session_attributes
,
691 const lttng_ust_ctl_field
**next_ust_ctl_field
,
692 lookup_field_fn lookup_field
,
693 lst::field_location::root lookup_root
,
694 lst::field_location::elements
& current_field_location_elements
,
695 lsu::ctl_field_quirks quirks
)
697 if (current
>= end
) {
698 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
699 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
702 const auto& variant_uctl_field
= *current
;
706 uint32_t choice_count
;
707 const char *tag_name
;
709 if (variant_uctl_field
.type
.atype
== lttng_ust_ctl_atype_variant
) {
711 choice_count
= variant_uctl_field
.type
.u
.legacy
.variant
.nr_choices
;
712 tag_name
= variant_uctl_field
.type
.u
.legacy
.variant
.tag_name
;
714 alignment
= variant_uctl_field
.type
.u
.variant_nestable
.alignment
;
715 choice_count
= variant_uctl_field
.type
.u
.variant_nestable
.nr_choices
;
716 tag_name
= variant_uctl_field
.type
.u
.variant_nestable
.tag_name
;
719 lst::field_location::elements selector_field_location_elements
=
720 current_field_location_elements
;
721 selector_field_location_elements
.emplace_back(tag_name
);
723 const lst::field_location selector_field_location
{
724 lookup_root
, std::move(selector_field_location_elements
)
727 /* Validate existence of selector field (throws if not found). */
728 const auto& selector_field
= lookup_field(selector_field_location
);
729 const auto *enumeration_selector_type
=
730 dynamic_cast<const lst::enumeration_type
*>(&selector_field
.get_type());
731 if (!enumeration_selector_type
) {
732 LTTNG_THROW_PROTOCOL_ERROR(
733 "Invalid selector field type referenced from variant: expected enumeration");
736 const bool selector_is_signed
= enumeration_selector_type
->signedness_
==
737 lst::integer_type::signedness::SIGNED
;
739 /* Choices follow. next_ust_ctl_field is updated as needed. */
740 if (selector_is_signed
) {
741 lst::variant_type
<lst::signed_enumeration_type::mapping::range_t::range_integer_t
>::
742 choices choices
= create_typed_variant_choices
<int64_t>(
749 current_field_location_elements
,
754 return lttng::make_unique
<lst::variant_type
<int64_t>>(
755 alignment
, std::move(selector_field_location
), std::move(choices
));
758 lst::unsigned_enumeration_type::mapping::range_t::range_integer_t
>::choices
759 choices
= create_typed_variant_choices
<uint64_t>(
766 current_field_location_elements
,
771 return lttng::make_unique
<lst::variant_type
<uint64_t>>(
772 alignment
, std::move(selector_field_location
), std::move(choices
));
777 create_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
778 const lttng_ust_ctl_field
*end
,
779 const session_attributes
& session_attributes
,
780 const lttng_ust_ctl_field
**next_ust_ctl_field
,
781 publish_field_fn publish_field
,
782 lookup_field_fn lookup_field
,
783 lst::field_location::root lookup_root
,
784 lst::field_location::elements
& current_field_location_elements
,
785 lsu::ctl_field_quirks quirks
)
787 switch (current
->type
.atype
) {
788 case lttng_ust_ctl_atype_integer
:
789 return create_integer_type_from_ust_ctl_fields(
790 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
791 case lttng_ust_ctl_atype_enum
:
792 case lttng_ust_ctl_atype_enum_nestable
:
793 return create_enumeration_type_from_ust_ctl_fields(
794 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
795 case lttng_ust_ctl_atype_float
:
796 return create_floating_point_type_from_ust_ctl_fields(
797 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
798 case lttng_ust_ctl_atype_string
:
799 return create_string_type_from_ust_ctl_fields(
800 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
801 case lttng_ust_ctl_atype_array
:
802 return create_array_type_from_ust_ctl_fields(
803 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
804 case lttng_ust_ctl_atype_array_nestable
:
805 return create_array_nestable_type_from_ust_ctl_fields(
813 current_field_location_elements
,
815 case lttng_ust_ctl_atype_sequence
:
816 return create_sequence_type_from_ust_ctl_fields(current
,
822 current_field_location_elements
,
824 case lttng_ust_ctl_atype_sequence_nestable
:
825 return create_sequence_nestable_type_from_ust_ctl_fields(
833 current_field_location_elements
,
835 case lttng_ust_ctl_atype_struct
:
836 case lttng_ust_ctl_atype_struct_nestable
:
837 return create_structure_field_from_ust_ctl_fields(
838 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
839 case lttng_ust_ctl_atype_variant
:
840 case lttng_ust_ctl_atype_variant_nestable
:
841 return create_variant_field_from_ust_ctl_fields(current
,
847 current_field_location_elements
,
850 LTTNG_THROW_PROTOCOL_ERROR(
851 fmt::format("Unknown {} value `{}` encountered while converting {} to {}",
852 typeid(current
->type
.atype
),
855 typeid(lst::type::cuptr::element_type
)));
859 void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
860 const lttng_ust_ctl_field
*end
,
861 const session_attributes
& session_attributes
,
862 const lttng_ust_ctl_field
**next_ust_ctl_field
,
863 publish_field_fn publish_field
,
864 lookup_field_fn lookup_field
,
865 lst::field_location::root lookup_root
,
866 lst::field_location::elements
& current_field_location_elements
,
867 lsu::ctl_field_quirks quirks
)
869 LTTNG_ASSERT(current
< end
);
871 if (lttng_strnlen(current
->name
, sizeof(current
->name
)) == sizeof(current
->name
)) {
872 LTTNG_THROW_PROTOCOL_ERROR(
873 fmt::format("Name of {} is not null-terminated", typeid(*current
)));
876 publish_field(lttng::make_unique
<lst::field
>(
878 create_type_from_ust_ctl_fields(current
,
885 current_field_location_elements
,
889 std::vector
<lst::field::cuptr
>::iterator
890 lookup_field_in_vector(std::vector
<lst::field::cuptr
>& fields
, const lst::field_location
& location
)
892 if (location
.elements_
.size() != 1) {
893 LTTNG_THROW_ERROR(fmt::format(
894 "Unexpected field location received during field look-up: location = {}",
899 * In the context of fields received from LTTng-UST, field
900 * look-up is extremely naive as the protocol can only
901 * express empty structures. It is safe to assume that
902 * location has a depth of 1 and directly refers to a field
903 * in the 'fields' vector.
905 const auto field_it
=
906 std::find_if(fields
.begin(), fields
.end(), [location
](lst::field::cuptr
& field
) {
907 return field
->name
== location
.elements_
[0];
910 if (field_it
== fields
.end()) {
911 LTTNG_THROW_PROTOCOL_ERROR(
912 fmt::format("Failed to look-up field: location = {}", location
));
919 * `lttng_ust_ctl_field`s can be nested, in which case creating a field will consume
920 * more than one lttng_ust_ctl_field. create_field_from_ust_ctl_fields returns the
921 * position of the next lttng_ust_ctl_field to consume or `end` when the last field
924 * Always returns a new field, throws on error.
926 std::vector
<lst::field::cuptr
>
927 create_fields_from_ust_ctl_fields(const lsu::registry_session
& session
,
928 const lttng_ust_ctl_field
*current
,
929 const lttng_ust_ctl_field
*end
,
930 lst::field_location::root lookup_root
,
931 lsu::ctl_field_quirks quirks
)
933 std::vector
<lst::field::cuptr
> fields
;
934 const auto trace_native_byte_order
= session
.abi
.byte_order
;
935 const session_attributes session_attributes
{
936 [&session
](const char *enum_name
, uint64_t enum_id
) {
937 return session
.enumeration(enum_name
, enum_id
);
939 trace_native_byte_order
941 /* Location of field being created. */
942 lst::field_location::elements current_field_location_elements
;
944 while (current
< end
) {
945 auto *next_field
= current
;
948 * create_field_from_ust_ctl_fields will consume one field at a time.
949 * However, some fields expressed by LTTng-UST's protocol are expended
950 * to multiple event fields (legacy sequence fields implicitly define
951 * their length field).
953 * The lambda allows the factory functions to push as many fields as
954 * needed depending on the decoded field's type.
956 create_field_from_ust_ctl_fields(
961 [&fields
](lst::field::cuptr field
) {
962 /* Publishing a field simply adds it to the converted
964 fields
.emplace_back(std::move(field
));
966 [&fields
](const lst::field_location
& location
)
967 -> lookup_field_fn::result_type
{
968 /* Resolve location to a previously-constructed field. */
969 return **lookup_field_in_vector(fields
, location
);
972 current_field_location_elements
,
975 current
= next_field
;
982 std::vector
<lst::field::cuptr
>
983 lsu::create_trace_fields_from_ust_ctl_fields(const lsu::registry_session
& session
,
984 const lttng_ust_ctl_field
*fields
,
985 std::size_t field_count
,
986 lst::field_location::root lookup_root
,
987 lsu::ctl_field_quirks quirks
)
989 return create_fields_from_ust_ctl_fields(
990 session
, fields
, fields
+ field_count
, lookup_root
, quirks
);