sessiond: validate existence of field reference received from LTTng-UST
[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(
28 const char *name, 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}, _native_trace_byte_order{native_trace_byte_order}
33 {
34 }
35
36 const registry_enum_getter_fn get_registry_enum;
37 const lst::byte_order _native_trace_byte_order;
38 };
39
40 /* Used to publish fields on which a field being decoded has an implicit dependency. */
41 using publish_field_fn = std::function<void(lst::field::cuptr)>;
42
43 /* Look-up field from a field location. */
44 using lookup_field_fn = std::function<const lst::field &(const lst::field_location &)>;
45
46 lst::type::cuptr create_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
47 const lttng_ust_ctl_field *end,
48 const session_attributes& session_attributes,
49 const lttng_ust_ctl_field **next_ust_ctl_field,
50 publish_field_fn publish_field,
51 lookup_field_fn lookup_field,
52 lst::field_location::root lookup_root,
53 lst::field_location::elements& current_field_location_elements);
54
55 void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
56 const lttng_ust_ctl_field *end,
57 const session_attributes& session_attributes,
58 const lttng_ust_ctl_field **next_ust_ctl_field,
59 publish_field_fn publish_field,
60 lookup_field_fn lookup_field,
61 lst::field_location::root lookup_root,
62 lst::field_location::elements& current_field_location_elements);
63
64 template <class UstCtlEncodingType>
65 enum lst::null_terminated_string_type::encoding ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
66 {
67 static const std::unordered_map<UstCtlEncodingType, enum lst::null_terminated_string_type::encoding>
68 encoding_conversion_map = {
69 {(UstCtlEncodingType) lttng_ust_ctl_encode_ASCII,
70 lst::null_terminated_string_type::encoding::ASCII},
71 {(UstCtlEncodingType) lttng_ust_ctl_encode_UTF8,
72 lst::null_terminated_string_type::encoding::UTF8},
73 };
74
75 const auto encoding_it = encoding_conversion_map.find(encoding);
76 if (encoding_it == encoding_conversion_map.end()) {
77 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
78 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
79 encoding));
80 }
81
82 return encoding_it->second;
83 }
84
85 template <class UstCtlBaseType>
86 enum lst::integer_type::base ust_ctl_base_to_integer_field_base(UstCtlBaseType base)
87 {
88 static const std::unordered_map<UstCtlBaseType, enum lst::integer_type::base>
89 base_conversion_map = {{2, lst::integer_type::base::BINARY},
90 {8, lst::integer_type::base::OCTAL},
91 {10, lst::integer_type::base::DECIMAL},
92 {16, lst::integer_type::base::HEXADECIMAL}};
93
94 const auto base_it = base_conversion_map.find(base);
95 if (base_it == base_conversion_map.end()) {
96 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
97 "Unknown integer base value `{}` encountered when decoding integer field",
98 base));
99 }
100
101 return base_it->second;
102 }
103
104 lst::type::cuptr create_integer_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
105 const lttng_ust_ctl_field *end,
106 const session_attributes& session_attributes,
107 const lttng_ust_ctl_field **next_ust_ctl_field)
108 {
109 if (current >= end) {
110 LTTNG_THROW_PROTOCOL_ERROR(
111 fmt::format("End of {} array reached unexpectedly during decoding",
112 typeid(*current)));
113 }
114
115 const auto base = ust_ctl_base_to_integer_field_base(current->type.u.integer.base);
116 const auto signedness = current->type.u.integer.signedness ?
117 lst::integer_type::signedness::SIGNED :
118 lst::integer_type::signedness::UNSIGNED;
119 const auto byte_order = current->type.u.integer.reverse_byte_order ?
120 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
121 session_attributes._native_trace_byte_order;
122
123 *next_ust_ctl_field = current + 1;
124
125 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
126 byte_order, current->type.u.integer.size, signedness, base);
127 }
128
129 lst::type::cuptr create_floating_point_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
130 const lttng_ust_ctl_field *end,
131 const session_attributes& session_attributes,
132 const lttng_ust_ctl_field **next_ust_ctl_field)
133 {
134 if (current >= end) {
135 LTTNG_THROW_PROTOCOL_ERROR(
136 fmt::format("End of {} array reached unexpectedly during decoding",
137 typeid(*current)));
138 }
139
140 *next_ust_ctl_field = current + 1;
141
142 const auto byte_order = current->type.u._float.reverse_byte_order ?
143 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
144 session_attributes._native_trace_byte_order;
145
146 try {
147 return lttng::make_unique<const lst::floating_point_type>(
148 current->type.u._float.alignment, byte_order,
149 current->type.u._float.exp_dig, current->type.u._float.mant_dig);
150 } catch (lttng::invalid_argument_error& ex) {
151 LTTNG_THROW_PROTOCOL_ERROR(fmt::format("Invalid floating point attribute in {}: {}",
152 typeid(*current), ex.what()));
153 }
154 }
155
156 lst::type::cuptr create_enumeration_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
157 const lttng_ust_ctl_field *end,
158 const session_attributes& session_attributes,
159 const lttng_ust_ctl_field **next_ust_ctl_field)
160 {
161 if (current >= end) {
162 LTTNG_THROW_PROTOCOL_ERROR(
163 fmt::format("End of {} array reached unexpectedly during decoding",
164 typeid(*current)));
165 }
166
167 uint64_t enumeration_id;
168 const auto& enum_uctl_field = *current;
169 const char *enumeration_name;
170 const auto *enum_container_uctl_type =
171 &current->type.u.legacy.basic.enumeration.container_type;
172
173 if (enum_uctl_field.type.atype == lttng_ust_ctl_atype_enum_nestable) {
174 /* Nestable enumeration fields are followed by their container type. */
175 ++current;
176 if (current >= end) {
177 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
178 "Array of {} is too short to contain nestable enumeration's container",
179 typeid(*current)));
180 }
181
182 if (current->type.atype != lttng_ust_ctl_atype_integer) {
183 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
184 "Invalid type of nestable enum container: type id = {}",
185 current->type.atype));
186 }
187
188 enum_container_uctl_type = &current->type.u.integer;
189 enumeration_id = enum_uctl_field.type.u.enum_nestable.id;
190 enumeration_name = enum_uctl_field.type.u.enum_nestable.name;
191 } else {
192 enumeration_id = enum_uctl_field.type.u.legacy.basic.enumeration.id;
193 enumeration_name = enum_uctl_field.type.u.legacy.basic.enumeration.name;
194 }
195
196 *next_ust_ctl_field = current + 1;
197
198 const auto base = ust_ctl_base_to_integer_field_base(enum_container_uctl_type->base);
199 const auto byte_order = enum_container_uctl_type->reverse_byte_order ?
200 lst::integer_type::reverse_byte_order(
201 session_attributes._native_trace_byte_order) :
202 session_attributes._native_trace_byte_order;
203 const auto signedness = enum_container_uctl_type->signedness ?
204 lst::integer_type::signedness::SIGNED :
205 lst::integer_type::signedness::UNSIGNED;
206
207 if (signedness == lst::integer_type::signedness::SIGNED) {
208 const auto& enum_registry = static_cast<const lsu::registry_signed_enum&>(
209 *session_attributes.get_registry_enum(
210 enumeration_name, enumeration_id));
211
212 return lttng::make_unique<const lst::signed_enumeration_type>(
213 enum_container_uctl_type->alignment, byte_order,
214 enum_container_uctl_type->size, base,
215 enum_registry._mappings);
216 } else {
217 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
218 *session_attributes.get_registry_enum(
219 enumeration_name, enumeration_id));
220
221 return lttng::make_unique<const lst::unsigned_enumeration_type>(
222 enum_container_uctl_type->alignment, byte_order,
223 enum_container_uctl_type->size, base,
224 enum_registry._mappings);
225 }
226 }
227
228 lst::type::cuptr create_string_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
229 const lttng_ust_ctl_field *end,
230 const session_attributes& session_attributes __attribute__((unused)),
231 const lttng_ust_ctl_field **next_ust_ctl_field)
232 {
233 if (current >= end) {
234 LTTNG_THROW_PROTOCOL_ERROR(
235 fmt::format("End of {} array reached unexpectedly during decoding",
236 typeid(*current)));
237 }
238
239 const auto& string_uctl_field = *current;
240 *next_ust_ctl_field = current + 1;
241
242 const auto encoding = ust_ctl_encoding_to_string_field_encoding(
243 string_uctl_field.type.u.string.encoding);
244
245 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
246 }
247
248 lst::type::cuptr create_integer_type_from_ust_ctl_basic_type(
249 const lttng_ust_ctl_basic_type& type, const session_attributes& session_attributes)
250 {
251 /* Checked by caller. */
252 LTTNG_ASSERT(type.atype == lttng_ust_ctl_atype_integer);
253
254 const auto byte_order = type.u.basic.integer.reverse_byte_order ?
255 lst::integer_type::reverse_byte_order(
256 session_attributes._native_trace_byte_order) :
257 session_attributes._native_trace_byte_order;
258 const auto signedness = type.u.basic.integer.signedness ?
259 lst::integer_type::signedness::SIGNED :
260 lst::integer_type::signedness::UNSIGNED;
261 const auto base = ust_ctl_base_to_integer_field_base(type.u.basic.integer.base);
262 const auto size = type.u.basic.integer.size;
263 const auto alignment = type.u.basic.integer.alignment;
264
265 return lttng::make_unique<const lst::integer_type>(
266 alignment, byte_order, size, signedness, base);
267 }
268
269 lst::type::cuptr create_array_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
270 const lttng_ust_ctl_field *end,
271 const session_attributes& session_attributes,
272 const lttng_ust_ctl_field **next_ust_ctl_field)
273 {
274 if (current >= end) {
275 LTTNG_THROW_PROTOCOL_ERROR(
276 fmt::format("End of {} array reached unexpectedly during decoding",
277 typeid(*current)));
278 }
279
280 const auto& array_uctl_field = *current;
281 uint32_t array_alignment, array_length;
282 lst::type::cuptr element_type;
283 nonstd::optional<enum lst::string_type::encoding> element_encoding;
284
285 array_length = array_uctl_field.type.u.legacy.array.length;
286 array_alignment = 0;
287
288 const auto& element_uctl_type = array_uctl_field.type.u.legacy.array.elem_type;
289 if (element_uctl_type.atype != lttng_ust_ctl_atype_integer) {
290 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
291 "Unexpected legacy array element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
292 element_uctl_type.atype, lttng_ust_ctl_atype_integer));
293 }
294
295 element_type = create_integer_type_from_ust_ctl_basic_type(
296 element_uctl_type, session_attributes);
297 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
298 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
299 /* Element represents a text character. */
300 element_encoding = ust_ctl_encoding_to_string_field_encoding(
301 element_uctl_type.u.basic.integer.encoding);
302 }
303
304 *next_ust_ctl_field = current + 1;
305
306 if (element_encoding) {
307 const auto integer_element_size =
308 static_cast<const lst::integer_type&>(*element_type).size;
309
310 if (integer_element_size != 8) {
311 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
312 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
313 integer_element_size));
314 }
315
316 /* Array is a static-length string. */
317 return lttng::make_unique<lst::static_length_string_type>(
318 array_alignment, *element_encoding, array_length);
319 }
320
321 return lttng::make_unique<lst::static_length_array_type>(
322 array_alignment, std::move(element_type), array_length);
323 }
324
325 lst::type::cuptr create_array_nestable_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
326 const lttng_ust_ctl_field *end,
327 const session_attributes& session_attributes,
328 const lttng_ust_ctl_field **next_ust_ctl_field,
329 publish_field_fn publish_field,
330 lookup_field_fn lookup_field,
331 lst::field_location::root lookup_root,
332 lst::field_location::elements &current_field_location_elements)
333 {
334 if (current >= end) {
335 LTTNG_THROW_PROTOCOL_ERROR(
336 fmt::format("End of {} array reached unexpectedly during decoding",
337 typeid(*current)));
338 }
339
340 const auto& array_uctl_field = *current;
341 uint32_t array_alignment, array_length;
342 lst::type::cuptr element_type;
343 nonstd::optional<enum lst::string_type::encoding> element_encoding;
344
345 array_length = array_uctl_field.type.u.array_nestable.length;
346 array_alignment = array_uctl_field.type.u.array_nestable.alignment;
347
348 /* Nestable array fields are followed by their element type. */
349 const auto& element_uctl_field = *(current + 1);
350
351 /* next_ust_ctl_field is updated as needed. */
352 element_type = create_type_from_ust_ctl_fields(&element_uctl_field, end, session_attributes,
353 next_ust_ctl_field, publish_field, lookup_field, lookup_root,
354 current_field_location_elements);
355 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
356 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
357 /* Element represents a text character. */
358 element_encoding = ust_ctl_encoding_to_string_field_encoding(
359 element_uctl_field.type.u.integer.encoding);
360 }
361
362 if (element_encoding) {
363 const auto integer_element_size =
364 static_cast<const lst::integer_type&>(*element_type).size;
365
366 if (integer_element_size != 8) {
367 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
368 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
369 integer_element_size));
370 }
371
372 /* Array is a static-length string. */
373 return lttng::make_unique<lst::static_length_string_type>(
374 array_alignment, *element_encoding, array_length);
375 }
376
377 return lttng::make_unique<lst::static_length_array_type>(
378 array_alignment, std::move(element_type), array_length);
379 }
380
381 /*
382 * For legacy sequence types, LTTng-UST expresses both the sequence and sequence
383 * length as part of the same lttng_ust_ctl_field entry.
384 */
385 lst::type::cuptr create_sequence_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
386 const lttng_ust_ctl_field *end,
387 const session_attributes& session_attributes,
388 const lttng_ust_ctl_field **next_ust_ctl_field,
389 publish_field_fn publish_field,
390 lst::field_location::root lookup_root,
391 lst::field_location::elements &current_field_location_elements)
392 {
393 if (current >= end) {
394 LTTNG_THROW_PROTOCOL_ERROR(
395 fmt::format("End of {} array reached unexpectedly during decoding",
396 typeid(*current)));
397 }
398
399 const auto& sequence_uctl_field = *current;
400 const auto& element_uctl_type = sequence_uctl_field.type.u.legacy.sequence.elem_type;
401 const auto& length_uctl_type = sequence_uctl_field.type.u.legacy.sequence.length_type;
402 const auto sequence_alignment = 0U;
403
404 if (element_uctl_type.atype != lttng_ust_ctl_atype_integer) {
405 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
406 "Unexpected legacy sequence element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
407 element_uctl_type.atype, lttng_ust_ctl_atype_integer));
408 }
409
410 if (length_uctl_type.atype != lttng_ust_ctl_atype_integer) {
411 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
412 "Unexpected legacy sequence length field type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
413 length_uctl_type.atype, lttng_ust_ctl_atype_integer));
414 }
415
416 nonstd::optional<enum lst::string_type::encoding> element_encoding;
417 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
418 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
419 /* Element represents a text character. */
420 element_encoding = ust_ctl_encoding_to_string_field_encoding(
421 element_uctl_type.u.basic.integer.encoding);
422 }
423
424 const auto length_field_name = fmt::format("_{}_length", sequence_uctl_field.name);
425 auto element_type = create_integer_type_from_ust_ctl_basic_type(
426 element_uctl_type, session_attributes);
427 auto length_type = create_integer_type_from_ust_ctl_basic_type(
428 length_uctl_type, session_attributes);
429
430 lst::field_location::elements length_field_location_elements =
431 current_field_location_elements;
432 length_field_location_elements.emplace_back(length_field_name);
433
434 const lst::field_location length_field_location{
435 lookup_root, std::move(length_field_location_elements)};
436
437 /* Publish an implicit length field _before_ the sequence field. */
438 publish_field(lttng::make_unique<lst::field>(std::move(length_field_name), std::move(length_type)));
439
440 *next_ust_ctl_field = current + 1;
441
442 if (element_encoding) {
443 const auto integer_element_size =
444 static_cast<const lst::integer_type&>(*element_type).size;
445
446 if (integer_element_size != 8) {
447 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
448 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
449 integer_element_size));
450 }
451
452 /* Sequence is a dynamic-length string. */
453 return lttng::make_unique<lst::dynamic_length_string_type>(sequence_alignment,
454 *element_encoding, std::move(length_field_location));
455 }
456
457 return lttng::make_unique<lst::dynamic_length_array_type>(sequence_alignment,
458 std::move(element_type), std::move(length_field_location));
459 }
460
461 lst::type::cuptr create_sequence_nestable_type_from_ust_ctl_fields(
462 const lttng_ust_ctl_field *current,
463 const lttng_ust_ctl_field *end,
464 const session_attributes& session_attributes,
465 const lttng_ust_ctl_field **next_ust_ctl_field,
466 publish_field_fn publish_field,
467 lookup_field_fn lookup_field,
468 lst::field_location::root lookup_root,
469 lst::field_location::elements &current_field_location_elements)
470 {
471 if (current >= end) {
472 LTTNG_THROW_PROTOCOL_ERROR(
473 fmt::format("End of {} array reached unexpectedly during decoding",
474 typeid(*current)));
475 }
476
477 const auto& sequence_uctl_field = *current;
478 const auto sequence_alignment = sequence_uctl_field.type.u.sequence_nestable.alignment;
479 const auto *length_field_name = sequence_uctl_field.type.u.sequence_nestable.length_name;
480
481 /* Nestable sequence fields are followed by their element type. */
482 const auto& element_uctl_field = *(current + 1);
483
484 nonstd::optional<enum lst::string_type::encoding> element_encoding;
485 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
486 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
487 /* Element represents a text character. */
488 element_encoding = ust_ctl_encoding_to_string_field_encoding(
489 element_uctl_field.type.u.integer.encoding);
490 }
491
492 /* next_ust_ctl_field is updated as needed. */
493 auto element_type = create_type_from_ust_ctl_fields(&element_uctl_field, end,
494 session_attributes, next_ust_ctl_field, publish_field, lookup_field,
495 lookup_root, current_field_location_elements);
496
497 if (lttng_strnlen(sequence_uctl_field.type.u.sequence_nestable.length_name,
498 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) ==
499 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) {
500 LTTNG_THROW_PROTOCOL_ERROR("Sequence length field name is not null terminated");
501 }
502
503 lst::field_location::elements length_field_location_elements =
504 current_field_location_elements;
505 length_field_location_elements.emplace_back(std::move(length_field_name));
506
507 const lst::field_location length_field_location{
508 lookup_root, std::move(length_field_location_elements)};
509
510 /* Validate existence of length field (throws if not found). */
511 const auto &length_field = lookup_field(length_field_location);
512 const auto *integer_selector_field =
513 dynamic_cast<const lst::integer_type *>(length_field._type.get());
514 if (!integer_selector_field) {
515 LTTNG_THROW_PROTOCOL_ERROR("Invalid selector field type referenced from sequence: expected integer or enumeration");
516 }
517
518 if (element_encoding) {
519 const auto integer_element_size =
520 static_cast<const lst::integer_type&>(*element_type).size;
521
522 if (integer_element_size != 8) {
523 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
524 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
525 integer_element_size));
526 }
527
528 /* Sqeuence is a dynamic-length string. */
529 return lttng::make_unique<lst::dynamic_length_string_type>(sequence_alignment,
530 *element_encoding, std::move(length_field_location));
531 }
532
533 return lttng::make_unique<lst::dynamic_length_array_type>(sequence_alignment,
534 std::move(element_type), std::move(length_field_location));
535 }
536
537 lst::type::cuptr create_structure_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
538 const lttng_ust_ctl_field *end,
539 const session_attributes& session_attributes __attribute__((unused)),
540 const lttng_ust_ctl_field **next_ust_ctl_field)
541 {
542 if (current >= end) {
543 LTTNG_THROW_PROTOCOL_ERROR(
544 fmt::format("End of {} array reached unexpectedly during decoding",
545 typeid(*current)));
546 }
547
548 uint32_t field_count;
549 uint32_t alignment;
550 const auto& structure_uctl_field = *current;
551
552 if (structure_uctl_field.type.atype == lttng_ust_ctl_atype_struct) {
553 field_count = structure_uctl_field.type.u.legacy._struct.nr_fields;
554 alignment = 0;
555 } else {
556 field_count = structure_uctl_field.type.u.struct_nestable.nr_fields;
557 alignment = structure_uctl_field.type.u.struct_nestable.alignment;
558 }
559
560 if (field_count != 0) {
561 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
562 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
563 field_count));
564 }
565
566 *next_ust_ctl_field = current + 1;
567 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
568 }
569
570 lst::type::cuptr create_variant_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
571 const lttng_ust_ctl_field *end,
572 const session_attributes& session_attributes,
573 const lttng_ust_ctl_field **next_ust_ctl_field,
574 lookup_field_fn lookup_field,
575 lst::field_location::root lookup_root,
576 lst::field_location::elements &current_field_location_elements)
577 {
578 if (current >= end) {
579 LTTNG_THROW_PROTOCOL_ERROR(
580 fmt::format("End of {} array reached unexpectedly during decoding",
581 typeid(*current)));
582 }
583
584 const auto& variant_uctl_field = *current;
585 current++;
586
587 uint32_t alignment;
588 uint32_t choice_count;
589 const char *tag_name;
590
591 if (variant_uctl_field.type.atype == lttng_ust_ctl_atype_variant) {
592 alignment = 0;
593 choice_count = variant_uctl_field.type.u.legacy.variant.nr_choices;
594 tag_name = variant_uctl_field.type.u.legacy.variant.tag_name;
595 } else {
596 alignment = variant_uctl_field.type.u.variant_nestable.alignment;
597 choice_count = variant_uctl_field.type.u.variant_nestable.nr_choices;
598 tag_name = variant_uctl_field.type.u.variant_nestable.tag_name;
599 }
600
601 lst::field_location::elements selector_field_location_elements =
602 current_field_location_elements;
603 selector_field_location_elements.emplace_back(tag_name);
604
605 const lst::field_location selector_field_location{
606 lookup_root, std::move(selector_field_location_elements)};
607
608 /* Validate existence of selector field (throws if not found). */
609 const auto &selector_field = lookup_field(selector_field_location);
610 const auto *integer_selector_field = dynamic_cast<const lst::integer_type *>(selector_field._type.get());
611 if (!integer_selector_field) {
612 LTTNG_THROW_PROTOCOL_ERROR("Invalid selector field type referenced from variant: expected integer or enumeration");
613 }
614
615 /* Choices follow. next_ust_ctl_field is updated as needed. */
616 lst::variant_type::choices choices;
617 for (unsigned int i = 0; i < choice_count; i++) {
618 create_field_from_ust_ctl_fields(
619 current, end, session_attributes, next_ust_ctl_field,
620 [&choices](lst::field::cuptr field) {
621 choices.emplace_back(std::move(field));
622 },
623 lookup_field,
624 lookup_root, current_field_location_elements);
625
626 current = *next_ust_ctl_field;
627 }
628
629 return lttng::make_unique<lst::variant_type>(
630 alignment, std::move(selector_field_location), std::move(choices));
631 }
632
633 lst::type::cuptr create_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
634 const lttng_ust_ctl_field *end,
635 const session_attributes& session_attributes,
636 const lttng_ust_ctl_field **next_ust_ctl_field,
637 publish_field_fn publish_field,
638 lookup_field_fn lookup_field,
639 lst::field_location::root lookup_root,
640 lst::field_location::elements &current_field_location_elements)
641 {
642 switch (current->type.atype) {
643 case lttng_ust_ctl_atype_integer:
644 return create_integer_type_from_ust_ctl_fields(
645 current, end, session_attributes, next_ust_ctl_field);
646 case lttng_ust_ctl_atype_enum:
647 case lttng_ust_ctl_atype_enum_nestable:
648 return create_enumeration_type_from_ust_ctl_fields(
649 current, end, session_attributes, next_ust_ctl_field);
650 case lttng_ust_ctl_atype_float:
651 return create_floating_point_type_from_ust_ctl_fields(
652 current, end, session_attributes, next_ust_ctl_field);
653 case lttng_ust_ctl_atype_string:
654 return create_string_type_from_ust_ctl_fields(
655 current, end, session_attributes, next_ust_ctl_field);
656 case lttng_ust_ctl_atype_array:
657 return create_array_type_from_ust_ctl_fields(current, end, session_attributes,
658 next_ust_ctl_field);
659 case lttng_ust_ctl_atype_array_nestable:
660 return create_array_nestable_type_from_ust_ctl_fields(current, end,
661 session_attributes, next_ust_ctl_field, publish_field, lookup_field,
662 lookup_root, current_field_location_elements);
663 case lttng_ust_ctl_atype_sequence:
664 return create_sequence_type_from_ust_ctl_fields(current, end, session_attributes,
665 next_ust_ctl_field, publish_field, lookup_root,
666 current_field_location_elements);
667 case lttng_ust_ctl_atype_sequence_nestable:
668 return create_sequence_nestable_type_from_ust_ctl_fields(current, end,
669 session_attributes, next_ust_ctl_field, publish_field, lookup_field,
670 lookup_root, current_field_location_elements);
671 case lttng_ust_ctl_atype_struct:
672 case lttng_ust_ctl_atype_struct_nestable:
673 return create_structure_field_from_ust_ctl_fields(
674 current, end, session_attributes, next_ust_ctl_field);
675 case lttng_ust_ctl_atype_variant:
676 case lttng_ust_ctl_atype_variant_nestable:
677 return create_variant_field_from_ust_ctl_fields(current, end, session_attributes,
678 next_ust_ctl_field, lookup_field, lookup_root,
679 current_field_location_elements);
680 default:
681 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
682 "Unknown {} value `{}` encountered while converting {} to {}",
683 typeid(current->type.atype), current->type.atype, typeid(*current),
684 typeid(lst::type::cuptr::element_type)));
685 }
686 }
687
688 void create_field_from_ust_ctl_fields(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 publish_field_fn publish_field,
693 lookup_field_fn lookup_field,
694 lst::field_location::root lookup_root,
695 lst::field_location::elements& current_field_location_elements)
696 {
697 LTTNG_ASSERT(current < end);
698
699 if (lttng_strnlen(current->name, sizeof(current->name)) == sizeof(current->name)) {
700 LTTNG_THROW_PROTOCOL_ERROR(
701 fmt::format("Name of {} is not null-terminated", typeid(*current)));
702 }
703
704 publish_field(lttng::make_unique<lst::field>(current->name,
705 create_type_from_ust_ctl_fields(current, end, session_attributes,
706 next_ust_ctl_field, publish_field, lookup_field,
707 lookup_root, current_field_location_elements)));
708 }
709
710 /*
711 * `lttng_ust_ctl_field`s can be nested, in which case creating a field will consume
712 * more than one lttng_ust_ctl_field. create_field_from_ust_ctl_fields returns the
713 * position of the next lttng_ust_ctl_field to consume or `end` when the last field
714 * is consumed.
715 *
716 * Always returns a new field, throws on error.
717 */
718 std::vector<lst::field::cuptr> create_fields_from_ust_ctl_fields(
719 const lsu::registry_session& session,
720 const lttng_ust_ctl_field *current,
721 const lttng_ust_ctl_field *end,
722 lst::field_location::root lookup_root)
723 {
724 std::vector<lst::field::cuptr> fields;
725 const auto trace_native_byte_order = session.abi.byte_order;
726 const session_attributes session_attributes{
727 [&session](const char *enum_name, uint64_t enum_id) {
728 return session.get_enumeration(enum_name, enum_id);
729 },
730 trace_native_byte_order};
731 /* Location of field being created. */
732 lst::field_location::elements current_field_location_elements;
733
734 while (current < end) {
735 auto *next_field = current;
736
737 /*
738 * create_field_from_ust_ctl_fields will consume one field at a time.
739 * However, some fields expressed by LTTng-UST's protocol are expended
740 * to multiple event fields (legacy sequence fields implicitly define
741 * their length field).
742 *
743 * The lambda allows the factory functions to push as many fields as
744 * needed depending on the decoded field's type.
745 */
746 create_field_from_ust_ctl_fields(
747 current, end, session_attributes, &next_field,
748 [&fields](lst::field::cuptr field) {
749 /* Publishing a field simply adds it to the converted fields. */
750 fields.emplace_back(std::move(field));
751 },
752 [&fields](const lst::field_location& location)
753 -> lookup_field_fn::result_type {
754 if (location.elements_.size() != 1) {
755 LTTNG_THROW_ERROR(fmt::format(
756 "Unexpected field location received during field look-up: location = {}",
757 location));
758 }
759
760 /*
761 * In the context of fields received from LTTng-UST, field
762 * look-up is extremely naive as the protocol can only
763 * express empty structures. It is safe to assume that
764 * location has a depth of 1 and directly refers to a field
765 * in the 'fields' vector.
766 */
767 const auto field_it = std::find_if(fields.begin(),
768 fields.end(),
769 [&location](decltype(fields)::value_type&
770 field) {
771 return field->name ==
772 location.elements_[0];
773 });
774
775 if (field_it == fields.end()) {
776 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
777 "Failed to look-up field: location = {}",
778 location));
779 }
780
781 return **field_it;
782 },
783 lookup_root, current_field_location_elements);
784
785 current = next_field;
786 }
787
788 return fields;
789 }
790 } /* namespace */
791
792 std::vector<lst::field::cuptr> lsu::create_trace_fields_from_ust_ctl_fields(
793 const lsu::registry_session& session,
794 const lttng_ust_ctl_field *fields,
795 std::size_t field_count,
796 lst::field_location::root lookup_root)
797 {
798 return create_fields_from_ust_ctl_fields(session, fields, fields + field_count, lookup_root);
799 }
This page took 0.044685 seconds and 4 git commands to generate.