Tests: Add test to check shared-memory FD leaks after relayd dies
[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 #include <utility>
15
16 namespace lst = lttng::sessiond::trace;
17 namespace lsu = lttng::sessiond::ust;
18 namespace {
19
20 /*
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
23 * an event.
24 */
25 class session_attributes {
26 public:
27 using registry_enum_getter_fn =
28 std::function<lsu::registry_enum::const_rcu_protected_reference(const char *name,
29 uint64_t id)>;
30
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 }
35 {
36 }
37
38 const registry_enum_getter_fn get_registry_enum;
39 const lst::byte_order _native_trace_byte_order;
40 };
41
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)>;
44
45 /* Look-up field from a field location. */
46 using lookup_field_fn = std::function<const lst::field&(const lst::field_location&)>;
47
48 lst::type::cuptr
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);
58
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);
68
69 template <class UstCtlEncodingType>
70 enum lst::null_terminated_string_type::encoding
71 ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
72 {
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 },
80 };
81
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",
86 encoding));
87 }
88
89 return encoding_it->second;
90 }
91
92 template <class UstCtlBaseType>
93 enum lst::integer_type::base ust_ctl_base_to_integer_field_base(UstCtlBaseType base)
94 {
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 } };
100
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",
105 base));
106 }
107
108 return base_it->second;
109 }
110
111 lst::type::cuptr
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)))
117 {
118 if (current >= end) {
119 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
120 "End of {} array reached unexpectedly during decoding", typeid(*current)));
121 }
122
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;
130
131 *next_ust_ctl_field = current + 1;
132
133 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
134 byte_order,
135 current->type.u.integer.size,
136 signedness,
137 base);
138 }
139
140 lst::type::cuptr
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)))
146 {
147 if (current >= end) {
148 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
149 "End of {} array reached unexpectedly during decoding", typeid(*current)));
150 }
151
152 *next_ust_ctl_field = current + 1;
153
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;
157
158 try {
159 return lttng::make_unique<const lst::floating_point_type>(
160 current->type.u._float.alignment,
161 byte_order,
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()));
167 }
168 }
169
170 lst::type::cuptr
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)))
176 {
177 if (current >= end) {
178 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
179 "End of {} array reached unexpectedly during decoding", typeid(*current)));
180 }
181
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 &current->type.u.legacy.basic.enumeration.container_type;
187
188 if (enum_uctl_field.type.atype == lttng_ust_ctl_atype_enum_nestable) {
189 /* Nestable enumeration fields are followed by their container type. */
190 ++current;
191 if (current >= end) {
192 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
193 "Array of {} is too short to contain nestable enumeration's container",
194 typeid(*current)));
195 }
196
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));
201 }
202
203 enum_container_uctl_type = &current->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;
206 } else {
207 enumeration_id = enum_uctl_field.type.u.legacy.basic.enumeration.id;
208 enumeration_name = enum_uctl_field.type.u.legacy.basic.enumeration.name;
209 }
210
211 *next_ust_ctl_field = current + 1;
212
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;
220
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));
224
225 return lttng::make_unique<const lst::signed_enumeration_type>(
226 enum_container_uctl_type->alignment,
227 byte_order,
228 enum_container_uctl_type->size,
229 base,
230 enum_registry._mappings);
231 } else {
232 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
233 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
234
235 return lttng::make_unique<const lst::unsigned_enumeration_type>(
236 enum_container_uctl_type->alignment,
237 byte_order,
238 enum_container_uctl_type->size,
239 base,
240 enum_registry._mappings);
241 }
242 }
243
244 lst::type::cuptr
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)))
251 {
252 if (current >= end) {
253 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
254 "End of {} array reached unexpectedly during decoding", typeid(*current)));
255 }
256
257 const auto& string_uctl_field = *current;
258 *next_ust_ctl_field = current + 1;
259
260 const auto encoding =
261 ust_ctl_encoding_to_string_field_encoding(string_uctl_field.type.u.string.encoding);
262
263 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
264 }
265
266 lst::type::cuptr
267 create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type& type,
268 const session_attributes& session_attributes)
269 {
270 /* Checked by caller. */
271 LTTNG_ASSERT(type.atype == lttng_ust_ctl_atype_integer);
272
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;
282
283 return lttng::make_unique<const lst::integer_type>(
284 alignment, byte_order, size, signedness, base);
285 }
286
287 lst::type::cuptr
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)))
293 {
294 if (current >= end) {
295 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
296 "End of {} array reached unexpectedly during decoding", typeid(*current)));
297 }
298
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;
303
304 array_length = array_uctl_field.type.u.legacy.array.length;
305 array_alignment = 0;
306
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));
313 }
314
315 element_type =
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);
322 }
323
324 *next_ust_ctl_field = current + 1;
325
326 if (element_encoding) {
327 const auto integer_element_size =
328 static_cast<const lst::integer_type&>(*element_type).size;
329
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));
334 }
335
336 /* Array is a static-length string. */
337 return lttng::make_unique<lst::static_length_string_type>(
338 array_alignment, *element_encoding, array_length);
339 }
340
341 return lttng::make_unique<lst::static_length_array_type>(
342 array_alignment, std::move(element_type), array_length);
343 }
344
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)
355 {
356 if (current >= end) {
357 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
358 "End of {} array reached unexpectedly during decoding", typeid(*current)));
359 }
360
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;
365
366 array_length = array_uctl_field.type.u.array_nestable.length;
367 array_alignment = array_uctl_field.type.u.array_nestable.alignment;
368
369 /* Nestable array fields are followed by their element type. */
370 const auto& element_uctl_field = *(current + 1);
371
372 /* next_ust_ctl_field is updated as needed. */
373 element_type = create_type_from_ust_ctl_fields(&element_uctl_field,
374 end,
375 session_attributes,
376 next_ust_ctl_field,
377 publish_field,
378 lookup_field,
379 lookup_root,
380 current_field_location_elements,
381 quirks);
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);
387 }
388
389 if (element_encoding) {
390 const auto integer_element_size =
391 static_cast<const lst::integer_type&>(*element_type).size;
392
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));
397 }
398
399 /* Array is a static-length string. */
400 return lttng::make_unique<lst::static_length_string_type>(
401 array_alignment, *element_encoding, array_length);
402 }
403
404 return lttng::make_unique<lst::static_length_array_type>(
405 array_alignment, std::move(element_type), array_length);
406 }
407
408 /*
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.
411 */
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)))
421 {
422 if (current >= end) {
423 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
424 "End of {} array reached unexpectedly during decoding", typeid(*current)));
425 }
426
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;
431
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));
437 }
438
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));
444 }
445
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);
452 }
453
454 auto length_field_name = fmt::format("_{}_length", sequence_uctl_field.name);
455 auto element_type =
456 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
457 auto length_type =
458 create_integer_type_from_ust_ctl_basic_type(length_uctl_type, session_attributes);
459
460 lst::field_location::elements length_field_location_elements =
461 current_field_location_elements;
462 length_field_location_elements.emplace_back(length_field_name);
463
464 lst::field_location length_field_location{ lookup_root,
465 std::move(length_field_location_elements) };
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 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)
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(length_field_name);
543
544 lst::field_location length_field_location{ lookup_root,
545 std::move(length_field_location_elements) };
546
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");
554 }
555
556 if (element_encoding) {
557 const auto integer_element_size =
558 static_cast<const lst::integer_type&>(*element_type).size;
559
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));
564 }
565
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));
569 }
570
571 return lttng::make_unique<lst::dynamic_length_array_type>(
572 sequence_alignment, std::move(element_type), std::move(length_field_location));
573 }
574
575 lst::type::cuptr
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)))
582 {
583 if (current >= end) {
584 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
585 "End of {} array reached unexpectedly during decoding", typeid(*current)));
586 }
587
588 uint32_t field_count;
589 uint32_t alignment;
590 const auto& structure_uctl_field = *current;
591
592 if (structure_uctl_field.type.atype == lttng_ust_ctl_atype_struct) {
593 field_count = structure_uctl_field.type.u.legacy._struct.nr_fields;
594 alignment = 0;
595 } else {
596 field_count = structure_uctl_field.type.u.struct_nestable.nr_fields;
597 alignment = structure_uctl_field.type.u.struct_nestable.alignment;
598 }
599
600 if (field_count != 0) {
601 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
602 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
603 field_count));
604 }
605
606 *next_ust_ctl_field = current + 1;
607 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
608 }
609
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)
622 {
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());
627
628 for (unsigned int i = 0; i < choice_count; i++) {
629 create_field_from_ust_ctl_fields(
630 current,
631 end,
632 session_attributes,
633 next_ust_ctl_field,
634 [&choices, &typed_enumeration, &selector_field, quirks](
635 lst::field::uptr field) {
636 /*
637 * Find the enumeration mapping that matches the
638 * field's name.
639 */
640 const auto mapping_it = std::find_if(
641 typed_enumeration.mappings_->begin(),
642 typed_enumeration.mappings_->end(),
643 [&field,
644 quirks](const typename std::remove_reference<
645 decltype(typed_enumeration)>::type::mapping&
646 mapping) {
647 if (static_cast<bool>(
648 quirks &
649 lsu::ctl_field_quirks::
650 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS)) {
651 /*
652 * Check if they match with
653 * a prepended underscore
654 * and, if not, perform the
655 * regular check.
656 */
657 if ((std::string("_") + field->name) ==
658 mapping.name) {
659 return true;
660 }
661 }
662
663 return mapping.name == field->name;
664 });
665
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",
669 field->name,
670 selector_field.name));
671 }
672
673 choices.emplace_back(*mapping_it, field->move_type());
674 },
675 lookup_field,
676 lookup_root,
677 current_field_location_elements,
678 quirks);
679
680 current = *next_ust_ctl_field;
681 }
682
683 return choices;
684 }
685
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)
695 {
696 if (current >= end) {
697 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
698 "End of {} array reached unexpectedly during decoding", typeid(*current)));
699 }
700
701 const auto& variant_uctl_field = *current;
702 current++;
703
704 uint32_t alignment;
705 uint32_t choice_count;
706 const char *tag_name;
707
708 if (variant_uctl_field.type.atype == lttng_ust_ctl_atype_variant) {
709 alignment = 0;
710 choice_count = variant_uctl_field.type.u.legacy.variant.nr_choices;
711 tag_name = variant_uctl_field.type.u.legacy.variant.tag_name;
712 } else {
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;
716 }
717
718 lst::field_location::elements selector_field_location_elements =
719 current_field_location_elements;
720 selector_field_location_elements.emplace_back(tag_name);
721
722 lst::field_location selector_field_location{ lookup_root,
723 std::move(selector_field_location_elements) };
724
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");
732 }
733
734 const bool selector_is_signed = enumeration_selector_type->signedness_ ==
735 lst::integer_type::signedness::SIGNED;
736
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>(
741 current,
742 end,
743 session_attributes,
744 next_ust_ctl_field,
745 lookup_field,
746 lookup_root,
747 current_field_location_elements,
748 choice_count,
749 selector_field,
750 quirks);
751
752 return lttng::make_unique<lst::variant_type<int64_t>>(
753 alignment, std::move(selector_field_location), std::move(choices));
754 } else {
755 lst::variant_type<
756 lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::choices
757 choices = create_typed_variant_choices<uint64_t>(
758 current,
759 end,
760 session_attributes,
761 next_ust_ctl_field,
762 lookup_field,
763 lookup_root,
764 current_field_location_elements,
765 choice_count,
766 selector_field,
767 quirks);
768
769 return lttng::make_unique<lst::variant_type<uint64_t>>(
770 alignment, std::move(selector_field_location), std::move(choices));
771 }
772 }
773
774 lst::type::cuptr
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)
784 {
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(
804 current,
805 end,
806 session_attributes,
807 next_ust_ctl_field,
808 publish_field,
809 lookup_field,
810 lookup_root,
811 current_field_location_elements,
812 quirks);
813 case lttng_ust_ctl_atype_sequence:
814 return create_sequence_type_from_ust_ctl_fields(current,
815 end,
816 session_attributes,
817 next_ust_ctl_field,
818 publish_field,
819 lookup_root,
820 current_field_location_elements,
821 quirks);
822 case lttng_ust_ctl_atype_sequence_nestable:
823 return create_sequence_nestable_type_from_ust_ctl_fields(
824 current,
825 end,
826 session_attributes,
827 next_ust_ctl_field,
828 publish_field,
829 lookup_field,
830 lookup_root,
831 current_field_location_elements,
832 quirks);
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,
840 end,
841 session_attributes,
842 next_ust_ctl_field,
843 lookup_field,
844 lookup_root,
845 current_field_location_elements,
846 quirks);
847 default:
848 LTTNG_THROW_PROTOCOL_ERROR(
849 fmt::format("Unknown {} value `{}` encountered while converting {} to {}",
850 typeid(current->type.atype),
851 current->type.atype,
852 typeid(*current),
853 typeid(lst::type::cuptr::element_type)));
854 }
855 }
856
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)
866 {
867 LTTNG_ASSERT(current < end);
868
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)));
872 }
873
874 publish_field(lttng::make_unique<lst::field>(
875 current->name,
876 create_type_from_ust_ctl_fields(current,
877 end,
878 session_attributes,
879 next_ust_ctl_field,
880 publish_field,
881 lookup_field,
882 lookup_root,
883 current_field_location_elements,
884 quirks)));
885 }
886
887 std::vector<lst::field::cuptr>::iterator
888 lookup_field_in_vector(std::vector<lst::field::cuptr>& fields, const lst::field_location& location)
889 {
890 if (location.elements_.size() != 1) {
891 LTTNG_THROW_ERROR(fmt::format(
892 "Unexpected field location received during field look-up: location = {}",
893 location));
894 }
895
896 /*
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.
902 */
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];
906 });
907
908 if (field_it == fields.end()) {
909 LTTNG_THROW_PROTOCOL_ERROR(
910 fmt::format("Failed to look-up field: location = {}", location));
911 }
912
913 return field_it;
914 }
915
916 /*
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
920 * is consumed.
921 *
922 * Always returns a new field, throws on error.
923 */
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)
930 {
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);
936 },
937 trace_native_byte_order
938 };
939 /* Location of field being created. */
940 lst::field_location::elements current_field_location_elements;
941
942 while (current < end) {
943 auto *next_field = current;
944
945 /*
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).
950 *
951 * The lambda allows the factory functions to push as many fields as
952 * needed depending on the decoded field's type.
953 */
954 create_field_from_ust_ctl_fields(
955 current,
956 end,
957 session_attributes,
958 &next_field,
959 [&fields](lst::field::cuptr field) {
960 /* Publishing a field simply adds it to the converted
961 * fields. */
962 fields.emplace_back(std::move(field));
963 },
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);
968 },
969 lookup_root,
970 current_field_location_elements,
971 quirks);
972
973 current = next_field;
974 }
975
976 return fields;
977 }
978 } /* namespace */
979
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)
986 {
987 return create_fields_from_ust_ctl_fields(
988 session, fields, fields + field_count, lookup_root, quirks);
989 }
This page took 0.080848 seconds and 4 git commands to generate.