2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include "trace-class.hpp"
15 #include <type_traits>
18 #include <vendor/optional.hpp>
28 * Field, and the various field types, represents fields as exposed by the
29 * LTTng tracers. These classes do not attempt to describe the complete spectrum of the CTF
35 using cuptr = std::unique_ptr<const type>;
37 static byte_order reverse_byte_order(byte_order byte_order) noexcept;
39 bool operator==(const type& other) const noexcept;
40 bool operator!=(const type& other) const noexcept;
42 virtual void accept(type_visitor& visitor) const = 0;
44 const unsigned int alignment;
47 type(unsigned int alignment);
50 virtual bool _is_equal(const type& rhs) const noexcept = 0;
55 using cuptr = std::unique_ptr<const field>;
57 field(std::string name, type::cuptr type);
58 void accept(field_visitor& visitor) const;
59 bool operator==(const field& other) const noexcept;
61 const std::string name;
62 const type::cuptr _type;
65 class integer_type : public type {
67 enum class signedness {
79 integer_type(unsigned int alignment,
80 byte_order byte_order,
82 signedness signedness,
85 virtual void accept(type_visitor& visitor) const override;
87 const enum byte_order byte_order;
88 const unsigned int size;
90 * signedness and base are suffixed with '_' to work-around a bug in older
91 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
92 * nested-name-specifiers.
94 const signedness signedness_;
98 virtual bool _is_equal(const type& other) const noexcept override;
101 class floating_point_type : public type {
103 floating_point_type(unsigned int alignment,
104 byte_order byte_order,
105 unsigned int exponent_digits,
106 unsigned int mantissa_digits);
108 virtual void accept(type_visitor& visitor) const override final;
110 const enum byte_order byte_order;
111 const unsigned int exponent_digits;
112 const unsigned int mantissa_digits;
115 virtual bool _is_equal(const type& other) const noexcept override final;
118 class enumeration_type : public integer_type {
120 enumeration_type(unsigned int alignment,
121 enum byte_order byte_order,
123 enum signedness signedness,
126 virtual void accept(type_visitor& visitor) const = 0;
130 template <class MappingIntegerType>
131 class enumeration_mapping_range {
133 using range_integer_t = MappingIntegerType;
135 enumeration_mapping_range(MappingIntegerType in_begin, MappingIntegerType in_end) :
136 begin{in_begin}, end{in_end}
140 const range_integer_t begin, end;
143 template <class MappingIntegerType>
144 bool operator==(const enumeration_mapping_range<MappingIntegerType>& lhs,
145 const enumeration_mapping_range<MappingIntegerType>& rhs) noexcept
147 return lhs.begin == rhs.begin && lhs.end == rhs.end;
150 template <class MappingIntegerType>
151 class enumeration_mapping {
153 using range_t = enumeration_mapping_range<MappingIntegerType>;
155 enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = delete;
156 enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
157 name{std::move(other.name)}, range{other.range}
161 /* Mapping with an implicit value. */
162 enumeration_mapping(std::string in_name) : name{std::move(in_name)}
166 enumeration_mapping(std::string in_name, range_t in_range) : name{std::move(in_name)}, range{in_range}
170 const std::string name;
171 const nonstd::optional<range_t> range;
174 template <class MappingIntegerType>
175 bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
176 const enumeration_mapping<MappingIntegerType>& rhs) noexcept
178 return lhs.name == rhs.name && lhs.range == rhs.range;
180 } /* namespace details */
182 template <class MappingIntegerType>
183 class typed_enumeration_type : public enumeration_type {
185 using mapping = details::enumeration_mapping<MappingIntegerType>;
186 using mappings = std::vector<mapping>;
188 static_assert(std::is_integral<MappingIntegerType>::value &&
189 sizeof(MappingIntegerType) == 8,
190 "MappingIntegerType must be either int64_t or uint64_t");
192 typed_enumeration_type(unsigned int in_alignment,
193 enum byte_order in_byte_order,
194 unsigned int in_size,
195 enum signedness in_signedness,
197 const std::shared_ptr<const mappings>& in_mappings) :
198 enumeration_type(in_alignment,
203 _mappings{std::move(in_mappings)}
207 virtual void accept(type_visitor& visitor) const override final;
209 const std::shared_ptr<const mappings> _mappings;
212 virtual bool _is_equal(const type& base_other) const noexcept override final
214 const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
217 return integer_type::_is_equal(base_other) && *this->_mappings == *other._mappings;
221 /* Aliases for all allowed enumeration mapping types. */
222 using signed_enumeration_type = typed_enumeration_type<int64_t>;
223 using unsigned_enumeration_type = typed_enumeration_type<uint64_t>;
225 class array_type : public type {
227 array_type(unsigned int alignment, type::cuptr element_type);
229 const type::cuptr element_type;
232 virtual bool _is_equal(const type& base_other) const noexcept override;
235 class static_length_array_type : public array_type {
237 static_length_array_type(unsigned int alignment,
238 type::cuptr element_type,
241 virtual void accept(type_visitor& visitor) const override final;
243 const uint64_t length;
246 virtual bool _is_equal(const type& base_other) const noexcept override final;
249 class dynamic_length_array_type : public array_type {
251 dynamic_length_array_type(unsigned int alignment,
252 type::cuptr element_type,
253 std::string length_field_name);
255 virtual void accept(type_visitor& visitor) const override final;
257 const std::string length_field_name;
260 virtual bool _is_equal(const type& base_other) const noexcept override final;
263 class string_type : public type {
265 enum class encoding {
270 string_type(unsigned int alignment, enum encoding encoding);
273 * encoding is suffixed with '_' to work-around a bug in older
274 * GCCs (before 6) that do not recognize hidden/shadowed enumeration as valid
275 * nested-name-specifiers.
277 const encoding encoding_;
280 virtual bool _is_equal(const type& base_other) const noexcept override;
283 class static_length_string_type : public string_type {
285 static_length_string_type(
286 unsigned int alignment, enum encoding in_encoding, uint64_t length);
287 virtual void accept(type_visitor& visitor) const override final;
289 const uint64_t length;
292 virtual bool _is_equal(const type& base_other) const noexcept override final;
295 class dynamic_length_string_type : public string_type {
297 dynamic_length_string_type(unsigned int alignment,
298 enum encoding in_encoding,
299 std::string length_field_name);
300 virtual void accept(type_visitor& visitor) const override final;
302 const std::string length_field_name;
305 virtual bool _is_equal(const type& base_other) const noexcept override final;
308 class null_terminated_string_type : public string_type {
310 null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
311 virtual void accept(type_visitor& visitor) const override final;
314 class structure_type : public type {
316 using fields = std::vector<field::cuptr>;
318 structure_type(unsigned int alignment, fields in_fields);
320 virtual void accept(type_visitor& visitor) const override final;
322 const fields _fields;
325 virtual bool _is_equal(const type& base_other) const noexcept override final;
328 class variant_type : public type {
330 using choices = std::vector<field::cuptr>;
332 variant_type(unsigned int alignment, std::string tag_name, choices in_choices);
334 virtual void accept(type_visitor& visitor) const override final;
336 const std::string tag_name;
337 const choices _choices;
340 virtual bool _is_equal(const type& base_other) const noexcept override final;
343 class field_visitor {
345 virtual ~field_visitor() = default;
346 virtual void visit(const field& field) = 0;
349 field_visitor() = default;
354 virtual ~type_visitor() = default;
355 virtual void visit(const integer_type& type) = 0;
356 virtual void visit(const floating_point_type& type) = 0;
357 virtual void visit(const signed_enumeration_type& type) = 0;
358 virtual void visit(const unsigned_enumeration_type& type) = 0;
359 virtual void visit(const static_length_array_type& type) = 0;
360 virtual void visit(const dynamic_length_array_type& type) = 0;
361 virtual void visit(const null_terminated_string_type& type) = 0;
362 virtual void visit(const static_length_string_type& type) = 0;
363 virtual void visit(const dynamic_length_string_type& type) = 0;
364 virtual void visit(const structure_type& type) = 0;
365 virtual void visit(const variant_type& type) = 0;
368 type_visitor() = default;
371 } /* namespace trace */
372 } /* namespace sessiond */
373 } /* namespace lttng */
375 #endif /* LTTNG_FIELD_H */