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