Run clang-format on the whole tree
[lttng-tools.git] / src / bin / lttng-sessiond / ust-field-convert.cpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "ust-field-convert.hpp"
9
10 #include <common/exception.hpp>
11 #include <common/make-unique.hpp>
12
13 #include <unordered_map>
14
15 namespace lst = lttng::sessiond::trace;
16 namespace lsu = lttng::sessiond::ust;
17 namespace {
18
19 /*
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
22 * an event.
23 */
24 class session_attributes {
25 public:
26 using registry_enum_getter_fn =
27 std::function<lsu::registry_enum::const_rcu_protected_reference(const char *name,
28 uint64_t id)>;
29
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 }
34 {
35 }
36
37 const registry_enum_getter_fn get_registry_enum;
38 const lst::byte_order _native_trace_byte_order;
39 };
40
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)>;
43
44 /* Look-up field from a field location. */
45 using lookup_field_fn = std::function<const lst::field&(const lst::field_location&)>;
46
47 lst::type::cuptr
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);
57
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);
67
68 template <class UstCtlEncodingType>
69 enum lst::null_terminated_string_type::encoding
70 ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
71 {
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 },
79 };
80
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",
85 encoding));
86 }
87
88 return encoding_it->second;
89 }
90
91 template <class UstCtlBaseType>
92 enum lst::integer_type::base ust_ctl_base_to_integer_field_base(UstCtlBaseType base)
93 {
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 } };
99
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",
104 base));
105 }
106
107 return base_it->second;
108 }
109
110 lst::type::cuptr
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)))
116 {
117 if (current >= end) {
118 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
119 "End of {} array reached unexpectedly during decoding", typeid(*current)));
120 }
121
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;
129
130 *next_ust_ctl_field = current + 1;
131
132 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
133 byte_order,
134 current->type.u.integer.size,
135 signedness,
136 base);
137 }
138
139 lst::type::cuptr
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)))
145 {
146 if (current >= end) {
147 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
148 "End of {} array reached unexpectedly during decoding", typeid(*current)));
149 }
150
151 *next_ust_ctl_field = current + 1;
152
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;
156
157 try {
158 return lttng::make_unique<const lst::floating_point_type>(
159 current->type.u._float.alignment,
160 byte_order,
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()));
166 }
167 }
168
169 lst::type::cuptr
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)))
175 {
176 if (current >= end) {
177 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
178 "End of {} array reached unexpectedly during decoding", typeid(*current)));
179 }
180
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 &current->type.u.legacy.basic.enumeration.container_type;
186
187 if (enum_uctl_field.type.atype == lttng_ust_ctl_atype_enum_nestable) {
188 /* Nestable enumeration fields are followed by their container type. */
189 ++current;
190 if (current >= end) {
191 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
192 "Array of {} is too short to contain nestable enumeration's container",
193 typeid(*current)));
194 }
195
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));
200 }
201
202 enum_container_uctl_type = &current->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;
205 } else {
206 enumeration_id = enum_uctl_field.type.u.legacy.basic.enumeration.id;
207 enumeration_name = enum_uctl_field.type.u.legacy.basic.enumeration.name;
208 }
209
210 *next_ust_ctl_field = current + 1;
211
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;
219
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));
223
224 return lttng::make_unique<const lst::signed_enumeration_type>(
225 enum_container_uctl_type->alignment,
226 byte_order,
227 enum_container_uctl_type->size,
228 base,
229 enum_registry._mappings);
230 } else {
231 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
232 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
233
234 return lttng::make_unique<const lst::unsigned_enumeration_type>(
235 enum_container_uctl_type->alignment,
236 byte_order,
237 enum_container_uctl_type->size,
238 base,
239 enum_registry._mappings);
240 }
241 }
242
243 lst::type::cuptr
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)))
250 {
251 if (current >= end) {
252 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
253 "End of {} array reached unexpectedly during decoding", typeid(*current)));
254 }
255
256 const auto& string_uctl_field = *current;
257 *next_ust_ctl_field = current + 1;
258
259 const auto encoding =
260 ust_ctl_encoding_to_string_field_encoding(string_uctl_field.type.u.string.encoding);
261
262 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
263 }
264
265 lst::type::cuptr
266 create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type& type,
267 const session_attributes& session_attributes)
268 {
269 /* Checked by caller. */
270 LTTNG_ASSERT(type.atype == lttng_ust_ctl_atype_integer);
271
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;
281
282 return lttng::make_unique<const lst::integer_type>(
283 alignment, byte_order, size, signedness, base);
284 }
285
286 lst::type::cuptr
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)))
292 {
293 if (current >= end) {
294 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
295 "End of {} array reached unexpectedly during decoding", typeid(*current)));
296 }
297
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;
302
303 array_length = array_uctl_field.type.u.legacy.array.length;
304 array_alignment = 0;
305
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));
312 }
313
314 element_type =
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);
321 }
322
323 *next_ust_ctl_field = current + 1;
324
325 if (element_encoding) {
326 const auto integer_element_size =
327 static_cast<const lst::integer_type&>(*element_type).size;
328
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));
333 }
334
335 /* Array is a static-length string. */
336 return lttng::make_unique<lst::static_length_string_type>(
337 array_alignment, *element_encoding, array_length);
338 }
339
340 return lttng::make_unique<lst::static_length_array_type>(
341 array_alignment, std::move(element_type), array_length);
342 }
343
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)
354 {
355 if (current >= end) {
356 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
357 "End of {} array reached unexpectedly during decoding", typeid(*current)));
358 }
359
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;
364
365 array_length = array_uctl_field.type.u.array_nestable.length;
366 array_alignment = array_uctl_field.type.u.array_nestable.alignment;
367
368 /* Nestable array fields are followed by their element type. */
369 const auto& element_uctl_field = *(current + 1);
370
371 /* next_ust_ctl_field is updated as needed. */
372 element_type = create_type_from_ust_ctl_fields(&element_uctl_field,
373 end,
374 session_attributes,
375 next_ust_ctl_field,
376 publish_field,
377 lookup_field,
378 lookup_root,
379 current_field_location_elements,
380 quirks);
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);
386 }
387
388 if (element_encoding) {
389 const auto integer_element_size =
390 static_cast<const lst::integer_type&>(*element_type).size;
391
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));
396 }
397
398 /* Array is a static-length string. */
399 return lttng::make_unique<lst::static_length_string_type>(
400 array_alignment, *element_encoding, array_length);
401 }
402
403 return lttng::make_unique<lst::static_length_array_type>(
404 array_alignment, std::move(element_type), array_length);
405 }
406
407 /*
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.
410 */
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)))
420 {
421 if (current >= end) {
422 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
423 "End of {} array reached unexpectedly during decoding", typeid(*current)));
424 }
425
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;
430
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));
436 }
437
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));
443 }
444
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);
451 }
452
453 const auto length_field_name = fmt::format("_{}_length", sequence_uctl_field.name);
454 auto element_type =
455 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
456 auto length_type =
457 create_integer_type_from_ust_ctl_basic_type(length_uctl_type, session_attributes);
458
459 lst::field_location::elements length_field_location_elements =
460 current_field_location_elements;
461 length_field_location_elements.emplace_back(length_field_name);
462
463 const lst::field_location length_field_location{
464 lookup_root, std::move(length_field_location_elements)
465 };
466
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)));
470
471 *next_ust_ctl_field = current + 1;
472
473 if (element_encoding) {
474 const auto integer_element_size =
475 static_cast<const lst::integer_type&>(*element_type).size;
476
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));
481 }
482
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));
486 }
487
488 return lttng::make_unique<lst::dynamic_length_array_type>(
489 sequence_alignment, std::move(element_type), std::move(length_field_location));
490 }
491
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)
502 {
503 if (current >= end) {
504 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
505 "End of {} array reached unexpectedly during decoding", typeid(*current)));
506 }
507
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;
511
512 /* Nestable sequence fields are followed by their element type. */
513 const auto& element_uctl_field = *(current + 1);
514
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);
521 }
522
523 /* next_ust_ctl_field is updated as needed. */
524 auto element_type = create_type_from_ust_ctl_fields(&element_uctl_field,
525 end,
526 session_attributes,
527 next_ust_ctl_field,
528 publish_field,
529 lookup_field,
530 lookup_root,
531 current_field_location_elements,
532 quirks);
533
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");
538 }
539
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));
543
544 const lst::field_location length_field_location{
545 lookup_root, std::move(length_field_location_elements)
546 };
547
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");
555 }
556
557 if (element_encoding) {
558 const auto integer_element_size =
559 static_cast<const lst::integer_type&>(*element_type).size;
560
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));
565 }
566
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));
570 }
571
572 return lttng::make_unique<lst::dynamic_length_array_type>(
573 sequence_alignment, std::move(element_type), std::move(length_field_location));
574 }
575
576 lst::type::cuptr
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)))
583 {
584 if (current >= end) {
585 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
586 "End of {} array reached unexpectedly during decoding", typeid(*current)));
587 }
588
589 uint32_t field_count;
590 uint32_t alignment;
591 const auto& structure_uctl_field = *current;
592
593 if (structure_uctl_field.type.atype == lttng_ust_ctl_atype_struct) {
594 field_count = structure_uctl_field.type.u.legacy._struct.nr_fields;
595 alignment = 0;
596 } else {
597 field_count = structure_uctl_field.type.u.struct_nestable.nr_fields;
598 alignment = structure_uctl_field.type.u.struct_nestable.alignment;
599 }
600
601 if (field_count != 0) {
602 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
603 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
604 field_count));
605 }
606
607 *next_ust_ctl_field = current + 1;
608 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
609 }
610
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)
623 {
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());
628
629 for (unsigned int i = 0; i < choice_count; i++) {
630 create_field_from_ust_ctl_fields(
631 current,
632 end,
633 session_attributes,
634 next_ust_ctl_field,
635 [&choices, typed_enumeration, &selector_field, quirks](
636 lst::field::uptr field) {
637 /*
638 * Find the enumeration mapping that matches the
639 * field's name.
640 */
641 const auto mapping_it = std::find_if(
642 typed_enumeration.mappings_->begin(),
643 typed_enumeration.mappings_->end(),
644 [&field,
645 quirks](const typename std::remove_reference<
646 decltype(typed_enumeration)>::type::mapping&
647 mapping) {
648 if (static_cast<bool>(
649 quirks &
650 lsu::ctl_field_quirks::
651 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS)) {
652 /*
653 * Check if they match with
654 * a prepended underscore
655 * and, if not, perform the
656 * regular check.
657 */
658 if ((std::string("_") + field->name) ==
659 mapping.name) {
660 return true;
661 }
662 }
663
664 return mapping.name == field->name;
665 });
666
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",
670 field->name,
671 selector_field.name));
672 }
673
674 choices.emplace_back(*mapping_it, field->move_type());
675 },
676 lookup_field,
677 lookup_root,
678 current_field_location_elements,
679 quirks);
680
681 current = *next_ust_ctl_field;
682 }
683
684 return choices;
685 }
686
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)
696 {
697 if (current >= end) {
698 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
699 "End of {} array reached unexpectedly during decoding", typeid(*current)));
700 }
701
702 const auto& variant_uctl_field = *current;
703 current++;
704
705 uint32_t alignment;
706 uint32_t choice_count;
707 const char *tag_name;
708
709 if (variant_uctl_field.type.atype == lttng_ust_ctl_atype_variant) {
710 alignment = 0;
711 choice_count = variant_uctl_field.type.u.legacy.variant.nr_choices;
712 tag_name = variant_uctl_field.type.u.legacy.variant.tag_name;
713 } else {
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;
717 }
718
719 lst::field_location::elements selector_field_location_elements =
720 current_field_location_elements;
721 selector_field_location_elements.emplace_back(tag_name);
722
723 const lst::field_location selector_field_location{
724 lookup_root, std::move(selector_field_location_elements)
725 };
726
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");
734 }
735
736 const bool selector_is_signed = enumeration_selector_type->signedness_ ==
737 lst::integer_type::signedness::SIGNED;
738
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>(
743 current,
744 end,
745 session_attributes,
746 next_ust_ctl_field,
747 lookup_field,
748 lookup_root,
749 current_field_location_elements,
750 choice_count,
751 selector_field,
752 quirks);
753
754 return lttng::make_unique<lst::variant_type<int64_t>>(
755 alignment, std::move(selector_field_location), std::move(choices));
756 } else {
757 lst::variant_type<
758 lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::choices
759 choices = create_typed_variant_choices<uint64_t>(
760 current,
761 end,
762 session_attributes,
763 next_ust_ctl_field,
764 lookup_field,
765 lookup_root,
766 current_field_location_elements,
767 choice_count,
768 selector_field,
769 quirks);
770
771 return lttng::make_unique<lst::variant_type<uint64_t>>(
772 alignment, std::move(selector_field_location), std::move(choices));
773 }
774 }
775
776 lst::type::cuptr
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)
786 {
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(
806 current,
807 end,
808 session_attributes,
809 next_ust_ctl_field,
810 publish_field,
811 lookup_field,
812 lookup_root,
813 current_field_location_elements,
814 quirks);
815 case lttng_ust_ctl_atype_sequence:
816 return create_sequence_type_from_ust_ctl_fields(current,
817 end,
818 session_attributes,
819 next_ust_ctl_field,
820 publish_field,
821 lookup_root,
822 current_field_location_elements,
823 quirks);
824 case lttng_ust_ctl_atype_sequence_nestable:
825 return create_sequence_nestable_type_from_ust_ctl_fields(
826 current,
827 end,
828 session_attributes,
829 next_ust_ctl_field,
830 publish_field,
831 lookup_field,
832 lookup_root,
833 current_field_location_elements,
834 quirks);
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,
842 end,
843 session_attributes,
844 next_ust_ctl_field,
845 lookup_field,
846 lookup_root,
847 current_field_location_elements,
848 quirks);
849 default:
850 LTTNG_THROW_PROTOCOL_ERROR(
851 fmt::format("Unknown {} value `{}` encountered while converting {} to {}",
852 typeid(current->type.atype),
853 current->type.atype,
854 typeid(*current),
855 typeid(lst::type::cuptr::element_type)));
856 }
857 }
858
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)
868 {
869 LTTNG_ASSERT(current < end);
870
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)));
874 }
875
876 publish_field(lttng::make_unique<lst::field>(
877 current->name,
878 create_type_from_ust_ctl_fields(current,
879 end,
880 session_attributes,
881 next_ust_ctl_field,
882 publish_field,
883 lookup_field,
884 lookup_root,
885 current_field_location_elements,
886 quirks)));
887 }
888
889 std::vector<lst::field::cuptr>::iterator
890 lookup_field_in_vector(std::vector<lst::field::cuptr>& fields, const lst::field_location& location)
891 {
892 if (location.elements_.size() != 1) {
893 LTTNG_THROW_ERROR(fmt::format(
894 "Unexpected field location received during field look-up: location = {}",
895 location));
896 }
897
898 /*
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.
904 */
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];
908 });
909
910 if (field_it == fields.end()) {
911 LTTNG_THROW_PROTOCOL_ERROR(
912 fmt::format("Failed to look-up field: location = {}", location));
913 }
914
915 return field_it;
916 }
917
918 /*
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
922 * is consumed.
923 *
924 * Always returns a new field, throws on error.
925 */
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)
932 {
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);
938 },
939 trace_native_byte_order
940 };
941 /* Location of field being created. */
942 lst::field_location::elements current_field_location_elements;
943
944 while (current < end) {
945 auto *next_field = current;
946
947 /*
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).
952 *
953 * The lambda allows the factory functions to push as many fields as
954 * needed depending on the decoded field's type.
955 */
956 create_field_from_ust_ctl_fields(
957 current,
958 end,
959 session_attributes,
960 &next_field,
961 [&fields](lst::field::cuptr field) {
962 /* Publishing a field simply adds it to the converted
963 * fields. */
964 fields.emplace_back(std::move(field));
965 },
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);
970 },
971 lookup_root,
972 current_field_location_elements,
973 quirks);
974
975 current = next_field;
976 }
977
978 return fields;
979 }
980 } /* namespace */
981
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)
988 {
989 return create_fields_from_ust_ctl_fields(
990 session, fields, fields + field_count, lookup_root, quirks);
991 }
This page took 0.059933 seconds and 4 git commands to generate.