clang-tidy: add a subset of cppcoreguidelines and other style checks
[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>
cd9adb8b 14#include <utility>
d7bfb9b0
JG
15
16namespace lst = lttng::sessiond::trace;
17namespace lsu = lttng::sessiond::ust;
18namespace {
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 */
25class session_attributes {
26public:
27 using registry_enum_getter_fn =
28ab034a
JG
28 std::function<lsu::registry_enum::const_rcu_protected_reference(const char *name,
29 uint64_t id)>;
d7bfb9b0
JG
30
31 session_attributes(registry_enum_getter_fn reg_enum_getter,
28ab034a 32 lst::byte_order native_trace_byte_order) :
cd9adb8b 33 get_registry_enum{ std::move(reg_enum_getter) },
28ab034a 34 _native_trace_byte_order{ native_trace_byte_order }
d7bfb9b0
JG
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. */
45110cdd 43using publish_field_fn = std::function<void(lst::field::uptr)>;
d7bfb9b0 44
6e01cdc6 45/* Look-up field from a field location. */
28ab034a
JG
46using lookup_field_fn = std::function<const lst::field&(const lst::field_location&)>;
47
48lst::type::cuptr
49create_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,
cd9adb8b
JG
53 const publish_field_fn& publish_field,
54 const lookup_field_fn& lookup_field,
28ab034a
JG
55 lst::field_location::root lookup_root,
56 lst::field_location::elements& current_field_location_elements,
57 lsu::ctl_field_quirks quirks);
d7bfb9b0
JG
58
59void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
60 const lttng_ust_ctl_field *end,
61 const session_attributes& session_attributes,
62 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
63 const publish_field_fn& publish_field,
64 const lookup_field_fn& lookup_field,
28ab034a
JG
65 lst::field_location::root lookup_root,
66 lst::field_location::elements& current_field_location_elements,
67 lsu::ctl_field_quirks quirks);
d7bfb9b0
JG
68
69template <class UstCtlEncodingType>
28ab034a
JG
70enum lst::null_terminated_string_type::encoding
71ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
d7bfb9b0 72{
28ab034a
JG
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 };
d7bfb9b0
JG
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(
28ab034a
JG
85 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
86 encoding));
d7bfb9b0
JG
87 }
88
89 return encoding_it->second;
90}
91
92template <class UstCtlBaseType>
93enum 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>
28ab034a
JG
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 } };
d7bfb9b0
JG
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(
28ab034a
JG
104 "Unknown integer base value `{}` encountered when decoding integer field",
105 base));
d7bfb9b0
JG
106 }
107
108 return base_it->second;
109}
110
28ab034a
JG
111lst::type::cuptr
112create_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)))
d7bfb9b0
JG
117{
118 if (current >= end) {
28ab034a
JG
119 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
120 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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 ?
28ab034a
JG
125 lst::integer_type::signedness::SIGNED :
126 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 127 const auto byte_order = current->type.u.integer.reverse_byte_order ?
28ab034a
JG
128 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
129 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
130
131 *next_ust_ctl_field = current + 1;
132
133 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
28ab034a
JG
134 byte_order,
135 current->type.u.integer.size,
136 signedness,
137 base);
d7bfb9b0
JG
138}
139
28ab034a
JG
140lst::type::cuptr
141create_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)))
d7bfb9b0
JG
146{
147 if (current >= end) {
28ab034a
JG
148 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
149 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
150 }
151
152 *next_ust_ctl_field = current + 1;
153
154 const auto byte_order = current->type.u._float.reverse_byte_order ?
28ab034a
JG
155 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
156 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
157
158 try {
159 return lttng::make_unique<const lst::floating_point_type>(
28ab034a
JG
160 current->type.u._float.alignment,
161 byte_order,
162 current->type.u._float.exp_dig,
163 current->type.u._float.mant_dig);
d7bfb9b0 164 } catch (lttng::invalid_argument_error& ex) {
28ab034a
JG
165 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
166 "Invalid floating point attribute in {}: {}", typeid(*current), ex.what()));
d7bfb9b0
JG
167 }
168}
169
28ab034a
JG
170lst::type::cuptr
171create_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)))
d7bfb9b0
JG
176{
177 if (current >= end) {
28ab034a
JG
178 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
179 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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 =
28ab034a 186 &current->type.u.legacy.basic.enumeration.container_type;
d7bfb9b0
JG
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(
28ab034a
JG
193 "Array of {} is too short to contain nestable enumeration's container",
194 typeid(*current)));
d7bfb9b0
JG
195 }
196
197 if (current->type.atype != lttng_ust_ctl_atype_integer) {
28ab034a
JG
198 LTTNG_THROW_PROTOCOL_ERROR(
199 fmt::format("Invalid type of nestable enum container: type id = {}",
200 current->type.atype));
d7bfb9b0
JG
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 ?
28ab034a
JG
215 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
216 session_attributes._native_trace_byte_order;
24ed18f2 217 const auto signedness = enum_container_uctl_type->signedness ?
28ab034a
JG
218 lst::integer_type::signedness::SIGNED :
219 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 220
24ed18f2 221 if (signedness == lst::integer_type::signedness::SIGNED) {
d7bfb9b0 222 const auto& enum_registry = static_cast<const lsu::registry_signed_enum&>(
28ab034a 223 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
224
225 return lttng::make_unique<const lst::signed_enumeration_type>(
28ab034a
JG
226 enum_container_uctl_type->alignment,
227 byte_order,
228 enum_container_uctl_type->size,
229 base,
230 enum_registry._mappings);
d7bfb9b0
JG
231 } else {
232 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
28ab034a 233 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
234
235 return lttng::make_unique<const lst::unsigned_enumeration_type>(
28ab034a
JG
236 enum_container_uctl_type->alignment,
237 byte_order,
238 enum_container_uctl_type->size,
239 base,
240 enum_registry._mappings);
d7bfb9b0
JG
241 }
242}
243
28ab034a
JG
244lst::type::cuptr
245create_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)))
d7bfb9b0
JG
251{
252 if (current >= end) {
28ab034a
JG
253 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
254 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
255 }
256
257 const auto& string_uctl_field = *current;
258 *next_ust_ctl_field = current + 1;
259
28ab034a
JG
260 const auto encoding =
261 ust_ctl_encoding_to_string_field_encoding(string_uctl_field.type.u.string.encoding);
d7bfb9b0
JG
262
263 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
264}
265
28ab034a
JG
266lst::type::cuptr
267create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type& type,
268 const session_attributes& session_attributes)
d7bfb9b0
JG
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 ?
28ab034a
JG
274 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
275 session_attributes._native_trace_byte_order;
d7bfb9b0 276 const auto signedness = type.u.basic.integer.signedness ?
28ab034a
JG
277 lst::integer_type::signedness::SIGNED :
278 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0
JG
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>(
28ab034a 284 alignment, byte_order, size, signedness, base);
d7bfb9b0
JG
285}
286
28ab034a
JG
287lst::type::cuptr
288create_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)))
d7bfb9b0
JG
293{
294 if (current >= end) {
28ab034a
JG
295 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
296 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
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));
d7bfb9b0
JG
313 }
314
28ab034a
JG
315 element_type =
316 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
d7bfb9b0 317 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 318 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
319 /* Element represents a text character. */
320 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 321 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
322 }
323
324 *next_ust_ctl_field = current + 1;
325
326 if (element_encoding) {
327 const auto integer_element_size =
28ab034a 328 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
329
330 if (integer_element_size != 8) {
331 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
332 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
333 integer_element_size));
d7bfb9b0
JG
334 }
335
336 /* Array is a static-length string. */
337 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 338 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
339 }
340
341 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 342 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
343}
344
28ab034a
JG
345lst::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,
cd9adb8b
JG
350 const publish_field_fn& publish_field,
351 const lookup_field_fn& lookup_field,
28ab034a
JG
352 lst::field_location::root lookup_root,
353 lst::field_location::elements& current_field_location_elements,
354 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
355{
356 if (current >= end) {
28ab034a
JG
357 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
358 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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. */
28ab034a
JG
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);
d7bfb9b0 382 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 383 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
384 /* Element represents a text character. */
385 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 386 element_uctl_field.type.u.integer.encoding);
d7bfb9b0
JG
387 }
388
389 if (element_encoding) {
390 const auto integer_element_size =
28ab034a 391 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
392
393 if (integer_element_size != 8) {
394 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
395 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
396 integer_element_size));
d7bfb9b0
JG
397 }
398
399 /* Array is a static-length string. */
400 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 401 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
402 }
403
404 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 405 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
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 */
28ab034a
JG
412lst::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,
cd9adb8b 417 const publish_field_fn& publish_field,
28ab034a
JG
418 lst::field_location::root lookup_root,
419 lst::field_location::elements& current_field_location_elements,
420 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
421{
422 if (current >= end) {
28ab034a
JG
423 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
424 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
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));
d7bfb9b0
JG
437 }
438
439 if (length_uctl_type.atype != lttng_ust_ctl_atype_integer) {
440 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
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));
d7bfb9b0
JG
444 }
445
446 nonstd::optional<enum lst::string_type::encoding> element_encoding;
447 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 448 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
449 /* Element represents a text character. */
450 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 451 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
452 }
453
cd9adb8b 454 auto length_field_name = fmt::format("_{}_length", sequence_uctl_field.name);
28ab034a
JG
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);
d7bfb9b0 459
eda1aa02 460 lst::field_location::elements length_field_location_elements =
28ab034a 461 current_field_location_elements;
eda1aa02
JG
462 length_field_location_elements.emplace_back(length_field_name);
463
cd9adb8b
JG
464 lst::field_location length_field_location{ lookup_root,
465 std::move(length_field_location_elements) };
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,
cd9adb8b
JG
497 const publish_field_fn& publish_field,
498 const lookup_field_fn& lookup_field,
28ab034a
JG
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;
5c7248cd 542 length_field_location_elements.emplace_back(length_field_name);
eda1aa02 543
cd9adb8b
JG
544 lst::field_location length_field_location{ lookup_root,
545 std::move(length_field_location_elements) };
eda1aa02 546
6e01cdc6 547 /* Validate existence of length field (throws if not found). */
28ab034a 548 const auto& length_field = lookup_field(length_field_location);
6e01cdc6 549 const auto *integer_selector_field =
28ab034a 550 dynamic_cast<const lst::integer_type *>(&length_field.get_type());
6e01cdc6 551 if (!integer_selector_field) {
28ab034a
JG
552 LTTNG_THROW_PROTOCOL_ERROR(
553 "Invalid selector field type referenced from sequence: expected integer or enumeration");
6e01cdc6
JG
554 }
555
d7bfb9b0
JG
556 if (element_encoding) {
557 const auto integer_element_size =
28ab034a 558 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
559
560 if (integer_element_size != 8) {
561 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
28ab034a
JG
562 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
563 integer_element_size));
d7bfb9b0
JG
564 }
565
566 /* Sqeuence is a dynamic-length string. */
28ab034a
JG
567 return lttng::make_unique<lst::dynamic_length_string_type>(
568 sequence_alignment, *element_encoding, std::move(length_field_location));
d7bfb9b0
JG
569 }
570
28ab034a
JG
571 return lttng::make_unique<lst::dynamic_length_array_type>(
572 sequence_alignment, std::move(element_type), std::move(length_field_location));
d7bfb9b0
JG
573}
574
28ab034a
JG
575lst::type::cuptr
576create_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)))
d7bfb9b0
JG
582{
583 if (current >= end) {
28ab034a
JG
584 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
585 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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(
28ab034a
JG
602 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
603 field_count));
d7bfb9b0
JG
604 }
605
606 *next_ust_ctl_field = current + 1;
607 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
608}
609
45110cdd 610template <class VariantSelectorMappingIntegerType>
28ab034a
JG
611typename lst::variant_type<VariantSelectorMappingIntegerType>::choices
612create_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)
45110cdd
JG
622{
623 typename lst::variant_type<VariantSelectorMappingIntegerType>::choices choices;
28ab034a
JG
624 const auto& typed_enumeration =
625 static_cast<const lst::typed_enumeration_type<VariantSelectorMappingIntegerType>&>(
45110cdd
JG
626 selector_field.get_type());
627
628 for (unsigned int i = 0; i < choice_count; i++) {
629 create_field_from_ust_ctl_fields(
28ab034a
JG
630 current,
631 end,
632 session_attributes,
633 next_ust_ctl_field,
9d89db29 634 [&choices, &typed_enumeration, &selector_field, quirks](
28ab034a
JG
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);
45110cdd
JG
679
680 current = *next_ust_ctl_field;
681 }
682
683 return choices;
684}
685
28ab034a
JG
686lst::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,
cd9adb8b 691 const lookup_field_fn& lookup_field,
28ab034a
JG
692 lst::field_location::root lookup_root,
693 lst::field_location::elements& current_field_location_elements,
694 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
695{
696 if (current >= end) {
28ab034a
JG
697 LTTNG_THROW_PROTOCOL_ERROR(fmt::format(
698 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
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
eda1aa02 718 lst::field_location::elements selector_field_location_elements =
28ab034a 719 current_field_location_elements;
eda1aa02
JG
720 selector_field_location_elements.emplace_back(tag_name);
721
cd9adb8b
JG
722 lst::field_location selector_field_location{ lookup_root,
723 std::move(selector_field_location_elements) };
eda1aa02 724
6e01cdc6 725 /* Validate existence of selector field (throws if not found). */
28ab034a 726 const auto& selector_field = lookup_field(selector_field_location);
45110cdd 727 const auto *enumeration_selector_type =
28ab034a 728 dynamic_cast<const lst::enumeration_type *>(&selector_field.get_type());
45110cdd 729 if (!enumeration_selector_type) {
28ab034a
JG
730 LTTNG_THROW_PROTOCOL_ERROR(
731 "Invalid selector field type referenced from variant: expected enumeration");
6e01cdc6
JG
732 }
733
45110cdd 734 const bool selector_is_signed = enumeration_selector_type->signedness_ ==
28ab034a 735 lst::integer_type::signedness::SIGNED;
45110cdd 736
d7bfb9b0 737 /* Choices follow. next_ust_ctl_field is updated as needed. */
45110cdd
JG
738 if (selector_is_signed) {
739 lst::variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::
28ab034a
JG
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);
45110cdd
JG
751
752 return lttng::make_unique<lst::variant_type<int64_t>>(
28ab034a 753 alignment, std::move(selector_field_location), std::move(choices));
45110cdd 754 } else {
28ab034a
JG
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);
d7bfb9b0 768
45110cdd 769 return lttng::make_unique<lst::variant_type<uint64_t>>(
28ab034a 770 alignment, std::move(selector_field_location), std::move(choices));
d7bfb9b0 771 }
d7bfb9b0
JG
772}
773
28ab034a
JG
774lst::type::cuptr
775create_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,
cd9adb8b
JG
779 const publish_field_fn& publish_field,
780 const lookup_field_fn& lookup_field,
28ab034a
JG
781 lst::field_location::root lookup_root,
782 lst::field_location::elements& current_field_location_elements,
783 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
784{
785 switch (current->type.atype) {
786 case lttng_ust_ctl_atype_integer:
787 return create_integer_type_from_ust_ctl_fields(
28ab034a 788 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
789 case lttng_ust_ctl_atype_enum:
790 case lttng_ust_ctl_atype_enum_nestable:
791 return create_enumeration_type_from_ust_ctl_fields(
28ab034a 792 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
793 case lttng_ust_ctl_atype_float:
794 return create_floating_point_type_from_ust_ctl_fields(
28ab034a 795 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
796 case lttng_ust_ctl_atype_string:
797 return create_string_type_from_ust_ctl_fields(
28ab034a 798 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 799 case lttng_ust_ctl_atype_array:
63c3462c 800 return create_array_type_from_ust_ctl_fields(
28ab034a 801 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 802 case lttng_ust_ctl_atype_array_nestable:
28ab034a
JG
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);
d7bfb9b0 813 case lttng_ust_ctl_atype_sequence:
28ab034a
JG
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);
d7bfb9b0 822 case lttng_ust_ctl_atype_sequence_nestable:
28ab034a
JG
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);
d7bfb9b0
JG
833 case lttng_ust_ctl_atype_struct:
834 case lttng_ust_ctl_atype_struct_nestable:
835 return create_structure_field_from_ust_ctl_fields(
28ab034a 836 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
837 case lttng_ust_ctl_atype_variant:
838 case lttng_ust_ctl_atype_variant_nestable:
28ab034a
JG
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);
d7bfb9b0 847 default:
28ab034a
JG
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)));
d7bfb9b0
JG
854 }
855}
856
857void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
858 const lttng_ust_ctl_field *end,
859 const session_attributes& session_attributes,
860 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
861 const publish_field_fn& publish_field,
862 const lookup_field_fn& lookup_field,
28ab034a
JG
863 lst::field_location::root lookup_root,
864 lst::field_location::elements& current_field_location_elements,
865 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
866{
867 LTTNG_ASSERT(current < end);
868
869 if (lttng_strnlen(current->name, sizeof(current->name)) == sizeof(current->name)) {
870 LTTNG_THROW_PROTOCOL_ERROR(
28ab034a
JG
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)));
d7bfb9b0
JG
885}
886
28ab034a
JG
887std::vector<lst::field::cuptr>::iterator
888lookup_field_in_vector(std::vector<lst::field::cuptr>& fields, const lst::field_location& location)
da9dd521
JG
889{
890 if (location.elements_.size() != 1) {
891 LTTNG_THROW_ERROR(fmt::format(
28ab034a
JG
892 "Unexpected field location received during field look-up: location = {}",
893 location));
da9dd521
JG
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 */
28ab034a
JG
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 });
da9dd521
JG
907
908 if (field_it == fields.end()) {
909 LTTNG_THROW_PROTOCOL_ERROR(
28ab034a 910 fmt::format("Failed to look-up field: location = {}", location));
da9dd521
JG
911 }
912
913 return field_it;
914}
915
d7bfb9b0
JG
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 */
28ab034a
JG
924std::vector<lst::field::cuptr>
925create_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)
d7bfb9b0
JG
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{
28ab034a
JG
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 };
eda1aa02
JG
939 /* Location of field being created. */
940 lst::field_location::elements current_field_location_elements;
d7bfb9b0
JG
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 */
6e01cdc6 954 create_field_from_ust_ctl_fields(
28ab034a
JG
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);
d7bfb9b0
JG
972
973 current = next_field;
974 }
975
976 return fields;
977}
978} /* namespace */
979
28ab034a
JG
980std::vector<lst::field::cuptr>
981lsu::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)
d7bfb9b0 986{
63c3462c 987 return create_fields_from_ust_ctl_fields(
28ab034a 988 session, fields, fields + field_count, lookup_root, quirks);
d7bfb9b0 989}
This page took 0.073199 seconds and 4 git commands to generate.