Run clang-format on the whole tree
[lttng-tools.git] / src / bin / lttng-sessiond / ust-field-convert.cpp
CommitLineData
d7bfb9b0
JG
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
6e01cdc6 10#include <common/exception.hpp>
d7bfb9b0
JG
11#include <common/make-unique.hpp>
12
13#include <unordered_map>
14
15namespace lst = lttng::sessiond::trace;
16namespace lsu = lttng::sessiond::ust;
17namespace {
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 */
24class session_attributes {
25public:
26 using registry_enum_getter_fn =
28ab034a
JG
27 std::function<lsu::registry_enum::const_rcu_protected_reference(const char *name,
28 uint64_t id)>;
d7bfb9b0
JG
29
30 session_attributes(registry_enum_getter_fn reg_enum_getter,
28ab034a
JG
31 lst::byte_order native_trace_byte_order) :
32 get_registry_enum{ reg_enum_getter },
33 _native_trace_byte_order{ native_trace_byte_order }
d7bfb9b0
JG
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. */
45110cdd 42using publish_field_fn = std::function<void(lst::field::uptr)>;
d7bfb9b0 43
6e01cdc6 44/* Look-up field from a field location. */
28ab034a
JG
45using lookup_field_fn = std::function<const lst::field&(const lst::field_location&)>;
46
47lst::type::cuptr
48create_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);
d7bfb9b0
JG
57
58void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
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);
d7bfb9b0
JG
67
68template <class UstCtlEncodingType>
28ab034a
JG
69enum lst::null_terminated_string_type::encoding
70ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
d7bfb9b0 71{
28ab034a
JG
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 };
d7bfb9b0
JG
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(
28ab034a
JG
84 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
85 encoding));
d7bfb9b0
JG
86 }
87
88 return encoding_it->second;
89}
90
91template <class UstCtlBaseType>
92enum 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>
28ab034a
JG
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 } };
d7bfb9b0
JG
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(
28ab034a
JG
103 "Unknown integer base value `{}` encountered when decoding integer field",
104 base));
d7bfb9b0
JG
105 }
106
107 return base_it->second;
108}
109
28ab034a
JG
110lst::type::cuptr
111create_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)))
d7bfb9b0
JG
116{
117 if (current >= end) {
28ab034a
JG
118 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
119 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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 ?
28ab034a
JG
124 lst::integer_type::signedness::SIGNED :
125 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 126 const auto byte_order = current->type.u.integer.reverse_byte_order ?
28ab034a
JG
127 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
128 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
129
130 *next_ust_ctl_field = current + 1;
131
132 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
28ab034a
JG
133 byte_order,
134 current->type.u.integer.size,
135 signedness,
136 base);
d7bfb9b0
JG
137}
138
28ab034a
JG
139lst::type::cuptr
140create_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)))
d7bfb9b0
JG
145{
146 if (current >= end) {
28ab034a
JG
147 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
148 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
149 }
150
151 *next_ust_ctl_field = current + 1;
152
153 const auto byte_order = current->type.u._float.reverse_byte_order ?
28ab034a
JG
154 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
155 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
156
157 try {
158 return lttng::make_unique<const lst::floating_point_type>(
28ab034a
JG
159 current->type.u._float.alignment,
160 byte_order,
161 current->type.u._float.exp_dig,
162 current->type.u._float.mant_dig);
d7bfb9b0 163 } catch (lttng::invalid_argument_error& ex) {
28ab034a
JG
164 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
165 "Invalid floating point attribute in {}: {}", typeid(*current), ex.what()));
d7bfb9b0
JG
166 }
167}
168
28ab034a
JG
169lst::type::cuptr
170create_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)))
d7bfb9b0
JG
175{
176 if (current >= end) {
28ab034a
JG
177 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
178 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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 =
28ab034a 185 &current->type.u.legacy.basic.enumeration.container_type;
d7bfb9b0
JG
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(
28ab034a
JG
192 "Array of {} is too short to contain nestable enumeration's container",
193 typeid(*current)));
d7bfb9b0
JG
194 }
195
196 if (current->type.atype != lttng_ust_ctl_atype_integer) {
28ab034a
JG
197 LTTNG_THROW_PROTOCOL_ERROR(
198 fmt::format("Invalid type of nestable enum container: type id = {}",
199 current->type.atype));
d7bfb9b0
JG
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 ?
28ab034a
JG
214 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
215 session_attributes._native_trace_byte_order;
24ed18f2 216 const auto signedness = enum_container_uctl_type->signedness ?
28ab034a
JG
217 lst::integer_type::signedness::SIGNED :
218 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 219
24ed18f2 220 if (signedness == lst::integer_type::signedness::SIGNED) {
d7bfb9b0 221 const auto& enum_registry = static_cast<const lsu::registry_signed_enum&>(
28ab034a 222 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
223
224 return lttng::make_unique<const lst::signed_enumeration_type>(
28ab034a
JG
225 enum_container_uctl_type->alignment,
226 byte_order,
227 enum_container_uctl_type->size,
228 base,
229 enum_registry._mappings);
d7bfb9b0
JG
230 } else {
231 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
28ab034a 232 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
233
234 return lttng::make_unique<const lst::unsigned_enumeration_type>(
28ab034a
JG
235 enum_container_uctl_type->alignment,
236 byte_order,
237 enum_container_uctl_type->size,
238 base,
239 enum_registry._mappings);
d7bfb9b0
JG
240 }
241}
242
28ab034a
JG
243lst::type::cuptr
244create_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)))
d7bfb9b0
JG
250{
251 if (current >= end) {
28ab034a
JG
252 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
253 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
254 }
255
256 const auto& string_uctl_field = *current;
257 *next_ust_ctl_field = current + 1;
258
28ab034a
JG
259 const auto encoding =
260 ust_ctl_encoding_to_string_field_encoding(string_uctl_field.type.u.string.encoding);
d7bfb9b0
JG
261
262 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
263}
264
28ab034a
JG
265lst::type::cuptr
266create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type& type,
267 const session_attributes& session_attributes)
d7bfb9b0
JG
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 ?
28ab034a
JG
273 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
274 session_attributes._native_trace_byte_order;
d7bfb9b0 275 const auto signedness = type.u.basic.integer.signedness ?
28ab034a
JG
276 lst::integer_type::signedness::SIGNED :
277 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0
JG
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>(
28ab034a 283 alignment, byte_order, size, signedness, base);
d7bfb9b0
JG
284}
285
28ab034a
JG
286lst::type::cuptr
287create_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)))
d7bfb9b0
JG
292{
293 if (current >= end) {
28ab034a
JG
294 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
295 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
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));
d7bfb9b0
JG
312 }
313
28ab034a
JG
314 element_type =
315 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
d7bfb9b0 316 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 317 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
318 /* Element represents a text character. */
319 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 320 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
321 }
322
323 *next_ust_ctl_field = current + 1;
324
325 if (element_encoding) {
326 const auto integer_element_size =
28ab034a 327 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
328
329 if (integer_element_size != 8) {
330 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
331 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
332 integer_element_size));
d7bfb9b0
JG
333 }
334
335 /* Array is a static-length string. */
336 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 337 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
338 }
339
340 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 341 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
342}
343
28ab034a
JG
344lst::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)
d7bfb9b0
JG
354{
355 if (current >= end) {
28ab034a
JG
356 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
357 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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. */
28ab034a
JG
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);
d7bfb9b0 381 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 382 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
383 /* Element represents a text character. */
384 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 385 element_uctl_field.type.u.integer.encoding);
d7bfb9b0
JG
386 }
387
388 if (element_encoding) {
389 const auto integer_element_size =
28ab034a 390 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
391
392 if (integer_element_size != 8) {
393 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
394 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
395 integer_element_size));
d7bfb9b0
JG
396 }
397
398 /* Array is a static-length string. */
399 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 400 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
401 }
402
403 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 404 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
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 */
28ab034a
JG
411lst::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)))
d7bfb9b0
JG
420{
421 if (current >= end) {
28ab034a
JG
422 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
423 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
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));
d7bfb9b0
JG
436 }
437
438 if (length_uctl_type.atype != lttng_ust_ctl_atype_integer) {
439 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
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));
d7bfb9b0
JG
443 }
444
445 nonstd::optional<enum lst::string_type::encoding> element_encoding;
446 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 447 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
448 /* Element represents a text character. */
449 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 450 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
451 }
452
453 const auto length_field_name = fmt::format("_{}_length", sequence_uctl_field.name);
28ab034a
JG
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);
d7bfb9b0 458
eda1aa02 459 lst::field_location::elements length_field_location_elements =
28ab034a 460 current_field_location_elements;
eda1aa02
JG
461 length_field_location_elements.emplace_back(length_field_name);
462
463 const lst::field_location length_field_location{
28ab034a
JG
464 lookup_root, std::move(length_field_location_elements)
465 };
eda1aa02 466
d7bfb9b0 467 /* Publish an implicit length field _before_ the sequence field. */
28ab034a
JG
468 publish_field(lttng::make_unique<lst::field>(std::move(length_field_name),
469 std::move(length_type)));
d7bfb9b0
JG
470
471 *next_ust_ctl_field = current + 1;
472
473 if (element_encoding) {
474 const auto integer_element_size =
28ab034a 475 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
476
477 if (integer_element_size != 8) {
478 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
479 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
480 integer_element_size));
d7bfb9b0
JG
481 }
482
483 /* Sequence is a dynamic-length string. */
28ab034a
JG
484 return lttng::make_unique<lst::dynamic_length_string_type>(
485 sequence_alignment, *element_encoding, std::move(length_field_location));
d7bfb9b0
JG
486 }
487
28ab034a
JG
488 return lttng::make_unique<lst::dynamic_length_array_type>(
489 sequence_alignment, std::move(element_type), std::move(length_field_location));
d7bfb9b0
JG
490}
491
492lst::type::cuptr create_sequence_nestable_type_from_ust_ctl_fields(
28ab034a
JG
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)
d7bfb9b0
JG
502{
503 if (current >= end) {
28ab034a
JG
504 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
505 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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 &&
28ab034a 517 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
518 /* Element represents a text character. */
519 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 520 element_uctl_field.type.u.integer.encoding);
d7bfb9b0
JG
521 }
522
523 /* next_ust_ctl_field is updated as needed. */
28ab034a
JG
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);
d7bfb9b0
JG
533
534 if (lttng_strnlen(sequence_uctl_field.type.u.sequence_nestable.length_name,
28ab034a
JG
535 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) ==
536 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) {
d7bfb9b0
JG
537 LTTNG_THROW_PROTOCOL_ERROR("Sequence length field name is not null terminated");
538 }
539
eda1aa02 540 lst::field_location::elements length_field_location_elements =
28ab034a 541 current_field_location_elements;
eda1aa02
JG
542 length_field_location_elements.emplace_back(std::move(length_field_name));
543
544 const lst::field_location length_field_location{
28ab034a
JG
545 lookup_root, std::move(length_field_location_elements)
546 };
eda1aa02 547
6e01cdc6 548 /* Validate existence of length field (throws if not found). */
28ab034a 549 const auto& length_field = lookup_field(length_field_location);
6e01cdc6 550 const auto *integer_selector_field =
28ab034a 551 dynamic_cast<const lst::integer_type *>(&length_field.get_type());
6e01cdc6 552 if (!integer_selector_field) {
28ab034a
JG
553 LTTNG_THROW_PROTOCOL_ERROR(
554 "Invalid selector field type referenced from sequence: expected integer or enumeration");
6e01cdc6
JG
555 }
556
d7bfb9b0
JG
557 if (element_encoding) {
558 const auto integer_element_size =
28ab034a 559 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
560
561 if (integer_element_size != 8) {
562 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
563 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
564 integer_element_size));
d7bfb9b0
JG
565 }
566
567 /* Sqeuence is a dynamic-length string. */
28ab034a
JG
568 return lttng::make_unique<lst::dynamic_length_string_type>(
569 sequence_alignment, *element_encoding, std::move(length_field_location));
d7bfb9b0
JG
570 }
571
28ab034a
JG
572 return lttng::make_unique<lst::dynamic_length_array_type>(
573 sequence_alignment, std::move(element_type), std::move(length_field_location));
d7bfb9b0
JG
574}
575
28ab034a
JG
576lst::type::cuptr
577create_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)))
d7bfb9b0
JG
583{
584 if (current >= end) {
28ab034a
JG
585 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
586 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
603 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
604 field_count));
d7bfb9b0
JG
605 }
606
607 *next_ust_ctl_field = current + 1;
608 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
609}
610
45110cdd 611template <class VariantSelectorMappingIntegerType>
28ab034a
JG
612typename lst::variant_type<VariantSelectorMappingIntegerType>::choices
613create_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)
45110cdd
JG
623{
624 typename lst::variant_type<VariantSelectorMappingIntegerType>::choices choices;
28ab034a
JG
625 const auto& typed_enumeration =
626 static_cast<const lst::typed_enumeration_type<VariantSelectorMappingIntegerType>&>(
45110cdd
JG
627 selector_field.get_type());
628
629 for (unsigned int i = 0; i < choice_count; i++) {
630 create_field_from_ust_ctl_fields(
28ab034a
JG
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);
45110cdd
JG
680
681 current = *next_ust_ctl_field;
682 }
683
684 return choices;
685}
686
28ab034a
JG
687lst::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)
d7bfb9b0
JG
696{
697 if (current >= end) {
28ab034a
JG
698 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
699 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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
eda1aa02 719 lst::field_location::elements selector_field_location_elements =
28ab034a 720 current_field_location_elements;
eda1aa02
JG
721 selector_field_location_elements.emplace_back(tag_name);
722
723 const lst::field_location selector_field_location{
28ab034a
JG
724 lookup_root, std::move(selector_field_location_elements)
725 };
eda1aa02 726
6e01cdc6 727 /* Validate existence of selector field (throws if not found). */
28ab034a 728 const auto& selector_field = lookup_field(selector_field_location);
45110cdd 729 const auto *enumeration_selector_type =
28ab034a 730 dynamic_cast<const lst::enumeration_type *>(&selector_field.get_type());
45110cdd 731 if (!enumeration_selector_type) {
28ab034a
JG
732 LTTNG_THROW_PROTOCOL_ERROR(
733 "Invalid selector field type referenced from variant: expected enumeration");
6e01cdc6
JG
734 }
735
45110cdd 736 const bool selector_is_signed = enumeration_selector_type->signedness_ ==
28ab034a 737 lst::integer_type::signedness::SIGNED;
45110cdd 738
d7bfb9b0 739 /* Choices follow. next_ust_ctl_field is updated as needed. */
45110cdd
JG
740 if (selector_is_signed) {
741 lst::variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::
28ab034a
JG
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);
45110cdd
JG
753
754 return lttng::make_unique<lst::variant_type<int64_t>>(
28ab034a 755 alignment, std::move(selector_field_location), std::move(choices));
45110cdd 756 } else {
28ab034a
JG
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);
d7bfb9b0 770
45110cdd 771 return lttng::make_unique<lst::variant_type<uint64_t>>(
28ab034a 772 alignment, std::move(selector_field_location), std::move(choices));
d7bfb9b0 773 }
d7bfb9b0
JG
774}
775
28ab034a
JG
776lst::type::cuptr
777create_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)
d7bfb9b0
JG
786{
787 switch (current->type.atype) {
788 case lttng_ust_ctl_atype_integer:
789 return create_integer_type_from_ust_ctl_fields(
28ab034a 790 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
791 case lttng_ust_ctl_atype_enum:
792 case lttng_ust_ctl_atype_enum_nestable:
793 return create_enumeration_type_from_ust_ctl_fields(
28ab034a 794 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
795 case lttng_ust_ctl_atype_float:
796 return create_floating_point_type_from_ust_ctl_fields(
28ab034a 797 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
798 case lttng_ust_ctl_atype_string:
799 return create_string_type_from_ust_ctl_fields(
28ab034a 800 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 801 case lttng_ust_ctl_atype_array:
63c3462c 802 return create_array_type_from_ust_ctl_fields(
28ab034a 803 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 804 case lttng_ust_ctl_atype_array_nestable:
28ab034a
JG
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);
d7bfb9b0 815 case lttng_ust_ctl_atype_sequence:
28ab034a
JG
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);
d7bfb9b0 824 case lttng_ust_ctl_atype_sequence_nestable:
28ab034a
JG
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);
d7bfb9b0
JG
835 case lttng_ust_ctl_atype_struct:
836 case lttng_ust_ctl_atype_struct_nestable:
837 return create_structure_field_from_ust_ctl_fields(
28ab034a 838 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
839 case lttng_ust_ctl_atype_variant:
840 case lttng_ust_ctl_atype_variant_nestable:
28ab034a
JG
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);
d7bfb9b0 849 default:
28ab034a
JG
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)));
d7bfb9b0
JG
856 }
857}
858
859void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
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)
d7bfb9b0
JG
868{
869 LTTNG_ASSERT(current < end);
870
871 if (lttng_strnlen(current->name, sizeof(current->name)) == sizeof(current->name)) {
872 LTTNG_THROW_PROTOCOL_ERROR(
28ab034a
JG
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)));
d7bfb9b0
JG
887}
888
28ab034a
JG
889std::vector<lst::field::cuptr>::iterator
890lookup_field_in_vector(std::vector<lst::field::cuptr>& fields, const lst::field_location& location)
da9dd521
JG
891{
892 if (location.elements_.size() != 1) {
893 LTTNG_THROW_ERROR(fmt::format(
28ab034a
JG
894 "Unexpected field location received during field look-up: location = {}",
895 location));
da9dd521
JG
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 */
28ab034a
JG
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 });
da9dd521
JG
909
910 if (field_it == fields.end()) {
911 LTTNG_THROW_PROTOCOL_ERROR(
28ab034a 912 fmt::format("Failed to look-up field: location = {}", location));
da9dd521
JG
913 }
914
915 return field_it;
916}
917
d7bfb9b0
JG
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 */
28ab034a
JG
926std::vector<lst::field::cuptr>
927create_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)
d7bfb9b0
JG
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{
28ab034a
JG
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 };
eda1aa02
JG
941 /* Location of field being created. */
942 lst::field_location::elements current_field_location_elements;
d7bfb9b0
JG
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 */
6e01cdc6 956 create_field_from_ust_ctl_fields(
28ab034a
JG
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);
d7bfb9b0
JG
974
975 current = next_field;
976 }
977
978 return fields;
979}
980} /* namespace */
981
28ab034a
JG
982std::vector<lst::field::cuptr>
983lsu::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)
d7bfb9b0 988{
63c3462c 989 return create_fields_from_ust_ctl_fields(
28ab034a 990 session, fields, fields + field_count, lookup_root, quirks);
d7bfb9b0 991}
This page took 0.070561 seconds and 4 git commands to generate.