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>
12 #include <common/make-unique.hpp>
14 #include <vendor/optional.hpp>
19 #include <type_traits>
29 enum class byte_order {
34 class field_location {
40 EVENT_RECORD_COMMON_CONTEXT,
41 EVENT_RECORD_SPECIFIC_CONTEXT,
45 using elements = std::vector<std::string>;
47 field_location(root lookup_root, elements elements);
48 bool operator==(const field_location& other) const noexcept;
51 const elements elements_;
55 * Field, and the various field types, represents fields as exposed by the
56 * LTTng tracers. These classes do not attempt to describe the complete spectrum of the CTF
62 using cuptr = std::unique_ptr<const type>;
64 static byte_order reverse_byte_order(byte_order byte_order) noexcept;
66 bool operator==(const type& other) const noexcept;
67 bool operator!=(const type& other) const noexcept;
70 type(const type&) = delete;
71 type(type&&) = delete;
72 type& operator=(type&&) = delete;
73 type& operator=(const type&) = delete;
75 /* Obtain an independent copy of `type`. */
76 virtual type::cuptr copy() const = 0;
78 virtual void accept(type_visitor& visitor) const = 0;
80 const unsigned int alignment;
83 explicit type(unsigned int alignment);
86 virtual bool _is_equal(const type& rhs) const noexcept = 0;
91 using uptr = std::unique_ptr<field>;
92 using cuptr = std::unique_ptr<const field>;
94 field(std::string name, type::cuptr type);
95 void accept(field_visitor& visitor) const;
96 bool operator==(const field& other) const noexcept;
98 const type& get_type() const;
99 type::cuptr move_type() noexcept;
101 const std::string name;
107 class integer_type : public type {
109 enum class signedness {
122 DEFAULT_CLOCK_TIMESTAMP,
123 /* Packet header field class specific roles. */
124 DATA_STREAM_CLASS_ID,
127 /* Packet context field class specific roles. */
128 DISCARDED_EVENT_RECORD_COUNTER_SNAPSHOT,
129 PACKET_CONTENT_LENGTH,
130 PACKET_END_DEFAULT_CLOCK_TIMESTAMP,
131 PACKET_SEQUENCE_NUMBER,
133 /* Event record field class roles. */
134 EVENT_RECORD_CLASS_ID,
137 using roles = std::vector<role>;
139 integer_type(unsigned int alignment,
140 byte_order byte_order,
142 signedness signedness,
146 type::cuptr copy() const override;
148 void accept(type_visitor& visitor) const override;
150 const enum byte_order byte_order;
151 const unsigned int size;
153 * signedness and base are suffixed with '_' to work-around a bug in older
154 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
155 * nested-name-specifiers.
157 const signedness signedness_;
162 bool _is_equal(const type& other) const noexcept override;
165 class floating_point_type : public type {
167 floating_point_type(unsigned int alignment,
168 byte_order byte_order,
169 unsigned int exponent_digits,
170 unsigned int mantissa_digits);
172 type::cuptr copy() const final;
174 void accept(type_visitor& visitor) const final;
176 const enum byte_order byte_order;
177 const unsigned int exponent_digits;
178 const unsigned int mantissa_digits;
181 bool _is_equal(const type& other) const noexcept final;
184 class enumeration_type : public integer_type {
186 ~enumeration_type() override = default;
187 enumeration_type(const enumeration_type&) = delete;
188 enumeration_type(enumeration_type&&) = delete;
189 enumeration_type& operator=(enumeration_type&&) = delete;
190 enumeration_type& operator=(const enumeration_type&) = delete;
193 enumeration_type(unsigned int alignment,
194 enum byte_order byte_order,
196 enum signedness signedness,
198 integer_type::roles roles = {});
200 void accept(type_visitor& visitor) const override = 0;
204 template <class MappingIntegerType>
205 class enumeration_mapping_range {
207 using range_integer_t = MappingIntegerType;
209 enumeration_mapping_range(MappingIntegerType in_begin, MappingIntegerType in_end) :
210 begin{in_begin}, end{in_end}
214 const range_integer_t begin, end;
217 template <class MappingIntegerType>
218 bool operator==(const enumeration_mapping_range<MappingIntegerType>& lhs,
219 const enumeration_mapping_range<MappingIntegerType>& rhs) noexcept
221 return lhs.begin == rhs.begin && lhs.end == rhs.end;
224 template <class MappingIntegerType>
225 class enumeration_mapping {
227 using range_t = enumeration_mapping_range<MappingIntegerType>;
229 enumeration_mapping(std::string in_name, MappingIntegerType value) :
230 name{ std::move(in_name) }, range{ value, value }
234 enumeration_mapping(std::string in_name, range_t in_range) :
235 name{ std::move(in_name) }, range{ in_range }
239 enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = default;
240 enumeration_mapping(enumeration_mapping<MappingIntegerType>&& other) noexcept :
241 name{ std::move(other.name) }, range{ other.range }
245 enumeration_mapping& operator=(enumeration_mapping&&) = delete;
246 enumeration_mapping& operator=(const enumeration_mapping&) = delete;
247 ~enumeration_mapping() = default;
249 const std::string name;
251 * Only one range per mapping is supported for the moment as
252 * the tracers (and CTF 1.8) can't express multiple ranges per
253 * mapping, which is allowed by CTF 2.
258 template <class MappingIntegerType>
259 bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
260 const enumeration_mapping<MappingIntegerType>& rhs) noexcept
262 return lhs.name == rhs.name && lhs.range == rhs.range;
264 } /* namespace details */
266 template <typename MappingIntegerType>
267 class typed_enumeration_type : public enumeration_type {
269 using mapping = details::enumeration_mapping<MappingIntegerType>;
270 using mappings = std::vector<mapping>;
272 static_assert(std::is_integral<MappingIntegerType>::value &&
273 sizeof(MappingIntegerType) == 8,
274 "MappingIntegerType must be either int64_t or uint64_t");
276 typed_enumeration_type(unsigned int in_alignment,
277 enum byte_order in_byte_order,
278 unsigned int in_size,
280 const std::shared_ptr<const mappings>& in_mappings,
281 integer_type::roles in_roles = {}) :
282 enumeration_type(in_alignment,
285 std::is_signed<MappingIntegerType>::value ?
286 integer_type::signedness::SIGNED :
287 integer_type::signedness::UNSIGNED,
289 std::move(in_roles)),
290 mappings_{std::move(in_mappings)}
294 type::cuptr copy() const override
296 return lttng::make_unique<typed_enumeration_type<MappingIntegerType>>(
297 alignment, byte_order, size, base_, mappings_, roles_);
300 void accept(type_visitor& visitor) const final;
302 const std::shared_ptr<const mappings> mappings_;
305 bool _is_equal(const type& base_other) const noexcept final
307 const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
310 return integer_type::_is_equal(base_other) && *this->mappings_ == *other.mappings_;
314 /* Aliases for all allowed enumeration mapping types. */
315 using signed_enumeration_type = typed_enumeration_type<int64_t>;
316 using unsigned_enumeration_type = typed_enumeration_type<uint64_t>;
318 class array_type : public type {
320 array_type(unsigned int alignment, type::cuptr element_type);
322 const type::cuptr element_type;
325 bool _is_equal(const type& base_other) const noexcept override;
328 class static_length_array_type : public array_type {
330 static_length_array_type(unsigned int alignment,
331 type::cuptr element_type,
334 type::cuptr copy() const final;
336 void accept(type_visitor& visitor) const final;
338 const uint64_t length;
341 bool _is_equal(const type& base_other) const noexcept final;
344 class dynamic_length_array_type : public array_type {
346 dynamic_length_array_type(unsigned int alignment,
347 type::cuptr element_type,
348 field_location length_field_location);
350 type::cuptr copy() const final;
352 void accept(type_visitor& visitor) const final;
354 const field_location length_field_location;
357 bool _is_equal(const type& base_other) const noexcept final;
360 class static_length_blob_type : public type {
363 /* Packet header field class specific role. */
364 METADATA_STREAM_UUID,
367 using roles = std::vector<role>;
369 static_length_blob_type(unsigned int alignment, uint64_t in_length_bytes, roles roles = {});
371 type::cuptr copy() const final;
373 void accept(type_visitor& visitor) const final;
375 const uint64_t length_bytes;
379 bool _is_equal(const type& base_other) const noexcept final;
382 class dynamic_length_blob_type : public type {
384 dynamic_length_blob_type(unsigned int alignment, field_location length_field_location);
386 type::cuptr copy() const final;
388 void accept(type_visitor& visitor) const final;
390 const field_location length_field_location;
393 bool _is_equal(const type& base_other) const noexcept final;
396 class string_type : public type {
398 enum class encoding {
403 string_type(unsigned int alignment, enum encoding encoding);
406 * encoding is suffixed with '_' to work-around a bug in older
407 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
408 * nested-name-specifiers.
410 const encoding encoding_;
413 bool _is_equal(const type& base_other) const noexcept override;
416 class static_length_string_type : public string_type {
418 static_length_string_type(
419 unsigned int alignment, enum encoding in_encoding, uint64_t length);
421 type::cuptr copy() const final;
423 void accept(type_visitor& visitor) const final;
425 const uint64_t length;
428 bool _is_equal(const type& base_other) const noexcept final;
431 class dynamic_length_string_type : public string_type {
433 dynamic_length_string_type(unsigned int alignment,
434 enum encoding in_encoding,
435 field_location length_field_location);
437 type::cuptr copy() const final;
439 void accept(type_visitor& visitor) const final;
441 const field_location length_field_location;
444 bool _is_equal(const type& base_other) const noexcept final;
447 class null_terminated_string_type : public string_type {
449 null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
451 type::cuptr copy() const final;
453 void accept(type_visitor& visitor) const final;
456 class structure_type : public type {
458 using fields = std::vector<field::cuptr>;
460 structure_type(unsigned int alignment, fields in_fields);
462 type::cuptr copy() const final;
464 void accept(type_visitor& visitor) const final;
466 const fields fields_;
469 bool _is_equal(const type& base_other) const noexcept final;
472 template <typename MappingIntegerType>
473 class variant_type : public type {
474 static_assert(std::is_same<MappingIntegerType,
475 unsigned_enumeration_type::mapping::range_t::
476 range_integer_t>::value ||
477 std::is_same<MappingIntegerType,
478 signed_enumeration_type::mapping::range_t::
479 range_integer_t>::value,
480 "Variant mapping integer type must be one of those allowed by typed_enumeration_type");
483 using choice = std::pair<const details::enumeration_mapping<MappingIntegerType>, type::cuptr>;
484 using choices = std::vector<choice>;
486 variant_type(unsigned int in_alignment,
487 field_location in_selector_field_location,
488 choices in_choices) :
490 selector_field_location{std::move(in_selector_field_location)},
491 choices_{std::move(in_choices)}
495 type::cuptr copy() const final
497 choices copy_of_choices;
499 copy_of_choices.reserve(choices_.size());
500 for (const auto& current_choice : choices_) {
501 copy_of_choices.emplace_back(
502 current_choice.first, current_choice.second->copy());
505 return lttng::make_unique<variant_type<MappingIntegerType>>(
506 alignment, selector_field_location, std::move(copy_of_choices));
509 void accept(type_visitor& visitor) const final;
511 const field_location selector_field_location;
512 const choices choices_;
515 static bool _choices_are_equal(const choices& a, const choices& b)
517 if (a.size() != b.size()) {
521 return std::equal(a.cbegin(), a.cend(), b.cbegin(),
522 [](const choice& choice_a, const choice& choice_b) {
523 return choice_a.first == choice_b.first &&
524 *choice_a.second == *choice_b.second;
528 bool _is_equal(const type& base_other) const noexcept final
530 const auto& other = static_cast<decltype(*this)&>(base_other);
532 return selector_field_location == other.selector_field_location &&
533 _choices_are_equal(choices_, other.choices_);
537 class field_visitor {
539 virtual ~field_visitor() = default;
540 field_visitor(field_visitor&&) = delete;
541 field_visitor(const field_visitor&) = delete;
542 field_visitor& operator=(const field_visitor&) = delete;
543 field_visitor& operator=(field_visitor&&) = delete;
545 virtual void visit(const field& field) = 0;
548 field_visitor() = default;
553 virtual ~type_visitor() = default;
554 type_visitor(type_visitor&&) = delete;
555 type_visitor(const type_visitor&) = delete;
556 type_visitor& operator=(const type_visitor&) = delete;
557 type_visitor& operator=(type_visitor&&) = delete;
559 virtual void visit(const integer_type& type) = 0;
560 virtual void visit(const floating_point_type& type) = 0;
561 virtual void visit(const signed_enumeration_type& type) = 0;
562 virtual void visit(const unsigned_enumeration_type& type) = 0;
563 virtual void visit(const static_length_array_type& type) = 0;
564 virtual void visit(const dynamic_length_array_type& type) = 0;
565 virtual void visit(const static_length_blob_type& type) = 0;
566 virtual void visit(const dynamic_length_blob_type& type) = 0;
567 virtual void visit(const null_terminated_string_type& type) = 0;
568 virtual void visit(const static_length_string_type& type) = 0;
569 virtual void visit(const dynamic_length_string_type& type) = 0;
570 virtual void visit(const structure_type& type) = 0;
571 virtual void visit(const variant_type<signed_enumeration_type::mapping::range_t::range_integer_t>& type) = 0;
572 virtual void visit(const variant_type<unsigned_enumeration_type::mapping::range_t::range_integer_t>& type) = 0;
575 type_visitor() = default;
578 } /* namespace trace */
579 } /* namespace sessiond */
580 } /* namespace lttng */
583 * Field formatters for libfmt.
585 * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
586 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
590 struct formatter<lttng::sessiond::trace::field_location> : formatter<std::string> {
591 template <typename FormatContextType>
592 typename FormatContextType::iterator format(
593 const lttng::sessiond::trace::field_location& location, FormatContextType& ctx)
595 std::string location_str{"["};
597 switch (location.root_) {
598 case lttng::sessiond::trace::field_location::root::PACKET_HEADER:
599 location_str += "\"packet-header\"";
601 case lttng::sessiond::trace::field_location::root::PACKET_CONTEXT:
602 location_str += "\"packet-context\"";
604 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_HEADER:
605 location_str += "\"event-record-header\"";
607 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_COMMON_CONTEXT:
608 location_str += "\"event-record-common-context\"";
610 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_SPECIFIC_CONTEXT:
611 location_str += "\"event-record-specific-context\"";
613 case lttng::sessiond::trace::field_location::root::EVENT_RECORD_PAYLOAD:
614 location_str += "\"event-record-payload\"";
618 for (const auto &name : location.elements_) {
619 location_str += ", \"" + name + "\"";
623 return format_to(ctx.out(), location_str);
628 template <typename MappingIntegerType>
629 ::std::string format_mapping_range(typename lttng::sessiond::trace::typed_enumeration_type<
630 MappingIntegerType>::mapping::range_t range)
632 if (range.begin == range.end) {
633 return ::fmt::format("[{}]", range.begin);
635 return ::fmt::format("[{}, {}]", range.begin, range.end);
638 } /* namespace details */
641 struct formatter<typename lttng::sessiond::trace::signed_enumeration_type::mapping::range_t>
642 : formatter<std::string> {
643 template <typename FormatContextType>
644 typename FormatContextType::iterator
645 format(typename lttng::sessiond::trace::signed_enumeration_type::mapping::range_t range,
646 FormatContextType& ctx)
648 return format_to(ctx.out(),
649 details::format_mapping_range<
650 lttng::sessiond::trace::signed_enumeration_type::
651 mapping::range_t::range_integer_t>(
657 struct formatter<typename lttng::sessiond::trace::unsigned_enumeration_type::mapping::range_t>
658 : formatter<std::string> {
659 template <typename FormatContextType>
660 typename FormatContextType::iterator
661 format(typename lttng::sessiond::trace::unsigned_enumeration_type::mapping::range_t range,
662 FormatContextType& ctx)
664 return format_to(ctx.out(),
665 details::format_mapping_range<
666 lttng::sessiond::trace::unsigned_enumeration_type::
667 mapping::range_t::range_integer_t>(
672 } /* namespace fmt */
674 #endif /* LTTNG_FIELD_H */