vscode: Add configurations to run the executables under the debugger
[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;
d7bfb9b0 18
8b75cd77
JG
19/*
20 * fmtlib helper that must be under the same namespace as lttng_ust_ctl_abstract_types
21 * (global).
22 */
23static int format_as(lttng_ust_ctl_abstract_types type)
24{
25 return fmt::underlying(type);
26}
27
28namespace {
d7bfb9b0
JG
29/*
30 * Type enclosing the session information that may be required during the decoding
31 * of the lttng_ust_ctl_field array provided by applications on registration of
32 * an event.
33 */
34class session_attributes {
35public:
36 using registry_enum_getter_fn =
28ab034a
JG
37 std::function<lsu::registry_enum::const_rcu_protected_reference(const char *name,
38 uint64_t id)>;
d7bfb9b0
JG
39
40 session_attributes(registry_enum_getter_fn reg_enum_getter,
28ab034a 41 lst::byte_order native_trace_byte_order) :
cd9adb8b 42 get_registry_enum{ std::move(reg_enum_getter) },
28ab034a 43 _native_trace_byte_order{ native_trace_byte_order }
d7bfb9b0
JG
44 {
45 }
46
47 const registry_enum_getter_fn get_registry_enum;
48 const lst::byte_order _native_trace_byte_order;
49};
50
51/* Used to publish fields on which a field being decoded has an implicit dependency. */
45110cdd 52using publish_field_fn = std::function<void(lst::field::uptr)>;
d7bfb9b0 53
6e01cdc6 54/* Look-up field from a field location. */
28ab034a
JG
55using lookup_field_fn = std::function<const lst::field&(const lst::field_location&)>;
56
57lst::type::cuptr
58create_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
59 const lttng_ust_ctl_field *end,
60 const session_attributes& session_attributes,
61 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
62 const publish_field_fn& publish_field,
63 const lookup_field_fn& lookup_field,
28ab034a
JG
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
68void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
69 const lttng_ust_ctl_field *end,
70 const session_attributes& session_attributes,
71 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
72 const publish_field_fn& publish_field,
73 const lookup_field_fn& lookup_field,
28ab034a
JG
74 lst::field_location::root lookup_root,
75 lst::field_location::elements& current_field_location_elements,
76 lsu::ctl_field_quirks quirks);
d7bfb9b0
JG
77
78template <class UstCtlEncodingType>
28ab034a
JG
79enum lst::null_terminated_string_type::encoding
80ust_ctl_encoding_to_string_field_encoding(UstCtlEncodingType encoding)
d7bfb9b0 81{
28ab034a
JG
82 static const std::unordered_map<UstCtlEncodingType,
83 enum lst::null_terminated_string_type::encoding>
84 encoding_conversion_map = {
85 { (UstCtlEncodingType) lttng_ust_ctl_encode_ASCII,
86 lst::null_terminated_string_type::encoding::ASCII },
87 { (UstCtlEncodingType) lttng_ust_ctl_encode_UTF8,
88 lst::null_terminated_string_type::encoding::UTF8 },
89 };
d7bfb9b0
JG
90
91 const auto encoding_it = encoding_conversion_map.find(encoding);
92 if (encoding_it == encoding_conversion_map.end()) {
f9a41357 93 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
94 "Unknown lttng_ust_ctl_string_encodings value `{}` encountered when decoding integer field",
95 encoding));
d7bfb9b0
JG
96 }
97
98 return encoding_it->second;
99}
100
101template <class UstCtlBaseType>
102enum lst::integer_type::base ust_ctl_base_to_integer_field_base(UstCtlBaseType base)
103{
104 static const std::unordered_map<UstCtlBaseType, enum lst::integer_type::base>
28ab034a
JG
105 base_conversion_map = { { 2, lst::integer_type::base::BINARY },
106 { 8, lst::integer_type::base::OCTAL },
107 { 10, lst::integer_type::base::DECIMAL },
108 { 16, lst::integer_type::base::HEXADECIMAL } };
d7bfb9b0
JG
109
110 const auto base_it = base_conversion_map.find(base);
111 if (base_it == base_conversion_map.end()) {
f9a41357 112 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
113 "Unknown integer base value `{}` encountered when decoding integer field",
114 base));
d7bfb9b0
JG
115 }
116
117 return base_it->second;
118}
119
28ab034a
JG
120lst::type::cuptr
121create_integer_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
122 const lttng_ust_ctl_field *end,
123 const session_attributes& session_attributes,
124 const lttng_ust_ctl_field **next_ust_ctl_field,
125 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
126{
127 if (current >= end) {
f9a41357 128 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 129 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
130 }
131
132 const auto base = ust_ctl_base_to_integer_field_base(current->type.u.integer.base);
133 const auto signedness = current->type.u.integer.signedness ?
28ab034a
JG
134 lst::integer_type::signedness::SIGNED :
135 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 136 const auto byte_order = current->type.u.integer.reverse_byte_order ?
28ab034a
JG
137 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
138 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
139
140 *next_ust_ctl_field = current + 1;
141
142 return lttng::make_unique<const lst::integer_type>(current->type.u.integer.alignment,
28ab034a
JG
143 byte_order,
144 current->type.u.integer.size,
145 signedness,
146 base);
d7bfb9b0
JG
147}
148
28ab034a
JG
149lst::type::cuptr
150create_floating_point_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
151 const lttng_ust_ctl_field *end,
152 const session_attributes& session_attributes,
153 const lttng_ust_ctl_field **next_ust_ctl_field,
154 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
155{
156 if (current >= end) {
f9a41357 157 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 158 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
159 }
160
161 *next_ust_ctl_field = current + 1;
162
163 const auto byte_order = current->type.u._float.reverse_byte_order ?
28ab034a
JG
164 lst::type::reverse_byte_order(session_attributes._native_trace_byte_order) :
165 session_attributes._native_trace_byte_order;
d7bfb9b0
JG
166
167 try {
168 return lttng::make_unique<const lst::floating_point_type>(
28ab034a
JG
169 current->type.u._float.alignment,
170 byte_order,
171 current->type.u._float.exp_dig,
172 current->type.u._float.mant_dig);
d7bfb9b0 173 } catch (lttng::invalid_argument_error& ex) {
f9a41357 174 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 175 "Invalid floating point attribute in {}: {}", typeid(*current), ex.what()));
d7bfb9b0
JG
176 }
177}
178
28ab034a
JG
179lst::type::cuptr
180create_enumeration_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
181 const lttng_ust_ctl_field *end,
182 const session_attributes& session_attributes,
183 const lttng_ust_ctl_field **next_ust_ctl_field,
184 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
185{
186 if (current >= end) {
f9a41357 187 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 188 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
189 }
190
191 uint64_t enumeration_id;
192 const auto& enum_uctl_field = *current;
193 const char *enumeration_name;
194 const auto *enum_container_uctl_type =
28ab034a 195 &current->type.u.legacy.basic.enumeration.container_type;
d7bfb9b0
JG
196
197 if (enum_uctl_field.type.atype == lttng_ust_ctl_atype_enum_nestable) {
198 /* Nestable enumeration fields are followed by their container type. */
199 ++current;
200 if (current >= end) {
f9a41357 201 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
202 "Array of {} is too short to contain nestable enumeration's container",
203 typeid(*current)));
d7bfb9b0
JG
204 }
205
206 if (current->type.atype != lttng_ust_ctl_atype_integer) {
f9a41357
JG
207 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
208 "Invalid type of nestable enum container: type id = {}",
209 current->type.atype));
d7bfb9b0
JG
210 }
211
212 enum_container_uctl_type = &current->type.u.integer;
213 enumeration_id = enum_uctl_field.type.u.enum_nestable.id;
214 enumeration_name = enum_uctl_field.type.u.enum_nestable.name;
215 } else {
216 enumeration_id = enum_uctl_field.type.u.legacy.basic.enumeration.id;
217 enumeration_name = enum_uctl_field.type.u.legacy.basic.enumeration.name;
218 }
219
220 *next_ust_ctl_field = current + 1;
221
222 const auto base = ust_ctl_base_to_integer_field_base(enum_container_uctl_type->base);
223 const auto byte_order = enum_container_uctl_type->reverse_byte_order ?
28ab034a
JG
224 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
225 session_attributes._native_trace_byte_order;
24ed18f2 226 const auto signedness = enum_container_uctl_type->signedness ?
28ab034a
JG
227 lst::integer_type::signedness::SIGNED :
228 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0 229
24ed18f2 230 if (signedness == lst::integer_type::signedness::SIGNED) {
d7bfb9b0 231 const auto& enum_registry = static_cast<const lsu::registry_signed_enum&>(
28ab034a 232 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
233
234 return lttng::make_unique<const lst::signed_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 } else {
241 const auto& enum_registry = static_cast<const lsu::registry_unsigned_enum&>(
28ab034a 242 *session_attributes.get_registry_enum(enumeration_name, enumeration_id));
d7bfb9b0
JG
243
244 return lttng::make_unique<const lst::unsigned_enumeration_type>(
28ab034a
JG
245 enum_container_uctl_type->alignment,
246 byte_order,
247 enum_container_uctl_type->size,
248 base,
249 enum_registry._mappings);
d7bfb9b0
JG
250 }
251}
252
28ab034a
JG
253lst::type::cuptr
254create_string_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
255 const lttng_ust_ctl_field *end,
256 const session_attributes& session_attributes
257 __attribute__((unused)),
258 const lttng_ust_ctl_field **next_ust_ctl_field,
259 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
260{
261 if (current >= end) {
f9a41357 262 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 263 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
264 }
265
266 const auto& string_uctl_field = *current;
267 *next_ust_ctl_field = current + 1;
268
28ab034a
JG
269 const auto encoding =
270 ust_ctl_encoding_to_string_field_encoding(string_uctl_field.type.u.string.encoding);
d7bfb9b0
JG
271
272 return lttng::make_unique<const lst::null_terminated_string_type>(1, encoding);
273}
274
28ab034a
JG
275lst::type::cuptr
276create_integer_type_from_ust_ctl_basic_type(const lttng_ust_ctl_basic_type& type,
277 const session_attributes& session_attributes)
d7bfb9b0
JG
278{
279 /* Checked by caller. */
280 LTTNG_ASSERT(type.atype == lttng_ust_ctl_atype_integer);
281
282 const auto byte_order = type.u.basic.integer.reverse_byte_order ?
28ab034a
JG
283 lst::integer_type::reverse_byte_order(session_attributes._native_trace_byte_order) :
284 session_attributes._native_trace_byte_order;
d7bfb9b0 285 const auto signedness = type.u.basic.integer.signedness ?
28ab034a
JG
286 lst::integer_type::signedness::SIGNED :
287 lst::integer_type::signedness::UNSIGNED;
d7bfb9b0
JG
288 const auto base = ust_ctl_base_to_integer_field_base(type.u.basic.integer.base);
289 const auto size = type.u.basic.integer.size;
290 const auto alignment = type.u.basic.integer.alignment;
291
292 return lttng::make_unique<const lst::integer_type>(
28ab034a 293 alignment, byte_order, size, signedness, base);
d7bfb9b0
JG
294}
295
28ab034a
JG
296lst::type::cuptr
297create_array_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
298 const lttng_ust_ctl_field *end,
299 const session_attributes& session_attributes,
300 const lttng_ust_ctl_field **next_ust_ctl_field,
301 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
302{
303 if (current >= end) {
f9a41357 304 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 305 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
306 }
307
308 const auto& array_uctl_field = *current;
309 uint32_t array_alignment, array_length;
310 lst::type::cuptr element_type;
311 nonstd::optional<enum lst::string_type::encoding> element_encoding;
312
313 array_length = array_uctl_field.type.u.legacy.array.length;
314 array_alignment = 0;
315
316 const auto& element_uctl_type = array_uctl_field.type.u.legacy.array.elem_type;
317 if (element_uctl_type.atype != lttng_ust_ctl_atype_integer) {
f9a41357 318 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
319 "Unexpected legacy array element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
320 element_uctl_type.atype,
321 lttng_ust_ctl_atype_integer));
d7bfb9b0
JG
322 }
323
28ab034a
JG
324 element_type =
325 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
d7bfb9b0 326 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 327 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
328 /* Element represents a text character. */
329 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 330 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
331 }
332
333 *next_ust_ctl_field = current + 1;
334
335 if (element_encoding) {
336 const auto integer_element_size =
28ab034a 337 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
338
339 if (integer_element_size != 8) {
f9a41357 340 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
341 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
342 integer_element_size));
d7bfb9b0
JG
343 }
344
345 /* Array is a static-length string. */
346 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 347 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
348 }
349
350 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 351 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
352}
353
28ab034a
JG
354lst::type::cuptr create_array_nestable_type_from_ust_ctl_fields(
355 const lttng_ust_ctl_field *current,
356 const lttng_ust_ctl_field *end,
357 const session_attributes& session_attributes,
358 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
359 const publish_field_fn& publish_field,
360 const lookup_field_fn& lookup_field,
28ab034a
JG
361 lst::field_location::root lookup_root,
362 lst::field_location::elements& current_field_location_elements,
363 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
364{
365 if (current >= end) {
f9a41357 366 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 367 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
368 }
369
370 const auto& array_uctl_field = *current;
371 uint32_t array_alignment, array_length;
372 lst::type::cuptr element_type;
373 nonstd::optional<enum lst::string_type::encoding> element_encoding;
374
375 array_length = array_uctl_field.type.u.array_nestable.length;
376 array_alignment = array_uctl_field.type.u.array_nestable.alignment;
377
378 /* Nestable array fields are followed by their element type. */
379 const auto& element_uctl_field = *(current + 1);
380
381 /* next_ust_ctl_field is updated as needed. */
28ab034a
JG
382 element_type = create_type_from_ust_ctl_fields(&element_uctl_field,
383 end,
384 session_attributes,
385 next_ust_ctl_field,
386 publish_field,
387 lookup_field,
388 lookup_root,
389 current_field_location_elements,
390 quirks);
d7bfb9b0 391 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 392 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
393 /* Element represents a text character. */
394 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 395 element_uctl_field.type.u.integer.encoding);
d7bfb9b0
JG
396 }
397
398 if (element_encoding) {
399 const auto integer_element_size =
28ab034a 400 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
401
402 if (integer_element_size != 8) {
f9a41357 403 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
404 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
405 integer_element_size));
d7bfb9b0
JG
406 }
407
408 /* Array is a static-length string. */
409 return lttng::make_unique<lst::static_length_string_type>(
28ab034a 410 array_alignment, *element_encoding, array_length);
d7bfb9b0
JG
411 }
412
413 return lttng::make_unique<lst::static_length_array_type>(
28ab034a 414 array_alignment, std::move(element_type), array_length);
d7bfb9b0
JG
415}
416
417/*
418 * For legacy sequence types, LTTng-UST expresses both the sequence and sequence
419 * length as part of the same lttng_ust_ctl_field entry.
420 */
28ab034a
JG
421lst::type::cuptr create_sequence_type_from_ust_ctl_fields(
422 const lttng_ust_ctl_field *current,
423 const lttng_ust_ctl_field *end,
424 const session_attributes& session_attributes,
425 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b 426 const publish_field_fn& publish_field,
28ab034a
JG
427 lst::field_location::root lookup_root,
428 lst::field_location::elements& current_field_location_elements,
429 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
430{
431 if (current >= end) {
f9a41357 432 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 433 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
434 }
435
436 const auto& sequence_uctl_field = *current;
437 const auto& element_uctl_type = sequence_uctl_field.type.u.legacy.sequence.elem_type;
438 const auto& length_uctl_type = sequence_uctl_field.type.u.legacy.sequence.length_type;
439 const auto sequence_alignment = 0U;
440
441 if (element_uctl_type.atype != lttng_ust_ctl_atype_integer) {
f9a41357 442 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
443 "Unexpected legacy sequence element type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
444 element_uctl_type.atype,
445 lttng_ust_ctl_atype_integer));
d7bfb9b0
JG
446 }
447
448 if (length_uctl_type.atype != lttng_ust_ctl_atype_integer) {
f9a41357 449 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
450 "Unexpected legacy sequence length field type: atype = {}, expected atype = lttng_ust_ctl_atype_integer ({})",
451 length_uctl_type.atype,
452 lttng_ust_ctl_atype_integer));
d7bfb9b0
JG
453 }
454
455 nonstd::optional<enum lst::string_type::encoding> element_encoding;
456 if (element_uctl_type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 457 element_uctl_type.u.basic.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
458 /* Element represents a text character. */
459 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 460 element_uctl_type.u.basic.integer.encoding);
d7bfb9b0
JG
461 }
462
f9a41357 463 auto length_field_name = lttng::format("_{}_length", sequence_uctl_field.name);
28ab034a
JG
464 auto element_type =
465 create_integer_type_from_ust_ctl_basic_type(element_uctl_type, session_attributes);
466 auto length_type =
467 create_integer_type_from_ust_ctl_basic_type(length_uctl_type, session_attributes);
d7bfb9b0 468
eda1aa02 469 lst::field_location::elements length_field_location_elements =
28ab034a 470 current_field_location_elements;
eda1aa02
JG
471 length_field_location_elements.emplace_back(length_field_name);
472
cd9adb8b
JG
473 lst::field_location length_field_location{ lookup_root,
474 std::move(length_field_location_elements) };
eda1aa02 475
d7bfb9b0 476 /* Publish an implicit length field _before_ the sequence field. */
28ab034a
JG
477 publish_field(lttng::make_unique<lst::field>(std::move(length_field_name),
478 std::move(length_type)));
d7bfb9b0
JG
479
480 *next_ust_ctl_field = current + 1;
481
482 if (element_encoding) {
483 const auto integer_element_size =
28ab034a 484 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
485
486 if (integer_element_size != 8) {
f9a41357 487 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
488 "Unexpected legacy array element type: integer has encoding but size is not 8: size = {}",
489 integer_element_size));
d7bfb9b0
JG
490 }
491
492 /* Sequence is a dynamic-length string. */
28ab034a
JG
493 return lttng::make_unique<lst::dynamic_length_string_type>(
494 sequence_alignment, *element_encoding, std::move(length_field_location));
d7bfb9b0
JG
495 }
496
28ab034a
JG
497 return lttng::make_unique<lst::dynamic_length_array_type>(
498 sequence_alignment, std::move(element_type), std::move(length_field_location));
d7bfb9b0
JG
499}
500
501lst::type::cuptr create_sequence_nestable_type_from_ust_ctl_fields(
28ab034a
JG
502 const lttng_ust_ctl_field *current,
503 const lttng_ust_ctl_field *end,
504 const session_attributes& session_attributes,
505 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
506 const publish_field_fn& publish_field,
507 const lookup_field_fn& lookup_field,
28ab034a
JG
508 lst::field_location::root lookup_root,
509 lst::field_location::elements& current_field_location_elements,
510 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
511{
512 if (current >= end) {
f9a41357 513 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 514 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
515 }
516
517 const auto& sequence_uctl_field = *current;
518 const auto sequence_alignment = sequence_uctl_field.type.u.sequence_nestable.alignment;
519 const auto *length_field_name = sequence_uctl_field.type.u.sequence_nestable.length_name;
520
521 /* Nestable sequence fields are followed by their element type. */
522 const auto& element_uctl_field = *(current + 1);
523
524 nonstd::optional<enum lst::string_type::encoding> element_encoding;
525 if (element_uctl_field.type.atype == lttng_ust_ctl_atype_integer &&
28ab034a 526 element_uctl_field.type.u.integer.encoding != lttng_ust_ctl_encode_none) {
d7bfb9b0
JG
527 /* Element represents a text character. */
528 element_encoding = ust_ctl_encoding_to_string_field_encoding(
28ab034a 529 element_uctl_field.type.u.integer.encoding);
d7bfb9b0
JG
530 }
531
532 /* next_ust_ctl_field is updated as needed. */
28ab034a
JG
533 auto element_type = create_type_from_ust_ctl_fields(&element_uctl_field,
534 end,
535 session_attributes,
536 next_ust_ctl_field,
537 publish_field,
538 lookup_field,
539 lookup_root,
540 current_field_location_elements,
541 quirks);
d7bfb9b0
JG
542
543 if (lttng_strnlen(sequence_uctl_field.type.u.sequence_nestable.length_name,
28ab034a
JG
544 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) ==
545 sizeof(sequence_uctl_field.type.u.sequence_nestable.length_name)) {
d7bfb9b0
JG
546 LTTNG_THROW_PROTOCOL_ERROR("Sequence length field name is not null terminated");
547 }
548
eda1aa02 549 lst::field_location::elements length_field_location_elements =
28ab034a 550 current_field_location_elements;
5c7248cd 551 length_field_location_elements.emplace_back(length_field_name);
eda1aa02 552
cd9adb8b
JG
553 lst::field_location length_field_location{ lookup_root,
554 std::move(length_field_location_elements) };
eda1aa02 555
6e01cdc6 556 /* Validate existence of length field (throws if not found). */
28ab034a 557 const auto& length_field = lookup_field(length_field_location);
6e01cdc6 558 const auto *integer_selector_field =
28ab034a 559 dynamic_cast<const lst::integer_type *>(&length_field.get_type());
6e01cdc6 560 if (!integer_selector_field) {
28ab034a
JG
561 LTTNG_THROW_PROTOCOL_ERROR(
562 "Invalid selector field type referenced from sequence: expected integer or enumeration");
6e01cdc6
JG
563 }
564
d7bfb9b0
JG
565 if (element_encoding) {
566 const auto integer_element_size =
28ab034a 567 static_cast<const lst::integer_type&>(*element_type).size;
d7bfb9b0
JG
568
569 if (integer_element_size != 8) {
f9a41357 570 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
571 "Unexpected array element type: integer has encoding but size is not 8: size = {}",
572 integer_element_size));
d7bfb9b0
JG
573 }
574
575 /* Sqeuence is a dynamic-length string. */
28ab034a
JG
576 return lttng::make_unique<lst::dynamic_length_string_type>(
577 sequence_alignment, *element_encoding, std::move(length_field_location));
d7bfb9b0
JG
578 }
579
28ab034a
JG
580 return lttng::make_unique<lst::dynamic_length_array_type>(
581 sequence_alignment, std::move(element_type), std::move(length_field_location));
d7bfb9b0
JG
582}
583
28ab034a
JG
584lst::type::cuptr
585create_structure_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
586 const lttng_ust_ctl_field *end,
587 const session_attributes& session_attributes
588 __attribute__((unused)),
589 const lttng_ust_ctl_field **next_ust_ctl_field,
590 lsu::ctl_field_quirks quirks __attribute__((unused)))
d7bfb9b0
JG
591{
592 if (current >= end) {
f9a41357 593 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 594 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
595 }
596
597 uint32_t field_count;
598 uint32_t alignment;
599 const auto& structure_uctl_field = *current;
600
601 if (structure_uctl_field.type.atype == lttng_ust_ctl_atype_struct) {
602 field_count = structure_uctl_field.type.u.legacy._struct.nr_fields;
603 alignment = 0;
604 } else {
605 field_count = structure_uctl_field.type.u.struct_nestable.nr_fields;
606 alignment = structure_uctl_field.type.u.struct_nestable.alignment;
607 }
608
609 if (field_count != 0) {
f9a41357 610 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
611 "Only empty structures are supported by LTTng-UST: nr_fields = {}",
612 field_count));
d7bfb9b0
JG
613 }
614
615 *next_ust_ctl_field = current + 1;
616 return lttng::make_unique<lst::structure_type>(alignment, lst::structure_type::fields());
617}
618
45110cdd 619template <class VariantSelectorMappingIntegerType>
28ab034a
JG
620typename lst::variant_type<VariantSelectorMappingIntegerType>::choices
621create_typed_variant_choices(const lttng_ust_ctl_field *current,
622 const lttng_ust_ctl_field *end,
623 const session_attributes& session_attributes,
624 const lttng_ust_ctl_field **next_ust_ctl_field,
625 lookup_field_fn lookup_field,
626 lst::field_location::root lookup_root,
627 lst::field_location::elements& current_field_location_elements,
628 unsigned int choice_count,
629 const lst::field& selector_field,
630 lsu::ctl_field_quirks quirks)
45110cdd
JG
631{
632 typename lst::variant_type<VariantSelectorMappingIntegerType>::choices choices;
28ab034a
JG
633 const auto& typed_enumeration =
634 static_cast<const lst::typed_enumeration_type<VariantSelectorMappingIntegerType>&>(
45110cdd
JG
635 selector_field.get_type());
636
637 for (unsigned int i = 0; i < choice_count; i++) {
638 create_field_from_ust_ctl_fields(
28ab034a
JG
639 current,
640 end,
641 session_attributes,
642 next_ust_ctl_field,
9d89db29 643 [&choices, &typed_enumeration, &selector_field, quirks](
28ab034a
JG
644 lst::field::uptr field) {
645 /*
646 * Find the enumeration mapping that matches the
647 * field's name.
648 */
649 const auto mapping_it = std::find_if(
650 typed_enumeration.mappings_->begin(),
651 typed_enumeration.mappings_->end(),
652 [&field,
653 quirks](const typename std::remove_reference<
654 decltype(typed_enumeration)>::type::mapping&
655 mapping) {
656 if (static_cast<bool>(
657 quirks &
658 lsu::ctl_field_quirks::
659 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS)) {
660 /*
661 * Check if they match with
662 * a prepended underscore
663 * and, if not, perform the
664 * regular check.
665 */
666 if ((std::string("_") + field->name) ==
667 mapping.name) {
668 return true;
669 }
670 }
671
672 return mapping.name == field->name;
673 });
674
675 if (mapping_it == typed_enumeration.mappings_->end()) {
f9a41357 676 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a
JG
677 "Invalid variant choice: `{}` does not match any mapping in `{}` enumeration",
678 field->name,
679 selector_field.name));
680 }
681
682 choices.emplace_back(*mapping_it, field->move_type());
683 },
684 lookup_field,
685 lookup_root,
686 current_field_location_elements,
687 quirks);
45110cdd
JG
688
689 current = *next_ust_ctl_field;
690 }
691
692 return choices;
693}
694
28ab034a
JG
695lst::type::cuptr create_variant_field_from_ust_ctl_fields(
696 const lttng_ust_ctl_field *current,
697 const lttng_ust_ctl_field *end,
698 const session_attributes& session_attributes,
699 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b 700 const lookup_field_fn& lookup_field,
28ab034a
JG
701 lst::field_location::root lookup_root,
702 lst::field_location::elements& current_field_location_elements,
703 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
704{
705 if (current >= end) {
f9a41357 706 LTTNG_THROW_PROTOCOL_ERROR(lttng::format(
28ab034a 707 "End of {} array reached unexpectedly during decoding", typeid(*current)));
d7bfb9b0
JG
708 }
709
710 const auto& variant_uctl_field = *current;
711 current++;
712
713 uint32_t alignment;
714 uint32_t choice_count;
715 const char *tag_name;
716
717 if (variant_uctl_field.type.atype == lttng_ust_ctl_atype_variant) {
718 alignment = 0;
719 choice_count = variant_uctl_field.type.u.legacy.variant.nr_choices;
720 tag_name = variant_uctl_field.type.u.legacy.variant.tag_name;
721 } else {
722 alignment = variant_uctl_field.type.u.variant_nestable.alignment;
723 choice_count = variant_uctl_field.type.u.variant_nestable.nr_choices;
724 tag_name = variant_uctl_field.type.u.variant_nestable.tag_name;
725 }
726
eda1aa02 727 lst::field_location::elements selector_field_location_elements =
28ab034a 728 current_field_location_elements;
eda1aa02
JG
729 selector_field_location_elements.emplace_back(tag_name);
730
cd9adb8b
JG
731 lst::field_location selector_field_location{ lookup_root,
732 std::move(selector_field_location_elements) };
eda1aa02 733
6e01cdc6 734 /* Validate existence of selector field (throws if not found). */
28ab034a 735 const auto& selector_field = lookup_field(selector_field_location);
45110cdd 736 const auto *enumeration_selector_type =
28ab034a 737 dynamic_cast<const lst::enumeration_type *>(&selector_field.get_type());
45110cdd 738 if (!enumeration_selector_type) {
28ab034a
JG
739 LTTNG_THROW_PROTOCOL_ERROR(
740 "Invalid selector field type referenced from variant: expected enumeration");
6e01cdc6
JG
741 }
742
45110cdd 743 const bool selector_is_signed = enumeration_selector_type->signedness_ ==
28ab034a 744 lst::integer_type::signedness::SIGNED;
45110cdd 745
d7bfb9b0 746 /* Choices follow. next_ust_ctl_field is updated as needed. */
45110cdd
JG
747 if (selector_is_signed) {
748 lst::variant_type<lst::signed_enumeration_type::mapping::range_t::range_integer_t>::
28ab034a
JG
749 choices choices = create_typed_variant_choices<int64_t>(
750 current,
751 end,
752 session_attributes,
753 next_ust_ctl_field,
754 lookup_field,
755 lookup_root,
756 current_field_location_elements,
757 choice_count,
758 selector_field,
759 quirks);
45110cdd
JG
760
761 return lttng::make_unique<lst::variant_type<int64_t>>(
28ab034a 762 alignment, std::move(selector_field_location), std::move(choices));
45110cdd 763 } else {
28ab034a
JG
764 lst::variant_type<
765 lst::unsigned_enumeration_type::mapping::range_t::range_integer_t>::choices
766 choices = create_typed_variant_choices<uint64_t>(
767 current,
768 end,
769 session_attributes,
770 next_ust_ctl_field,
771 lookup_field,
772 lookup_root,
773 current_field_location_elements,
774 choice_count,
775 selector_field,
776 quirks);
d7bfb9b0 777
45110cdd 778 return lttng::make_unique<lst::variant_type<uint64_t>>(
28ab034a 779 alignment, std::move(selector_field_location), std::move(choices));
d7bfb9b0 780 }
d7bfb9b0
JG
781}
782
28ab034a
JG
783lst::type::cuptr
784create_type_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
785 const lttng_ust_ctl_field *end,
786 const session_attributes& session_attributes,
787 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
788 const publish_field_fn& publish_field,
789 const lookup_field_fn& lookup_field,
28ab034a
JG
790 lst::field_location::root lookup_root,
791 lst::field_location::elements& current_field_location_elements,
792 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
793{
794 switch (current->type.atype) {
795 case lttng_ust_ctl_atype_integer:
796 return create_integer_type_from_ust_ctl_fields(
28ab034a 797 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
798 case lttng_ust_ctl_atype_enum:
799 case lttng_ust_ctl_atype_enum_nestable:
800 return create_enumeration_type_from_ust_ctl_fields(
28ab034a 801 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
802 case lttng_ust_ctl_atype_float:
803 return create_floating_point_type_from_ust_ctl_fields(
28ab034a 804 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
805 case lttng_ust_ctl_atype_string:
806 return create_string_type_from_ust_ctl_fields(
28ab034a 807 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 808 case lttng_ust_ctl_atype_array:
63c3462c 809 return create_array_type_from_ust_ctl_fields(
28ab034a 810 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0 811 case lttng_ust_ctl_atype_array_nestable:
28ab034a
JG
812 return create_array_nestable_type_from_ust_ctl_fields(
813 current,
814 end,
815 session_attributes,
816 next_ust_ctl_field,
817 publish_field,
818 lookup_field,
819 lookup_root,
820 current_field_location_elements,
821 quirks);
d7bfb9b0 822 case lttng_ust_ctl_atype_sequence:
28ab034a
JG
823 return create_sequence_type_from_ust_ctl_fields(current,
824 end,
825 session_attributes,
826 next_ust_ctl_field,
827 publish_field,
828 lookup_root,
829 current_field_location_elements,
830 quirks);
d7bfb9b0 831 case lttng_ust_ctl_atype_sequence_nestable:
28ab034a
JG
832 return create_sequence_nestable_type_from_ust_ctl_fields(
833 current,
834 end,
835 session_attributes,
836 next_ust_ctl_field,
837 publish_field,
838 lookup_field,
839 lookup_root,
840 current_field_location_elements,
841 quirks);
d7bfb9b0
JG
842 case lttng_ust_ctl_atype_struct:
843 case lttng_ust_ctl_atype_struct_nestable:
844 return create_structure_field_from_ust_ctl_fields(
28ab034a 845 current, end, session_attributes, next_ust_ctl_field, quirks);
d7bfb9b0
JG
846 case lttng_ust_ctl_atype_variant:
847 case lttng_ust_ctl_atype_variant_nestable:
28ab034a
JG
848 return create_variant_field_from_ust_ctl_fields(current,
849 end,
850 session_attributes,
851 next_ust_ctl_field,
852 lookup_field,
853 lookup_root,
854 current_field_location_elements,
855 quirks);
d7bfb9b0 856 default:
28ab034a 857 LTTNG_THROW_PROTOCOL_ERROR(
f9a41357
JG
858 lttng::format("Unknown {} value `{}` encountered while converting {} to {}",
859 typeid(current->type.atype),
860 current->type.atype,
861 typeid(*current),
862 typeid(lst::type::cuptr::element_type)));
d7bfb9b0
JG
863 }
864}
865
866void create_field_from_ust_ctl_fields(const lttng_ust_ctl_field *current,
28ab034a
JG
867 const lttng_ust_ctl_field *end,
868 const session_attributes& session_attributes,
869 const lttng_ust_ctl_field **next_ust_ctl_field,
cd9adb8b
JG
870 const publish_field_fn& publish_field,
871 const lookup_field_fn& lookup_field,
28ab034a
JG
872 lst::field_location::root lookup_root,
873 lst::field_location::elements& current_field_location_elements,
874 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
875{
876 LTTNG_ASSERT(current < end);
877
878 if (lttng_strnlen(current->name, sizeof(current->name)) == sizeof(current->name)) {
879 LTTNG_THROW_PROTOCOL_ERROR(
f9a41357 880 lttng::format("Name of {} is not null-terminated", typeid(*current)));
28ab034a
JG
881 }
882
883 publish_field(lttng::make_unique<lst::field>(
884 current->name,
885 create_type_from_ust_ctl_fields(current,
886 end,
887 session_attributes,
888 next_ust_ctl_field,
889 publish_field,
890 lookup_field,
891 lookup_root,
892 current_field_location_elements,
893 quirks)));
d7bfb9b0
JG
894}
895
28ab034a
JG
896std::vector<lst::field::cuptr>::iterator
897lookup_field_in_vector(std::vector<lst::field::cuptr>& fields, const lst::field_location& location)
da9dd521
JG
898{
899 if (location.elements_.size() != 1) {
f9a41357 900 LTTNG_THROW_ERROR(lttng::format(
28ab034a
JG
901 "Unexpected field location received during field look-up: location = {}",
902 location));
da9dd521
JG
903 }
904
905 /*
906 * In the context of fields received from LTTng-UST, field
907 * look-up is extremely naive as the protocol can only
908 * express empty structures. It is safe to assume that
909 * location has a depth of 1 and directly refers to a field
910 * in the 'fields' vector.
911 */
28ab034a
JG
912 const auto field_it =
913 std::find_if(fields.begin(), fields.end(), [location](lst::field::cuptr& field) {
914 return field->name == location.elements_[0];
915 });
da9dd521
JG
916
917 if (field_it == fields.end()) {
918 LTTNG_THROW_PROTOCOL_ERROR(
f9a41357 919 lttng::format("Failed to look-up field: location = {}", location));
da9dd521
JG
920 }
921
922 return field_it;
923}
924
d7bfb9b0
JG
925/*
926 * `lttng_ust_ctl_field`s can be nested, in which case creating a field will consume
927 * more than one lttng_ust_ctl_field. create_field_from_ust_ctl_fields returns the
928 * position of the next lttng_ust_ctl_field to consume or `end` when the last field
929 * is consumed.
930 *
931 * Always returns a new field, throws on error.
932 */
28ab034a
JG
933std::vector<lst::field::cuptr>
934create_fields_from_ust_ctl_fields(const lsu::registry_session& session,
935 const lttng_ust_ctl_field *current,
936 const lttng_ust_ctl_field *end,
937 lst::field_location::root lookup_root,
938 lsu::ctl_field_quirks quirks)
d7bfb9b0
JG
939{
940 std::vector<lst::field::cuptr> fields;
941 const auto trace_native_byte_order = session.abi.byte_order;
942 const session_attributes session_attributes{
28ab034a
JG
943 [&session](const char *enum_name, uint64_t enum_id) {
944 return session.enumeration(enum_name, enum_id);
945 },
946 trace_native_byte_order
947 };
eda1aa02
JG
948 /* Location of field being created. */
949 lst::field_location::elements current_field_location_elements;
d7bfb9b0
JG
950
951 while (current < end) {
952 auto *next_field = current;
953
954 /*
955 * create_field_from_ust_ctl_fields will consume one field at a time.
956 * However, some fields expressed by LTTng-UST's protocol are expended
957 * to multiple event fields (legacy sequence fields implicitly define
958 * their length field).
959 *
960 * The lambda allows the factory functions to push as many fields as
961 * needed depending on the decoded field's type.
962 */
6e01cdc6 963 create_field_from_ust_ctl_fields(
28ab034a
JG
964 current,
965 end,
966 session_attributes,
967 &next_field,
968 [&fields](lst::field::cuptr field) {
969 /* Publishing a field simply adds it to the converted
970 * fields. */
971 fields.emplace_back(std::move(field));
972 },
973 [&fields](const lst::field_location& location)
974 -> lookup_field_fn::result_type {
975 /* Resolve location to a previously-constructed field. */
976 return **lookup_field_in_vector(fields, location);
977 },
978 lookup_root,
979 current_field_location_elements,
980 quirks);
d7bfb9b0
JG
981
982 current = next_field;
983 }
984
985 return fields;
986}
987} /* namespace */
988
28ab034a
JG
989std::vector<lst::field::cuptr>
990lsu::create_trace_fields_from_ust_ctl_fields(const lsu::registry_session& session,
991 const lttng_ust_ctl_field *fields,
992 std::size_t field_count,
993 lst::field_location::root lookup_root,
994 lsu::ctl_field_quirks quirks)
d7bfb9b0 995{
63c3462c 996 return create_fields_from_ust_ctl_fields(
28ab034a 997 session, fields, fields + field_count, lookup_root, quirks);
d7bfb9b0 998}
This page took 0.085178 seconds and 4 git commands to generate.