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>
16 namespace lst
= lttng::sessiond::trace
;
17 namespace lsu
= lttng::sessiond::ust
;
21 * Type enclosing the session information that may be required during the decoding
22 * of the lttng_ust_ctl_field array provided by applications on registration of
25 class session_attributes
{
27 using registry_enum_getter_fn
=
28 std::function
<lsu::registry_enum::const_rcu_protected_reference(const char *name
,
31 session_attributes(registry_enum_getter_fn reg_enum_getter
,
32 lst::byte_order native_trace_byte_order
) :
33 get_registry_enum
{ std::move(reg_enum_getter
) },
34 _native_trace_byte_order
{ native_trace_byte_order
}
38 const registry_enum_getter_fn get_registry_enum
;
39 const lst::byte_order _native_trace_byte_order
;
42 /* Used to publish fields on which a field being decoded has an implicit dependency. */
43 using publish_field_fn
= std::function
<void(lst::field::uptr
)>;
45 /* Look-up field from a field location. */
46 using lookup_field_fn
= std::function
<const lst::field
&(const lst::field_location
&)>;
49 create_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
50 const lttng_ust_ctl_field
*end
,
51 const session_attributes
& session_attributes
,
52 const lttng_ust_ctl_field
**next_ust_ctl_field
,
53 const publish_field_fn
& publish_field
,
54 const lookup_field_fn
& lookup_field
,
55 lst::field_location::root lookup_root
,
56 lst::field_location::elements
& current_field_location_elements
,
57 lsu::ctl_field_quirks quirks
);
59 void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
60 const lttng_ust_ctl_field
*end
,
61 const session_attributes
& session_attributes
,
62 const lttng_ust_ctl_field
**next_ust_ctl_field
,
63 const publish_field_fn
& publish_field
,
64 const lookup_field_fn
& lookup_field
,
65 lst::field_location::root lookup_root
,
66 lst::field_location::elements
& current_field_location_elements
,
67 lsu::ctl_field_quirks quirks
);
69 template <class UstCtlEncodingType
>
70 enum lst::null_terminated_string_type::encoding
71 ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding
)
73 static const std::unordered_map
<UstCtlEncodingType
,
74 enum lst::null_terminated_string_type::encoding
>
75 encoding_conversion_map
= {
76 { (UstCtlEncodingType
) lttng_ust_ctl_encode_ASCII
,
77 lst::null_terminated_string_type::encoding::ASCII
},
78 { (UstCtlEncodingType
) lttng_ust_ctl_encode_UTF8
,
79 lst::null_terminated_string_type::encoding::UTF8
},
82 const auto encoding_it
= encoding_conversion_map
.find(encoding
);
83 if (encoding_it
== encoding_conversion_map
.end()) {
84 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
85 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
89 return encoding_it
->second
;
92 template <class UstCtlBaseType
>
93 enum lst::integer_type::base
ust_ctl_base_to_integer_field_base(UstCtlBaseType base
)
95 static const std::unordered_map
<UstCtlBaseType
, enum lst::integer_type::base
>
96 base_conversion_map
= { { 2, lst::integer_type::base::BINARY
},
97 { 8, lst::integer_type::base::OCTAL
},
98 { 10, lst::integer_type::base::DECIMAL
},
99 { 16, lst::integer_type::base::HEXADECIMAL
} };
101 const auto base_it
= base_conversion_map
.find(base
);
102 if (base_it
== base_conversion_map
.end()) {
103 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
104 "Unknown integer base value `{}` encountered when decoding integer field",
108 return base_it
->second
;
112 create_integer_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
113 const lttng_ust_ctl_field
*end
,
114 const session_attributes
& session_attributes
,
115 const lttng_ust_ctl_field
**next_ust_ctl_field
,
116 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
118 if (current
>= end
) {
119 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
120 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
123 const auto base
= ust_ctl_base_to_integer_field_base(current
->type
.u
.integer
.base
);
124 const auto signedness
= current
->type
.u
.integer
.signedness
?
125 lst::integer_type::signedness::SIGNED
:
126 lst::integer_type::signedness::UNSIGNED
;
127 const auto byte_order
= current
->type
.u
.integer
.reverse_byte_order
?
128 lst::type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
129 session_attributes
._native_trace_byte_order
;
131 *next_ust_ctl_field
= current
+ 1;
133 return lttng::make_unique
<const lst::integer_type
>(current
->type
.u
.integer
.alignment
,
135 current
->type
.u
.integer
.size
,
141 create_floating_point_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
142 const lttng_ust_ctl_field
*end
,
143 const session_attributes
& session_attributes
,
144 const lttng_ust_ctl_field
**next_ust_ctl_field
,
145 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
147 if (current
>= end
) {
148 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
149 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
152 *next_ust_ctl_field
= current
+ 1;
154 const auto byte_order
= current
->type
.u
._float
.reverse_byte_order
?
155 lst::type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
156 session_attributes
._native_trace_byte_order
;
159 return lttng::make_unique
<const lst::floating_point_type
>(
160 current
->type
.u
._float
.alignment
,
162 current
->type
.u
._float
.exp_dig
,
163 current
->type
.u
._float
.mant_dig
);
164 } catch (lttng::invalid_argument_error
& ex
) {
165 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
166 "Invalid floating point attribute in {}: {}", typeid(*current
), ex
.what()));
171 create_enumeration_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
172 const lttng_ust_ctl_field
*end
,
173 const session_attributes
& session_attributes
,
174 const lttng_ust_ctl_field
**next_ust_ctl_field
,
175 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
177 if (current
>= end
) {
178 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
179 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
182 uint64_t enumeration_id
;
183 const auto& enum_uctl_field
= *current
;
184 const char *enumeration_name
;
185 const auto *enum_container_uctl_type
=
186 ¤t
->type
.u
.legacy
.basic
.enumeration
.container_type
;
188 if (enum_uctl_field
.type
.atype
== lttng_ust_ctl_atype_enum_nestable
) {
189 /* Nestable enumeration fields are followed by their container type. */
191 if (current
>= end
) {
192 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
193 "Array of {} is too short to contain nestable enumeration's container",
197 if (current
->type
.atype
!= lttng_ust_ctl_atype_integer
) {
198 LTTNG_THROW_PROTOCOL_ERROR(
199 fmt::format("Invalid type of nestable enum container: type id = {}",
200 current
->type
.atype
));
203 enum_container_uctl_type
= ¤t
->type
.u
.integer
;
204 enumeration_id
= enum_uctl_field
.type
.u
.enum_nestable
.id
;
205 enumeration_name
= enum_uctl_field
.type
.u
.enum_nestable
.name
;
207 enumeration_id
= enum_uctl_field
.type
.u
.legacy
.basic
.enumeration
.id
;
208 enumeration_name
= enum_uctl_field
.type
.u
.legacy
.basic
.enumeration
.name
;
211 *next_ust_ctl_field
= current
+ 1;
213 const auto base
= ust_ctl_base_to_integer_field_base(enum_container_uctl_type
->base
);
214 const auto byte_order
= enum_container_uctl_type
->reverse_byte_order
?
215 lst::integer_type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
216 session_attributes
._native_trace_byte_order
;
217 const auto signedness
= enum_container_uctl_type
->signedness
?
218 lst::integer_type::signedness::SIGNED
:
219 lst::integer_type::signedness::UNSIGNED
;
221 if (signedness
== lst::integer_type::signedness::SIGNED
) {
222 const auto& enum_registry
= static_cast<const lsu::registry_signed_enum
&>(
223 *session_attributes
.get_registry_enum(enumeration_name
, enumeration_id
));
225 return lttng::make_unique
<const lst::signed_enumeration_type
>(
226 enum_container_uctl_type
->alignment
,
228 enum_container_uctl_type
->size
,
230 enum_registry
._mappings
);
232 const auto& enum_registry
= static_cast<const lsu::registry_unsigned_enum
&>(
233 *session_attributes
.get_registry_enum(enumeration_name
, enumeration_id
));
235 return lttng::make_unique
<const lst::unsigned_enumeration_type
>(
236 enum_container_uctl_type
->alignment
,
238 enum_container_uctl_type
->size
,
240 enum_registry
._mappings
);
245 create_string_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
246 const lttng_ust_ctl_field
*end
,
247 const session_attributes
& session_attributes
248 __attribute__((unused
)),
249 const lttng_ust_ctl_field
**next_ust_ctl_field
,
250 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
252 if (current
>= end
) {
253 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
254 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
257 const auto& string_uctl_field
= *current
;
258 *next_ust_ctl_field
= current
+ 1;
260 const auto encoding
=
261 ust_ctl_encoding_to_string_field_encoding(string_uctl_field
.type
.u
.string
.encoding
);
263 return lttng::make_unique
<const lst::null_terminated_string_type
>(1, encoding
);
267 create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type
& type
,
268 const session_attributes
& session_attributes
)
270 /* Checked by caller. */
271 LTTNG_ASSERT(type
.atype
== lttng_ust_ctl_atype_integer
);
273 const auto byte_order
= type
.u
.basic
.integer
.reverse_byte_order
?
274 lst::integer_type::reverse_byte_order(session_attributes
._native_trace_byte_order
) :
275 session_attributes
._native_trace_byte_order
;
276 const auto signedness
= type
.u
.basic
.integer
.signedness
?
277 lst::integer_type::signedness::SIGNED
:
278 lst::integer_type::signedness::UNSIGNED
;
279 const auto base
= ust_ctl_base_to_integer_field_base(type
.u
.basic
.integer
.base
);
280 const auto size
= type
.u
.basic
.integer
.size
;
281 const auto alignment
= type
.u
.basic
.integer
.alignment
;
283 return lttng::make_unique
<const lst::integer_type
>(
284 alignment
, byte_order
, size
, signedness
, base
);
288 create_array_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
289 const lttng_ust_ctl_field
*end
,
290 const session_attributes
& session_attributes
,
291 const lttng_ust_ctl_field
**next_ust_ctl_field
,
292 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
294 if (current
>= end
) {
295 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
296 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
299 const auto& array_uctl_field
= *current
;
300 uint32_t array_alignment
, array_length
;
301 lst::type::cuptr element_type
;
302 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
304 array_length
= array_uctl_field
.type
.u
.legacy
.array
.length
;
307 const auto& element_uctl_type
= array_uctl_field
.type
.u
.legacy
.array
.elem_type
;
308 if (element_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
309 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
310 "Unexpected legacy array element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
311 element_uctl_type
.atype
,
312 lttng_ust_ctl_atype_integer
));
316 create_integer_type_from_ust_ctl_basic_type(element_uctl_type
, session_attributes
);
317 if (element_uctl_type
.atype
== lttng_ust_ctl_atype_integer
&&
318 element_uctl_type
.u
.basic
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
319 /* Element represents a text character. */
320 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
321 element_uctl_type
.u
.basic
.integer
.encoding
);
324 *next_ust_ctl_field
= current
+ 1;
326 if (element_encoding
) {
327 const auto integer_element_size
=
328 static_cast<const lst::integer_type
&>(*element_type
).size
;
330 if (integer_element_size
!= 8) {
331 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
332 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
333 integer_element_size
));
336 /* Array is a static-length string. */
337 return lttng::make_unique
<lst::static_length_string_type
>(
338 array_alignment
, *element_encoding
, array_length
);
341 return lttng::make_unique
<lst::static_length_array_type
>(
342 array_alignment
, std::move(element_type
), array_length
);
345 lst::type::cuptr
create_array_nestable_type_from_ust_ctl_fields(
346 const lttng_ust_ctl_field
*current
,
347 const lttng_ust_ctl_field
*end
,
348 const session_attributes
& session_attributes
,
349 const lttng_ust_ctl_field
**next_ust_ctl_field
,
350 const publish_field_fn
& publish_field
,
351 const lookup_field_fn
& lookup_field
,
352 lst::field_location::root lookup_root
,
353 lst::field_location::elements
& current_field_location_elements
,
354 lsu::ctl_field_quirks quirks
)
356 if (current
>= end
) {
357 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
358 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
361 const auto& array_uctl_field
= *current
;
362 uint32_t array_alignment
, array_length
;
363 lst::type::cuptr element_type
;
364 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
366 array_length
= array_uctl_field
.type
.u
.array_nestable
.length
;
367 array_alignment
= array_uctl_field
.type
.u
.array_nestable
.alignment
;
369 /* Nestable array fields are followed by their element type. */
370 const auto& element_uctl_field
= *(current
+ 1);
372 /* next_ust_ctl_field is updated as needed. */
373 element_type
= create_type_from_ust_ctl_fields(&element_uctl_field
,
380 current_field_location_elements
,
382 if (element_uctl_field
.type
.atype
== lttng_ust_ctl_atype_integer
&&
383 element_uctl_field
.type
.u
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
384 /* Element represents a text character. */
385 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
386 element_uctl_field
.type
.u
.integer
.encoding
);
389 if (element_encoding
) {
390 const auto integer_element_size
=
391 static_cast<const lst::integer_type
&>(*element_type
).size
;
393 if (integer_element_size
!= 8) {
394 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
395 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
396 integer_element_size
));
399 /* Array is a static-length string. */
400 return lttng::make_unique
<lst::static_length_string_type
>(
401 array_alignment
, *element_encoding
, array_length
);
404 return lttng::make_unique
<lst::static_length_array_type
>(
405 array_alignment
, std::move(element_type
), array_length
);
409 * For legacy sequence types, LTTng-UST expresses both the sequence and sequence
410 * length as part of the same lttng_ust_ctl_field entry.
412 lst::type::cuptr
create_sequence_type_from_ust_ctl_fields(
413 const lttng_ust_ctl_field
*current
,
414 const lttng_ust_ctl_field
*end
,
415 const session_attributes
& session_attributes
,
416 const lttng_ust_ctl_field
**next_ust_ctl_field
,
417 const publish_field_fn
& publish_field
,
418 lst::field_location::root lookup_root
,
419 lst::field_location::elements
& current_field_location_elements
,
420 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
422 if (current
>= end
) {
423 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
424 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
427 const auto& sequence_uctl_field
= *current
;
428 const auto& element_uctl_type
= sequence_uctl_field
.type
.u
.legacy
.sequence
.elem_type
;
429 const auto& length_uctl_type
= sequence_uctl_field
.type
.u
.legacy
.sequence
.length_type
;
430 const auto sequence_alignment
= 0U;
432 if (element_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
433 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
434 "Unexpected legacy sequence element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
435 element_uctl_type
.atype
,
436 lttng_ust_ctl_atype_integer
));
439 if (length_uctl_type
.atype
!= lttng_ust_ctl_atype_integer
) {
440 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
441 "Unexpected legacy sequence length field type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
442 length_uctl_type
.atype
,
443 lttng_ust_ctl_atype_integer
));
446 nonstd::optional
<enum lst::string_type::encoding
> element_encoding
;
447 if (element_uctl_type
.atype
== lttng_ust_ctl_atype_integer
&&
448 element_uctl_type
.u
.basic
.integer
.encoding
!= lttng_ust_ctl_encode_none
) {
449 /* Element represents a text character. */
450 element_encoding
= ust_ctl_encoding_to_string_field_encoding(
451 element_uctl_type
.u
.basic
.integer
.encoding
);
454 auto length_field_name
= fmt::format("_{}_length", sequence_uctl_field
.name
);
456 create_integer_type_from_ust_ctl_basic_type(element_uctl_type
, session_attributes
);
458 create_integer_type_from_ust_ctl_basic_type(length_uctl_type
, session_attributes
);
460 lst::field_location::elements length_field_location_elements
=
461 current_field_location_elements
;
462 length_field_location_elements
.emplace_back(length_field_name
);
464 lst::field_location length_field_location
{ lookup_root
,
465 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 const publish_field_fn
& publish_field
,
498 const 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(length_field_name
);
544 lst::field_location length_field_location
{ lookup_root
,
545 std::move(length_field_location_elements
) };
547 /* Validate existence of length field (throws if not found). */
548 const auto& length_field
= lookup_field(length_field_location
);
549 const auto *integer_selector_field
=
550 dynamic_cast<const lst::integer_type
*>(&length_field
.get_type());
551 if (!integer_selector_field
) {
552 LTTNG_THROW_PROTOCOL_ERROR(
553 "Invalid selector field type referenced from sequence: expected integer or enumeration");
556 if (element_encoding
) {
557 const auto integer_element_size
=
558 static_cast<const lst::integer_type
&>(*element_type
).size
;
560 if (integer_element_size
!= 8) {
561 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
562 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
563 integer_element_size
));
566 /* Sqeuence is a dynamic-length string. */
567 return lttng::make_unique
<lst::dynamic_length_string_type
>(
568 sequence_alignment
, *element_encoding
, std::move(length_field_location
));
571 return lttng::make_unique
<lst::dynamic_length_array_type
>(
572 sequence_alignment
, std::move(element_type
), std::move(length_field_location
));
576 create_structure_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
577 const lttng_ust_ctl_field
*end
,
578 const session_attributes
& session_attributes
579 __attribute__((unused
)),
580 const lttng_ust_ctl_field
**next_ust_ctl_field
,
581 lsu::ctl_field_quirks quirks
__attribute__((unused
)))
583 if (current
>= end
) {
584 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
585 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
588 uint32_t field_count
;
590 const auto& structure_uctl_field
= *current
;
592 if (structure_uctl_field
.type
.atype
== lttng_ust_ctl_atype_struct
) {
593 field_count
= structure_uctl_field
.type
.u
.legacy
._struct
.nr_fields
;
596 field_count
= structure_uctl_field
.type
.u
.struct_nestable
.nr_fields
;
597 alignment
= structure_uctl_field
.type
.u
.struct_nestable
.alignment
;
600 if (field_count
!= 0) {
601 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
602 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
606 *next_ust_ctl_field
= current
+ 1;
607 return lttng::make_unique
<lst::structure_type
>(alignment
, lst::structure_type::fields());
610 template <class VariantSelectorMappingIntegerType
>
611 typename
lst::variant_type
<VariantSelectorMappingIntegerType
>::choices
612 create_typed_variant_choices(const lttng_ust_ctl_field
*current
,
613 const lttng_ust_ctl_field
*end
,
614 const session_attributes
& session_attributes
,
615 const lttng_ust_ctl_field
**next_ust_ctl_field
,
616 lookup_field_fn lookup_field
,
617 lst::field_location::root lookup_root
,
618 lst::field_location::elements
& current_field_location_elements
,
619 unsigned int choice_count
,
620 const lst::field
& selector_field
,
621 lsu::ctl_field_quirks quirks
)
623 typename
lst::variant_type
<VariantSelectorMappingIntegerType
>::choices choices
;
624 const auto& typed_enumeration
=
625 static_cast<const lst::typed_enumeration_type
<VariantSelectorMappingIntegerType
>&>(
626 selector_field
.get_type());
628 for (unsigned int i
= 0; i
< choice_count
; i
++) {
629 create_field_from_ust_ctl_fields(
634 [&choices
, typed_enumeration
, &selector_field
, quirks
](
635 lst::field::uptr field
) {
637 * Find the enumeration mapping that matches the
640 const auto mapping_it
= std::find_if(
641 typed_enumeration
.mappings_
->begin(),
642 typed_enumeration
.mappings_
->end(),
644 quirks
](const typename
std::remove_reference
<
645 decltype(typed_enumeration
)>::type::mapping
&
647 if (static_cast<bool>(
649 lsu::ctl_field_quirks::
650 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS
)) {
652 * Check if they match with
653 * a prepended underscore
654 * and, if not, perform the
657 if ((std::string("_") + field
->name
) ==
663 return mapping
.name
== field
->name
;
666 if (mapping_it
== typed_enumeration
.mappings_
->end()) {
667 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
668 "Invalid variant choice: `{}` does not match any mapping in `{}` enumeration",
670 selector_field
.name
));
673 choices
.emplace_back(*mapping_it
, field
->move_type());
677 current_field_location_elements
,
680 current
= *next_ust_ctl_field
;
686 lst::type::cuptr
create_variant_field_from_ust_ctl_fields(
687 const lttng_ust_ctl_field
*current
,
688 const lttng_ust_ctl_field
*end
,
689 const session_attributes
& session_attributes
,
690 const lttng_ust_ctl_field
**next_ust_ctl_field
,
691 const lookup_field_fn
& lookup_field
,
692 lst::field_location::root lookup_root
,
693 lst::field_location::elements
& current_field_location_elements
,
694 lsu::ctl_field_quirks quirks
)
696 if (current
>= end
) {
697 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
698 "End of {} array reached unexpectedly during decoding", typeid(*current
)));
701 const auto& variant_uctl_field
= *current
;
705 uint32_t choice_count
;
706 const char *tag_name
;
708 if (variant_uctl_field
.type
.atype
== lttng_ust_ctl_atype_variant
) {
710 choice_count
= variant_uctl_field
.type
.u
.legacy
.variant
.nr_choices
;
711 tag_name
= variant_uctl_field
.type
.u
.legacy
.variant
.tag_name
;
713 alignment
= variant_uctl_field
.type
.u
.variant_nestable
.alignment
;
714 choice_count
= variant_uctl_field
.type
.u
.variant_nestable
.nr_choices
;
715 tag_name
= variant_uctl_field
.type
.u
.variant_nestable
.tag_name
;
718 lst::field_location::elements selector_field_location_elements
=
719 current_field_location_elements
;
720 selector_field_location_elements
.emplace_back(tag_name
);
722 lst::field_location selector_field_location
{ lookup_root
,
723 std::move(selector_field_location_elements
) };
725 /* Validate existence of selector field (throws if not found). */
726 const auto& selector_field
= lookup_field(selector_field_location
);
727 const auto *enumeration_selector_type
=
728 dynamic_cast<const lst::enumeration_type
*>(&selector_field
.get_type());
729 if (!enumeration_selector_type
) {
730 LTTNG_THROW_PROTOCOL_ERROR(
731 "Invalid selector field type referenced from variant: expected enumeration");
734 const bool selector_is_signed
= enumeration_selector_type
->signedness_
==
735 lst::integer_type::signedness::SIGNED
;
737 /* Choices follow. next_ust_ctl_field is updated as needed. */
738 if (selector_is_signed
) {
739 lst::variant_type
<lst::signed_enumeration_type::mapping::range_t::range_integer_t
>::
740 choices choices
= create_typed_variant_choices
<int64_t>(
747 current_field_location_elements
,
752 return lttng::make_unique
<lst::variant_type
<int64_t>>(
753 alignment
, std::move(selector_field_location
), std::move(choices
));
756 lst::unsigned_enumeration_type::mapping::range_t::range_integer_t
>::choices
757 choices
= create_typed_variant_choices
<uint64_t>(
764 current_field_location_elements
,
769 return lttng::make_unique
<lst::variant_type
<uint64_t>>(
770 alignment
, std::move(selector_field_location
), std::move(choices
));
775 create_type_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
776 const lttng_ust_ctl_field
*end
,
777 const session_attributes
& session_attributes
,
778 const lttng_ust_ctl_field
**next_ust_ctl_field
,
779 const publish_field_fn
& publish_field
,
780 const lookup_field_fn
& lookup_field
,
781 lst::field_location::root lookup_root
,
782 lst::field_location::elements
& current_field_location_elements
,
783 lsu::ctl_field_quirks quirks
)
785 switch (current
->type
.atype
) {
786 case lttng_ust_ctl_atype_integer
:
787 return create_integer_type_from_ust_ctl_fields(
788 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
789 case lttng_ust_ctl_atype_enum
:
790 case lttng_ust_ctl_atype_enum_nestable
:
791 return create_enumeration_type_from_ust_ctl_fields(
792 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
793 case lttng_ust_ctl_atype_float
:
794 return create_floating_point_type_from_ust_ctl_fields(
795 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
796 case lttng_ust_ctl_atype_string
:
797 return create_string_type_from_ust_ctl_fields(
798 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
799 case lttng_ust_ctl_atype_array
:
800 return create_array_type_from_ust_ctl_fields(
801 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
802 case lttng_ust_ctl_atype_array_nestable
:
803 return create_array_nestable_type_from_ust_ctl_fields(
811 current_field_location_elements
,
813 case lttng_ust_ctl_atype_sequence
:
814 return create_sequence_type_from_ust_ctl_fields(current
,
820 current_field_location_elements
,
822 case lttng_ust_ctl_atype_sequence_nestable
:
823 return create_sequence_nestable_type_from_ust_ctl_fields(
831 current_field_location_elements
,
833 case lttng_ust_ctl_atype_struct
:
834 case lttng_ust_ctl_atype_struct_nestable
:
835 return create_structure_field_from_ust_ctl_fields(
836 current
, end
, session_attributes
, next_ust_ctl_field
, quirks
);
837 case lttng_ust_ctl_atype_variant
:
838 case lttng_ust_ctl_atype_variant_nestable
:
839 return create_variant_field_from_ust_ctl_fields(current
,
845 current_field_location_elements
,
848 LTTNG_THROW_PROTOCOL_ERROR(
849 fmt::format("Unknown {} value `{}` encountered while converting {} to {}",
850 typeid(current
->type
.atype
),
853 typeid(lst::type::cuptr::element_type
)));
857 void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field
*current
,
858 const lttng_ust_ctl_field
*end
,
859 const session_attributes
& session_attributes
,
860 const lttng_ust_ctl_field
**next_ust_ctl_field
,
861 const publish_field_fn
& publish_field
,
862 const lookup_field_fn
& lookup_field
,
863 lst::field_location::root lookup_root
,
864 lst::field_location::elements
& current_field_location_elements
,
865 lsu::ctl_field_quirks quirks
)
867 LTTNG_ASSERT(current
< end
);
869 if (lttng_strnlen(current
->name
, sizeof(current
->name
)) == sizeof(current
->name
)) {
870 LTTNG_THROW_PROTOCOL_ERROR(
871 fmt::format("Name of {} is not null-terminated", typeid(*current
)));
874 publish_field(lttng::make_unique
<lst::field
>(
876 create_type_from_ust_ctl_fields(current
,
883 current_field_location_elements
,
887 std::vector
<lst::field::cuptr
>::iterator
888 lookup_field_in_vector(std::vector
<lst::field::cuptr
>& fields
, const lst::field_location
& location
)
890 if (location
.elements_
.size() != 1) {
891 LTTNG_THROW_ERROR(fmt::format(
892 "Unexpected field location received during field look-up: location = {}",
897 * In the context of fields received from LTTng-UST, field
898 * look-up is extremely naive as the protocol can only
899 * express empty structures. It is safe to assume that
900 * location has a depth of 1 and directly refers to a field
901 * in the 'fields' vector.
903 const auto field_it
=
904 std::find_if(fields
.begin(), fields
.end(), [location
](lst::field::cuptr
& field
) {
905 return field
->name
== location
.elements_
[0];
908 if (field_it
== fields
.end()) {
909 LTTNG_THROW_PROTOCOL_ERROR(
910 fmt::format("Failed to look-up field: location = {}", location
));
917 * `lttng_ust_ctl_field`s can be nested, in which case creating a field will consume
918 * more than one lttng_ust_ctl_field. create_field_from_ust_ctl_fields returns the
919 * position of the next lttng_ust_ctl_field to consume or `end` when the last field
922 * Always returns a new field, throws on error.
924 std::vector
<lst::field::cuptr
>
925 create_fields_from_ust_ctl_fields(const lsu::registry_session
& session
,
926 const lttng_ust_ctl_field
*current
,
927 const lttng_ust_ctl_field
*end
,
928 lst::field_location::root lookup_root
,
929 lsu::ctl_field_quirks quirks
)
931 std::vector
<lst::field::cuptr
> fields
;
932 const auto trace_native_byte_order
= session
.abi
.byte_order
;
933 const session_attributes session_attributes
{
934 [&session
](const char *enum_name
, uint64_t enum_id
) {
935 return session
.enumeration(enum_name
, enum_id
);
937 trace_native_byte_order
939 /* Location of field being created. */
940 lst::field_location::elements current_field_location_elements
;
942 while (current
< end
) {
943 auto *next_field
= current
;
946 * create_field_from_ust_ctl_fields will consume one field at a time.
947 * However, some fields expressed by LTTng-UST's protocol are expended
948 * to multiple event fields (legacy sequence fields implicitly define
949 * their length field).
951 * The lambda allows the factory functions to push as many fields as
952 * needed depending on the decoded field's type.
954 create_field_from_ust_ctl_fields(
959 [&fields
](lst::field::cuptr field
) {
960 /* Publishing a field simply adds it to the converted
962 fields
.emplace_back(std::move(field
));
964 [&fields
](const lst::field_location
& location
)
965 -> lookup_field_fn::result_type
{
966 /* Resolve location to a previously-constructed field. */
967 return **lookup_field_in_vector(fields
, location
);
970 current_field_location_elements
,
973 current
= next_field
;
980 std::vector
<lst::field::cuptr
>
981 lsu::create_trace_fields_from_ust_ctl_fields(const lsu::registry_session
& session
,
982 const lttng_ust_ctl_field
*fields
,
983 std::size_t field_count
,
984 lst::field_location::root lookup_root
,
985 lsu::ctl_field_quirks quirks
)
987 return create_fields_from_ust_ctl_fields(
988 session
, fields
, fields
+ field_count
, lookup_root
, quirks
);