2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "tsdl-trace-class-visitor.hpp"
9 #include "clock-class.hpp"
11 #include <common/exception.hpp>
12 #include <common/format.hpp>
13 #include <common/make-unique.hpp>
14 #include <common/uuid.hpp>
16 #include <vendor/optional.hpp>
25 namespace lst
= lttng::sessiond::trace
;
26 namespace tsdl
= lttng::sessiond::tsdl
;
29 const auto ctf_spec_major
= 1;
30 const auto ctf_spec_minor
= 8;
33 * Although the CTF v1.8 specification recommends ignoring any leading underscore, Some readers,
34 * such as Babeltrace 1.x, expect special identifiers without a prepended underscore.
36 const std::set
<std::string
> safe_tsdl_identifiers
= {
54 * A previous implementation always prepended '_' to the identifiers in order to
55 * side-step the problem of escaping TSDL keywords and ensuring identifiers
56 * started with an alphabetic character.
58 * Changing this behaviour to a smarter algorithm would break readers that have
59 * come to expect this initial underscore.
61 std::string
escape_tsdl_identifier(const std::string
& original_identifier
)
63 if (original_identifier
.size() == 0) {
64 LTTNG_THROW_ERROR("Invalid 0-length identifier used in trace description");
67 if (safe_tsdl_identifiers
.find(original_identifier
) != safe_tsdl_identifiers
.end()) {
68 return original_identifier
;
71 std::string new_identifier
;
72 /* Optimisticly assume most identifiers are valid and allocate the same length. */
73 new_identifier
.reserve(original_identifier
.size());
76 /* Replace illegal characters by '_'. */
77 std::locale c_locale
{"C"};
78 for (const auto current_char
: original_identifier
) {
79 if (!std::isalnum(current_char
, c_locale
) && current_char
!= '_') {
80 new_identifier
+= '_';
82 new_identifier
+= current_char
;
86 return new_identifier
;
89 std::string
escape_tsdl_env_string_value(const std::string
& original_string
)
91 std::string escaped_string
;
93 escaped_string
.reserve(original_string
.size());
95 for (const auto c
: original_string
) {
98 escaped_string
+= "\\n";
101 escaped_string
+= "\\\\";
104 escaped_string
+= "\"";
112 return escaped_string
;
115 class tsdl_field_visitor
: public lttng::sessiond::trace::field_visitor
,
116 public lttng::sessiond::trace::type_visitor
{
118 tsdl_field_visitor(const lst::abi
& abi
,
119 unsigned int indentation_level
,
120 const nonstd::optional
<std::string
>& in_default_clock_class_name
=
122 _indentation_level
{indentation_level
},
124 _bypass_identifier_escape
{false},
125 _default_clock_class_name
{in_default_clock_class_name
?
126 in_default_clock_class_name
->c_str() :
131 std::string
& get_description()
137 virtual void visit(const lst::field
& field
) override final
140 * Hack: keep the name of the field being visited since
141 * the tracers can express sequences, variants, and arrays with an alignment
142 * constraint, which is not expressible in TSDL. To work around this limitation, an
143 * empty structure declaration is inserted when needed to express the aligment
144 * constraint. The name of this structure is generated using the field's name.
146 _current_field_name
.push(_bypass_identifier_escape
?
147 field
.name
: escape_tsdl_identifier(field
.name
));
149 field
.get_type().accept(*this);
151 _description
+= _current_field_name
.top();
152 _current_field_name
.pop();
155 * Some types requires suffixes to be appended (e.g. the length of arrays
156 * and sequences, the mappings of enumerations).
158 while (!_type_suffixes
.empty()) {
159 _description
+= _type_suffixes
.front();
160 _type_suffixes
.pop();
166 virtual void visit(const lst::integer_type
& type
) override final
168 _description
+= "integer { ";
170 /* Mandatory properties (no defaults). */
171 _description
+= fmt::format("size = {size}; align = {alignment};",
172 fmt::arg("size", type
.size
),
173 fmt::arg("alignment", type
.alignment
));
175 /* Defaults to unsigned. */
176 if (type
.signedness_
== lst::integer_type::signedness::SIGNED
) {
177 _description
+= " signed = true;";
180 /* Defaults to 10. */
181 if (type
.base_
!= lst::integer_type::base::DECIMAL
) {
184 switch (type
.base_
) {
185 case lst::integer_type::base::BINARY
:
188 case lst::integer_type::base::OCTAL
:
191 case lst::integer_type::base::HEXADECIMAL
:
195 LTTNG_THROW_ERROR(fmt::format(
196 "Unexpected base encountered while serializing integer type to TSDL: base = {}",
200 _description
+= fmt::format(" base = {};", base
);
203 /* Defaults to the trace's native byte order. */
204 if (type
.byte_order
!= _trace_abi
.byte_order
) {
205 const auto byte_order_str
= type
.byte_order
== lst::byte_order::BIG_ENDIAN_
? "be" : "le";
207 _description
+= fmt::format(" byte_order = {};", byte_order_str
);
210 if (_current_integer_encoding_override
) {
211 const char *encoding_str
;
213 switch (*_current_integer_encoding_override
) {
214 case lst::string_type::encoding::ASCII
:
215 encoding_str
= "ASCII";
217 case lst::string_type::encoding::UTF8
:
218 encoding_str
= "UTF8";
221 LTTNG_THROW_ERROR(fmt::format(
222 "Unexpected encoding encountered while serializing integer type to TSDL: encoding = {}",
223 (int) *_current_integer_encoding_override
));
226 _description
+= fmt::format(" encoding = {};", encoding_str
);
227 _current_integer_encoding_override
.reset();
230 if (std::find(type
.roles_
.begin(), type
.roles_
.end(),
231 lst::integer_type::role::DEFAULT_CLOCK_TIMESTAMP
) !=
233 std::find(type
.roles_
.begin(), type
.roles_
.end(),
234 lst::integer_type::role::
235 PACKET_END_DEFAULT_CLOCK_TIMESTAMP
) !=
237 LTTNG_ASSERT(_default_clock_class_name
);
238 _description
+= fmt::format(
239 " map = clock.{}.value;", _default_clock_class_name
);
242 _description
+= " }";
245 virtual void visit(const lst::floating_point_type
& type
) override final
247 _description
+= fmt::format(
248 "floating_point {{ align = {alignment}; mant_dig = {mantissa_digits}; exp_dig = {exponent_digits};",
249 fmt::arg("alignment", type
.alignment
),
250 fmt::arg("mantissa_digits", type
.mantissa_digits
),
251 fmt::arg("exponent_digits", type
.exponent_digits
));
253 /* Defaults to the trace's native byte order. */
254 if (type
.byte_order
!= _trace_abi
.byte_order
) {
255 const auto byte_order_str
= type
.byte_order
== lst::byte_order::BIG_ENDIAN_
? "be" : "le";
257 _description
+= fmt::format(" byte_order = {};", byte_order_str
);
260 _description
+= " }";
263 template <class EnumerationType
>
264 void visit_enumeration(const EnumerationType
& type
)
266 /* name follows, when applicable. */
267 _description
+= "enum : ";
269 tsdl_field_visitor integer_visitor
{_trace_abi
, _indentation_level
};
271 integer_visitor
.visit(static_cast<const lst::integer_type
&>(type
));
272 _description
+= integer_visitor
.get_description() + " {\n";
274 const auto mappings_indentation_level
= _indentation_level
+ 1;
276 bool first_mapping
= true;
277 for (const auto& mapping
: *type
._mappings
) {
278 if (!first_mapping
) {
279 _description
+= ",\n";
282 _description
.resize(_description
.size() + mappings_indentation_level
, '\t');
283 if (!mapping
.range
) {
284 _description
+= fmt::format("\"{}\"", mapping
.name
);
285 } else if (mapping
.range
->begin
== mapping
.range
->end
) {
286 _description
+= fmt::format(
287 "\"{mapping_name}\" = {mapping_value}",
288 fmt::arg("mapping_name", mapping
.name
),
289 fmt::arg("mapping_value", mapping
.range
->begin
));
291 _description
+= fmt::format(
292 "\"{mapping_name}\" = {mapping_range_begin} ... {mapping_range_end}",
293 fmt::arg("mapping_name", mapping
.name
),
294 fmt::arg("mapping_range_begin",
295 mapping
.range
->begin
),
296 fmt::arg("mapping_range_end", mapping
.range
->end
));
299 first_mapping
= false;
302 _description
+= "\n";
303 _description
.resize(_description
.size() + _indentation_level
, '\t');
307 virtual void visit(const lst::signed_enumeration_type
& type
) override final
309 visit_enumeration(type
);
312 virtual void visit(const lst::unsigned_enumeration_type
& type
) override final
314 visit_enumeration(type
);
317 virtual void visit(const lst::static_length_array_type
& type
) override final
319 if (type
.alignment
!= 0) {
320 LTTNG_ASSERT(_current_field_name
.size() > 0);
321 _description
+= fmt::format(
322 "struct {{ }} align({alignment}) {field_name}_padding;\n",
323 fmt::arg("alignment", type
.alignment
),
324 fmt::arg("field_name", _current_field_name
.top()));
325 _description
.resize(_description
.size() + _indentation_level
, '\t');
328 type
.element_type
->accept(*this);
329 _type_suffixes
.emplace(fmt::format("[{}]", type
.length
));
332 virtual void visit(const lst::dynamic_length_array_type
& type
) override final
334 if (type
.alignment
!= 0) {
336 * Note that this doesn't support nested sequences. For
337 * the moment, tracers can't express those. However, we
338 * could wrap nested sequences in structures, which
339 * would allow us to express alignment constraints.
341 LTTNG_ASSERT(_current_field_name
.size() > 0);
342 _description
+= fmt::format(
343 "struct {{ }} align({alignment}) {field_name}_padding;\n",
344 fmt::arg("alignment", type
.alignment
),
345 fmt::arg("field_name", _current_field_name
.top()));
346 _description
.resize(_description
.size() + _indentation_level
, '\t');
349 type
.element_type
->accept(*this);
350 _type_suffixes
.emplace(fmt::format("[{}]",
351 _bypass_identifier_escape
?
352 *(type
.length_field_location
.elements_
.end() - 1) :
353 escape_tsdl_identifier(*(type
.length_field_location
.elements_
.end() - 1))));
356 virtual void visit(const lst::static_length_blob_type
& type
) override final
358 /* This type doesn't exist in CTF 1.x, express it as a static length array of uint8_t. */
359 std::unique_ptr
<const lst::type
> uint8_element
= lttng::make_unique
<lst::integer_type
>(8,
360 _trace_abi
.byte_order
, 8, lst::integer_type::signedness::UNSIGNED
,
361 lst::integer_type::base::HEXADECIMAL
);
362 const auto array
= lttng::make_unique
<lst::static_length_array_type
>(
363 type
.alignment
, std::move(uint8_element
), type
.length_bytes
);
368 virtual void visit(const lst::dynamic_length_blob_type
& type
) override final
370 /* This type doesn't exist in CTF 1.x, express it as a dynamic length array of uint8_t. */
371 std::unique_ptr
<const lst::type
> uint8_element
= lttng::make_unique
<lst::integer_type
>(0,
372 _trace_abi
.byte_order
, 8, lst::integer_type::signedness::UNSIGNED
,
373 lst::integer_type::base::HEXADECIMAL
);
374 const auto array
= lttng::make_unique
<lst::dynamic_length_array_type
>(
375 type
.alignment
, std::move(uint8_element
), type
.length_field_location
);
380 virtual void visit(const lst::null_terminated_string_type
& type
) override final
382 /* Defaults to UTF-8. */
383 if (type
.encoding_
== lst::null_terminated_string_type::encoding::ASCII
) {
384 _description
+= "string { encoding = ASCII }";
386 _description
+= "string";
390 virtual void visit(const lst::structure_type
& type
) override final
392 _indentation_level
++;
393 _description
+= "struct {";
395 const auto previous_bypass_identifier_escape
= _bypass_identifier_escape
;
396 _bypass_identifier_escape
= false;
397 for (const auto& field
: type
._fields
) {
398 _description
+= "\n";
399 _description
.resize(_description
.size() + _indentation_level
, '\t');
400 field
->accept(*this);
403 _bypass_identifier_escape
= previous_bypass_identifier_escape
;
405 _indentation_level
--;
406 if (type
._fields
.size() != 0) {
407 _description
+= "\n";
408 _description
.resize(_description
.size() + _indentation_level
, '\t');
414 template <class MappingIntegerType
>
415 void visit_variant(const lst::variant_type
<MappingIntegerType
>& type
)
417 if (type
.alignment
!= 0) {
418 LTTNG_ASSERT(_current_field_name
.size() > 0);
419 _description
+= fmt::format(
420 "struct {{ }} align({alignment}) {field_name}_padding;\n",
421 fmt::arg("alignment", type
.alignment
),
422 fmt::arg("field_name", _current_field_name
.top()));
423 _description
.resize(_description
.size() + _indentation_level
, '\t');
426 _indentation_level
++;
427 _description
+= fmt::format("variant <{}> {{\n",
428 _bypass_identifier_escape
?
429 *(type
.selector_field_location
.elements_
.end() - 1) :
430 escape_tsdl_identifier(*(type
.selector_field_location
.elements_
.end() - 1)));
433 * The CTF 1.8 specification only recommends that implementations ignore
434 * leading underscores in field names. Both babeltrace 1 and 2 expect the
435 * variant choice and enumeration mapping name to match perfectly. Given that we
436 * don't have access to the tag in this context, we have to assume they match.
438 const auto previous_bypass_identifier_escape
= _bypass_identifier_escape
;
439 _bypass_identifier_escape
= true;
440 for (const auto& field
: type
._choices
) {
441 _description
.resize(_description
.size() + _indentation_level
, '\t');
442 field
.second
->accept(*this);
443 _description
+= "\n";
446 _bypass_identifier_escape
= previous_bypass_identifier_escape
;
448 _indentation_level
--;
449 _description
.resize(_description
.size() + _indentation_level
, '\t');
453 virtual void visit(const lst::variant_type
<lst::signed_enumeration_type::mapping::range_t::range_integer_t
>& type
) override final
458 virtual void visit(const lst::variant_type
<lst::unsigned_enumeration_type::mapping::range_t::range_integer_t
>& type
) override final
463 lst::type::cuptr
create_character_type(enum lst::string_type::encoding encoding
)
465 _current_integer_encoding_override
= encoding
;
466 return lttng::make_unique
<lst::integer_type
>(8, _trace_abi
.byte_order
, 8,
467 lst::integer_type::signedness::UNSIGNED
,
468 lst::integer_type::base::DECIMAL
);
471 virtual void visit(const lst::static_length_string_type
& type
) override final
474 * TSDL expresses static-length strings as arrays of 8-bit integer with
475 * an encoding specified.
477 const auto char_array
= lttng::make_unique
<lst::static_length_array_type
>(
478 type
.alignment
, create_character_type(type
.encoding_
), type
.length
);
483 virtual void visit(const lst::dynamic_length_string_type
& type
) override final
486 * TSDL expresses dynamic-length strings as arrays of 8-bit integer with
487 * an encoding specified.
489 const auto char_sequence
= lttng::make_unique
<lst::dynamic_length_array_type
>(
490 type
.alignment
, create_character_type(type
.encoding_
),
491 type
.length_field_location
);
493 visit(*char_sequence
);
496 std::stack
<std::string
> _current_field_name
;
498 * Encoding to specify for the next serialized integer type.
499 * Since the integer_type does not allow an encoding to be specified (it is a TSDL-specific
500 * concept), this attribute is used when expressing static or dynamic length strings as
501 * arrays/sequences of bytes with an encoding.
503 nonstd::optional
<enum lst::string_type::encoding
> _current_integer_encoding_override
;
505 unsigned int _indentation_level
;
506 const lst::abi
& _trace_abi
;
508 std::queue
<std::string
> _type_suffixes
;
510 /* Description in TSDL format. */
511 std::string _description
;
513 bool _bypass_identifier_escape
;
514 const char *_default_clock_class_name
;
518 tsdl::trace_class_visitor::trace_class_visitor(const lst::abi
& trace_abi
,
519 tsdl::append_metadata_fragment_function append_metadata_fragment
) :
520 _trace_abi
{trace_abi
},
521 _append_metadata_fragment(append_metadata_fragment
)
525 void tsdl::trace_class_visitor::append_metadata_fragment(const std::string
& fragment
) const
527 _append_metadata_fragment(fragment
);
530 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::trace_class
& trace_class
)
532 tsdl_field_visitor
packet_header_visitor(trace_class
.abi
, 1);
534 trace_class
.get_packet_header()->accept(packet_header_visitor
);
536 /* Declare type aliases, trace class, and packet header. */
537 auto trace_class_tsdl
= fmt::format(
538 "/* CTF {ctf_major}.{ctf_minor} */\n\n"
540 " major = {ctf_major};\n"
541 " minor = {ctf_minor};\n"
542 " uuid = \"{uuid}\";\n"
543 " byte_order = {byte_order};\n"
544 " packet.header := {packet_header_layout};\n"
546 fmt::arg("ctf_major", ctf_spec_major
),
547 fmt::arg("ctf_minor", ctf_spec_minor
),
548 fmt::arg("uint8_t_alignment", trace_class
.abi
.uint8_t_alignment
),
549 fmt::arg("uint16_t_alignment", trace_class
.abi
.uint16_t_alignment
),
550 fmt::arg("uint32_t_alignment", trace_class
.abi
.uint32_t_alignment
),
551 fmt::arg("uint64_t_alignment", trace_class
.abi
.uint64_t_alignment
),
552 fmt::arg("long_alignment", trace_class
.abi
.long_alignment
),
553 fmt::arg("long_size", trace_class
.abi
.long_alignment
),
554 fmt::arg("bits_per_long", trace_class
.abi
.bits_per_long
),
555 fmt::arg("uuid", lttng::utils::uuid_to_str(trace_class
.uuid
)),
556 fmt::arg("byte_order",
557 trace_class
.abi
.byte_order
== lst::byte_order::BIG_ENDIAN_
? "be" : "le"),
558 fmt::arg("packet_header_layout", packet_header_visitor
.get_description()));
560 /* Declare trace scope and type aliases. */
561 append_metadata_fragment(trace_class_tsdl
);
564 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::clock_class
& clock_class
)
566 auto uuid_str
= clock_class
.uuid
?
567 fmt::format(" uuid = \"{}\";\n",
568 lttng::utils::uuid_to_str(*clock_class
.uuid
)) :
571 /* Assumes a single clock that maps to specific stream class fields/roles. */
572 auto clock_class_str
= fmt::format(
574 " name = \"{name}\";\n"
577 " description = \"{description}\";\n"
578 " freq = {frequency};\n"
579 " offset = {offset};\n"
582 fmt::arg("name", clock_class
.name
),
583 fmt::arg("uuid", uuid_str
),
584 fmt::arg("description", clock_class
.description
),
585 fmt::arg("frequency", clock_class
.frequency
),
586 fmt::arg("offset", clock_class
.offset
));
588 append_metadata_fragment(clock_class_str
);
591 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::stream_class
& stream_class
)
593 auto stream_class_str
= fmt::format("stream {{\n"
594 " id = {};\n", stream_class
.id
);
596 const auto *event_header
= stream_class
.get_event_header();
598 auto event_header_visitor
= tsdl_field_visitor(
599 _trace_abi
, 1, stream_class
.default_clock_class_name
);
601 event_header
->accept(event_header_visitor
);
602 stream_class_str
+= fmt::format(" event.header := {};\n",
603 event_header_visitor
.get_description());
606 const auto *packet_context
= stream_class
.get_packet_context();
607 if (packet_context
) {
608 auto packet_context_visitor
= tsdl_field_visitor(
609 _trace_abi
, 1, stream_class
.default_clock_class_name
);
611 packet_context
->accept(packet_context_visitor
);
612 stream_class_str
+= fmt::format(" packet.context := {};\n",
613 packet_context_visitor
.get_description());
616 const auto *event_context
= stream_class
.get_event_context();
618 auto event_context_visitor
= tsdl_field_visitor(_trace_abi
, 1);
620 event_context
->accept(event_context_visitor
);
621 stream_class_str
+= fmt::format(" event.context := {};\n",
622 event_context_visitor
.get_description());
625 stream_class_str
+= "};\n\n";
627 append_metadata_fragment(stream_class_str
);
630 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::event_class
& event_class
)
632 auto event_class_str
= fmt::format("event {{\n"
633 " name = \"{name}\";\n"
635 " stream_id = {stream_class_id};\n"
636 " loglevel = {log_level};\n",
637 fmt::arg("name", event_class
.name
),
638 fmt::arg("id", event_class
.id
),
639 fmt::arg("stream_class_id", event_class
.stream_class_id
),
640 fmt::arg("log_level", event_class
.log_level
));
642 if (event_class
.model_emf_uri
) {
643 event_class_str
+= fmt::format(
644 " model.emf.uri = \"{}\";\n", *event_class
.model_emf_uri
);
647 auto payload_visitor
= tsdl_field_visitor(_trace_abi
, 1);
649 event_class
.payload
->accept(static_cast<lst::type_visitor
&>(payload_visitor
));
651 event_class_str
+= fmt::format(
652 " fields := {};\n}};\n\n", payload_visitor
.get_description());
654 append_metadata_fragment(event_class_str
);
657 void tsdl::trace_class_visitor::environment_begin()
659 _environment
+= "env {\n";
662 void tsdl::trace_class_visitor::visit(
663 const lttng::sessiond::trace::environment_field
<int64_t>& field
)
665 _environment
+= fmt::format(" {} = {};\n", field
.name
, field
.value
);
668 void tsdl::trace_class_visitor::visit(
669 const lttng::sessiond::trace::environment_field
<const char *>& field
)
671 _environment
+= fmt::format(
672 " {} = \"{}\";\n", field
.name
, escape_tsdl_env_string_value(field
.value
));
675 void tsdl::trace_class_visitor::environment_end()
677 _environment
+= "};\n\n";
678 append_metadata_fragment(_environment
);
679 _environment
.clear();