2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/format.hpp>
13 #include <vendor/optional.hpp>
17 #include <type_traits>
27 enum class byte_order {
32 class field_location {
38 EVENT_RECORD_COMMON_CONTEXT,
39 EVENT_RECORD_SPECIFIC_CONTEXT,
43 using elements = std::vector<std::string>;
45 field_location(root lookup_root, elements elements);
46 bool operator==(const field_location& other) const noexcept;
49 const elements elements_;
53 * Field, and the various field types, represents fields as exposed by the
54 * LTTng tracers. These classes do not attempt to describe the complete spectrum of the CTF
60 using cuptr = std::unique_ptr<const type>;
62 static byte_order reverse_byte_order(byte_order byte_order) noexcept;
64 bool operator==(const type& other) const noexcept;
65 bool operator!=(const type& other) const noexcept;
67 virtual void accept(type_visitor& visitor) const = 0;
69 const unsigned int alignment;
72 type(unsigned int alignment);
75 virtual bool _is_equal(const type& rhs) const noexcept = 0;
80 using cuptr = std::unique_ptr<const field>;
82 field(std::string name, type::cuptr type);
83 void accept(field_visitor& visitor) const;
84 bool operator==(const field& other) const noexcept;
86 const std::string name;
87 const type::cuptr _type;
90 class integer_type : public type {
92 enum class signedness {
105 DEFAULT_CLOCK_TIMESTAMP,
106 /* Packet header field class specific roles. */
107 DATA_STREAM_CLASS_ID,
110 /* Packet context field class specific roles. */
111 DISCARDED_EVENT_RECORD_COUNTER_SNAPSHOT,
112 PACKET_CONTENT_LENGTH,
113 PACKET_END_DEFAULT_CLOCK_TIMESTAMP,
114 PACKET_SEQUENCE_NUMBER,
116 /* Event record field class roles. */
117 EVENT_RECORD_CLASS_ID,
120 using roles = std::vector<role>;
122 integer_type(unsigned int alignment,
123 byte_order byte_order,
125 signedness signedness,
129 virtual void accept(type_visitor& visitor) const override;
131 const enum byte_order byte_order;
132 const unsigned int size;
134 * signedness and base are suffixed with '_' to work-around a bug in older
135 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
136 * nested-name-specifiers.
138 const signedness signedness_;
143 virtual bool _is_equal(const type& other) const noexcept override;
146 class floating_point_type : public type {
148 floating_point_type(unsigned int alignment,
149 byte_order byte_order,
150 unsigned int exponent_digits,
151 unsigned int mantissa_digits);
153 virtual void accept(type_visitor& visitor) const override final;
155 const enum byte_order byte_order;
156 const unsigned int exponent_digits;
157 const unsigned int mantissa_digits;
160 virtual bool _is_equal(const type& other) const noexcept override final;
163 class enumeration_type : public integer_type {
165 enumeration_type(unsigned int alignment,
166 enum byte_order byte_order,
168 enum signedness signedness,
170 integer_type::roles roles = {});
172 virtual void accept(type_visitor& visitor) const = 0;
176 template <class MappingIntegerType>
177 class enumeration_mapping_range {
179 using range_integer_t = MappingIntegerType;
181 enumeration_mapping_range(MappingIntegerType in_begin, MappingIntegerType in_end) :
182 begin{in_begin}, end{in_end}
186 const range_integer_t begin, end;
189 template <class MappingIntegerType>
190 bool operator==(const enumeration_mapping_range<MappingIntegerType>& lhs,
191 const enumeration_mapping_range<MappingIntegerType>& rhs) noexcept
193 return lhs.begin == rhs.begin && lhs.end == rhs.end;
196 template <class MappingIntegerType>
197 class enumeration_mapping {
199 using range_t = enumeration_mapping_range<MappingIntegerType>;
201 enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = delete;
202 enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
203 name{std::move(other.name)}, range{other.range}
207 /* Mapping with an implicit value. */
208 enumeration_mapping(std::string in_name) : name{std::move(in_name)}
212 enumeration_mapping(std::string in_name, range_t in_range) : name{std::move(in_name)}, range{in_range}
216 const std::string name;
217 const nonstd::optional<range_t> range;
220 template <class MappingIntegerType>
221 bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
222 const enumeration_mapping<MappingIntegerType>& rhs) noexcept
224 return lhs.name == rhs.name && lhs.range == rhs.range;
226 } /* namespace details */
228 template <class MappingIntegerType>
229 class typed_enumeration_type : public enumeration_type {
231 using mapping = details::enumeration_mapping<MappingIntegerType>;
232 using mappings = std::vector<mapping>;
234 static_assert(std::is_integral<MappingIntegerType>::value &&
235 sizeof(MappingIntegerType) == 8,
236 "MappingIntegerType must be either int64_t or uint64_t");
238 typed_enumeration_type(unsigned int in_alignment,
239 enum byte_order in_byte_order,
240 unsigned int in_size,
242 const std::shared_ptr<const mappings>& in_mappings,
243 integer_type::roles in_roles = {}) :
244 enumeration_type(in_alignment,
247 std::is_signed<MappingIntegerType>::value ?
248 integer_type::signedness::SIGNED :
249 integer_type::signedness::UNSIGNED,
251 std::move(in_roles)),
252 _mappings{std::move(in_mappings)}
256 virtual void accept(type_visitor& visitor) const override final;
258 const std::shared_ptr<const mappings> _mappings;
261 virtual bool _is_equal(const type& base_other) const noexcept override final
263 const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
266 return integer_type::_is_equal(base_other) && *this->_mappings == *other._mappings;
270 /* Aliases for all allowed enumeration mapping types. */
271 using signed_enumeration_type = typed_enumeration_type<int64_t>;
272 using unsigned_enumeration_type = typed_enumeration_type<uint64_t>;
274 class array_type : public type {
276 array_type(unsigned int alignment, type::cuptr element_type);
278 const type::cuptr element_type;
281 virtual bool _is_equal(const type& base_other) const noexcept override;
284 class static_length_array_type : public array_type {
286 static_length_array_type(unsigned int alignment,
287 type::cuptr element_type,
290 virtual void accept(type_visitor& visitor) const override final;
292 const uint64_t length;
295 virtual bool _is_equal(const type& base_other) const noexcept override final;
298 class dynamic_length_array_type : public array_type {
300 dynamic_length_array_type(unsigned int alignment,
301 type::cuptr element_type,
302 field_location length_field_location);
304 virtual void accept(type_visitor& visitor) const override final;
306 const field_location length_field_location;
309 virtual bool _is_equal(const type& base_other) const noexcept override final;
312 class static_length_blob_type : public type {
315 /* Packet header field class specific role. */
319 using roles = std::vector<role>;
321 static_length_blob_type(unsigned int alignment, uint64_t in_length_bytes, roles roles = {});
323 virtual void accept(type_visitor& visitor) const override final;
325 const uint64_t length_bytes;
329 virtual bool _is_equal(const type& base_other) const noexcept override final;
332 class dynamic_length_blob_type : public type {
334 dynamic_length_blob_type(unsigned int alignment, field_location length_field_location);
336 virtual void accept(type_visitor& visitor) const override final;
338 const field_location length_field_location;
341 virtual bool _is_equal(const type& base_other) const noexcept override final;
344 class string_type : public type {
346 enum class encoding {
351 string_type(unsigned int alignment, enum encoding encoding);
354 * encoding is suffixed with '_' to work-around a bug in older
355 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
356 * nested-name-specifiers.
358 const encoding encoding_;
361 virtual bool _is_equal(const type& base_other) const noexcept override;
364 class static_length_string_type : public string_type {
366 static_length_string_type(
367 unsigned int alignment, enum encoding in_encoding, uint64_t length);
368 virtual void accept(type_visitor& visitor) const override final;
370 const uint64_t length;
373 virtual bool _is_equal(const type& base_other) const noexcept override final;
376 class dynamic_length_string_type : public string_type {
378 dynamic_length_string_type(unsigned int alignment,
379 enum encoding in_encoding,
380 field_location length_field_location);
381 virtual void accept(type_visitor& visitor) const override final;
383 const field_location length_field_location;
386 virtual bool _is_equal(const type& base_other) const noexcept override final;
389 class null_terminated_string_type : public string_type {
391 null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
392 virtual void accept(type_visitor& visitor) const override final;
395 class structure_type : public type {
397 using fields = std::vector<field::cuptr>;
399 structure_type(unsigned int alignment, fields in_fields);
401 virtual void accept(type_visitor& visitor) const override final;
403 const fields _fields;
406 virtual bool _is_equal(const type& base_other) const noexcept override final;
409 class variant_type : public type {
411 using choices = std::vector<field::cuptr>;
413 variant_type(unsigned int alignment,
414 field_location selector_field_location,
417 virtual void accept(type_visitor& visitor) const override final;
419 const field_location selector_field_location;
420 const choices _choices;
424 virtual bool _is_equal(const type& base_other) const noexcept override final;
427 class field_visitor {
429 virtual ~field_visitor() = default;
430 virtual void visit(const field& field) = 0;
433 field_visitor() = default;
438 virtual ~type_visitor() = default;
439 virtual void visit(const integer_type& type) = 0;
440 virtual void visit(const floating_point_type& type) = 0;
441 virtual void visit(const signed_enumeration_type& type) = 0;
442 virtual void visit(const unsigned_enumeration_type& type) = 0;
443 virtual void visit(const static_length_array_type& type) = 0;
444 virtual void visit(const dynamic_length_array_type& type) = 0;
445 virtual void visit(const static_length_blob_type& type) = 0;
446 virtual void visit(const dynamic_length_blob_type& type) = 0;
447 virtual void visit(const null_terminated_string_type& type) = 0;
448 virtual void visit(const static_length_string_type& type) = 0;
449 virtual void visit(const dynamic_length_string_type& type) = 0;
450 virtual void visit(const structure_type& type) = 0;
451 virtual void visit(const variant_type& type) = 0;
454 type_visitor() = default;
457 } /* namespace trace */
458 } /* namespace sessiond */
459 } /* namespace lttng */
462 * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
463 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
467 struct formatter<lttng::sessiond::trace::field_location> : formatter<std::string> {
468 template <typename FormatCtx>
469 typename FormatCtx::iterator format(
470 const lttng::sessiond::trace::field_location& location, FormatCtx& ctx)
472 std::string location_str{"["};
474 switch (location.root_) {
475 case lttng::sessiond::trace::field_location::root::PACKET_HEADER:
476 location_str += "\"packet-header\"";
478 case lttng::sessiond::trace::field_location::root::PACKET_CONTEXT:
479 location_str += "\"packet-context\"";
481 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_HEADER:
482 location_str += "\"event-record-header\"";
484 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_COMMON_CONTEXT:
485 location_str += "\"event-record-common-context\"";
487 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_SPECIFIC_CONTEXT:
488 location_str += "\"event-record-specific-context\"";
490 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_PAYLOAD:
491 location_str += "\"event-record-payload\"";
495 for (const auto &name : location.elements_) {
496 location_str += ", \"" + name + "\"";
500 return format_to(ctx.out(), location_str);
503 } /* namespace fmt */
505 #endif /* LTTNG_FIELD_H */