2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "clock-class.hpp"
9 #include "tsdl-trace-class-visitor.hpp"
11 #include <common/exception.hpp>
12 #include <common/format.hpp>
13 #include <common/make-unique.hpp>
14 #include <common/uuid.hpp>
20 namespace lst
= lttng::sessiond::trace
;
21 namespace tsdl
= lttng::sessiond::tsdl
;
24 const auto ctf_spec_major
= 1;
25 const auto ctf_spec_minor
= 8;
28 * A previous implementation always prepended '_' to the identifiers in order to
29 * side-step the problem of escaping TSDL keywords and ensuring identifiers
30 * started with an alphabetic character.
32 * Changing this behaviour to a smarter algorithm would break readers that have
33 * come to expect this initial underscore.
35 std::string
escape_tsdl_identifier(const std::string
& original_identifier
)
37 if (original_identifier
.size() == 0) {
38 LTTNG_THROW_ERROR("Invalid 0-length identifier used in trace description");
41 std::string new_identifier
;
42 /* Optimisticly assume most identifiers are valid and allocate the same length. */
43 new_identifier
.reserve(original_identifier
.size());
46 /* Replace illegal characters by '_'. */
47 std::locale c_locale
{"C"};
48 for (const auto current_char
: original_identifier
) {
49 if (!std::isalnum(current_char
, c_locale
) && current_char
!= '_') {
50 new_identifier
+= '_';
52 new_identifier
+= current_char
;
56 return new_identifier
;
59 std::string
escape_tsdl_env_string_value(const std::string
& original_string
)
61 std::string escaped_string
;
63 escaped_string
.reserve(original_string
.size());
65 for (const auto c
: original_string
) {
68 escaped_string
+= "\\n";
71 escaped_string
+= "\\\\";
74 escaped_string
+= "\"";
82 return escaped_string
;
85 class tsdl_field_visitor
: public lttng::sessiond::trace::field_visitor
,
86 public lttng::sessiond::trace::type_visitor
{
88 tsdl_field_visitor(const lst::abi
& abi
, unsigned int indentation_level
) :
89 _indentation_level
{indentation_level
}, _trace_abi
{abi
}
93 std::string
& get_description()
99 virtual void visit(const lst::field
& field
) override final
102 * Hack: keep the name of the field being visited since
103 * the tracers can express sequences, variants, and arrays with an alignment
104 * constraint, which is not expressible in TSDL. To work around this limitation, an
105 * empty structure declaration is inserted when needed to express the aligment
106 * constraint. The name of this structure is generated using the field's name.
108 _escaped_current_field_name
= escape_tsdl_identifier(field
.name
);
110 field
._type
->accept(*this);
112 _description
+= _escaped_current_field_name
;
115 * Some types requires suffixes to be appended (e.g. the length of arrays
116 * and sequences, the mappings of enumerations).
118 while (!_type_suffixes
.empty()) {
119 _description
+= _type_suffixes
.front();
120 _type_suffixes
.pop();
124 _escaped_current_field_name
.clear();
127 virtual void visit(const lst::integer_type
& type
) override final
129 _description
+= "integer { ";
131 /* Mandatory properties (no defaults). */
132 _description
+= fmt::format("size = {size}; align = {alignment};",
133 fmt::arg("size", type
.size
),
134 fmt::arg("alignment", type
.alignment
));
136 /* Defaults to unsigned. */
137 if (type
.signedness_
== lst::integer_type::signedness::SIGNED
) {
138 _description
+= " signed = true;";
141 /* Defaults to 10. */
142 if (type
.base_
!= lst::integer_type::base::DECIMAL
) {
145 switch (type
.base_
) {
146 case lst::integer_type::base::BINARY
:
149 case lst::integer_type::base::OCTAL
:
152 case lst::integer_type::base::HEXADECIMAL
:
156 LTTNG_THROW_ERROR(fmt::format(
157 "Unexpected base encountered while serializing integer type to TSDL: base = {}",
161 _description
+= fmt::format(" base = {};", base
);
164 /* Defaults to the trace's native byte order. */
165 if (type
.byte_order
!= _trace_abi
.byte_order
) {
166 const auto byte_order_str
= type
.byte_order
== lst::byte_order::BIG_ENDIAN_
? "be" : "le";
168 _description
+= fmt::format(" byte_order = {};", byte_order_str
);
171 if (_current_integer_encoding_override
) {
172 const char *encoding_str
;
174 switch (*_current_integer_encoding_override
) {
175 case lst::string_type::encoding::ASCII
:
176 encoding_str
= "ASCII";
178 case lst::string_type::encoding::UTF8
:
179 encoding_str
= "UTF8";
182 LTTNG_THROW_ERROR(fmt::format(
183 "Unexpected encoding encountered while serializing integer type to TSDL: encoding = {}",
184 (int) *_current_integer_encoding_override
));
187 _description
+= fmt::format(" encoding = {};", encoding_str
);
188 _current_integer_encoding_override
.reset();
191 _description
+= " }";
194 virtual void visit(const lst::floating_point_type
& type
) override final
196 _description
+= fmt::format(
197 "floating_point {{ align = {alignment}; mant_dig = {mantissa_digits}; exp_dig = {exponent_digits};",
198 fmt::arg("alignment", type
.alignment
),
199 fmt::arg("mantissa_digits", type
.mantissa_digits
),
200 fmt::arg("exponent_digits", type
.exponent_digits
));
202 /* Defaults to the trace's native byte order. */
203 if (type
.byte_order
!= _trace_abi
.byte_order
) {
204 const auto byte_order_str
= type
.byte_order
== lst::byte_order::BIG_ENDIAN_
? "be" : "le";
206 _description
+= fmt::format(" byte_order = {};", byte_order_str
);
209 _description
+= " }";
212 template <class EnumerationType
>
213 void visit_enumeration(const EnumerationType
& type
)
215 /* name follows, when applicable. */
216 _description
+= "enum : ";
218 tsdl_field_visitor integer_visitor
{_trace_abi
, _indentation_level
};
220 integer_visitor
.visit(static_cast<const lst::integer_type
&>(type
));
221 _description
+= integer_visitor
.get_description() + " {\n";
223 const auto mappings_indentation_level
= _indentation_level
+ 1;
225 bool first_mapping
= true;
226 for (const auto& mapping
: *type
._mappings
) {
227 if (!first_mapping
) {
228 _description
+= ",\n";
231 _description
.resize(_description
.size() + mappings_indentation_level
, '\t');
232 if (!mapping
.range
) {
233 _description
+= fmt::format("\"{}\"", mapping
.name
);
234 } else if (mapping
.range
->begin
== mapping
.range
->end
) {
235 _description
+= fmt::format(
236 "\"{mapping_name}\" = {mapping_value}",
237 fmt::arg("mapping_name", mapping
.name
),
238 fmt::arg("mapping_value", mapping
.range
->begin
));
240 _description
+= fmt::format(
241 "\"{mapping_name}\" = {mapping_range_begin} ... {mapping_range_end}",
242 fmt::arg("mapping_name", mapping
.name
),
243 fmt::arg("mapping_range_begin",
244 mapping
.range
->begin
),
245 fmt::arg("mapping_range_end", mapping
.range
->end
));
248 first_mapping
= false;
251 _description
+= "\n";
252 _description
.resize(_description
.size() + _indentation_level
, '\t');
256 virtual void visit(const lst::signed_enumeration_type
& type
) override final
258 visit_enumeration(type
);
261 virtual void visit(const lst::unsigned_enumeration_type
& type
) override final
263 visit_enumeration(type
);
266 virtual void visit(const lst::static_length_array_type
& type
) override final
268 if (type
.alignment
!= 0) {
269 LTTNG_ASSERT(_escaped_current_field_name
.size() > 0);
270 _description
+= fmt::format(
271 "struct {{ }} align({alignment}) {field_name}_padding;\n",
272 fmt::arg("alignment", type
.alignment
),
273 fmt::arg("field_name", _escaped_current_field_name
));
274 _description
.resize(_description
.size() + _indentation_level
, '\t');
277 type
.element_type
->accept(*this);
278 _type_suffixes
.emplace(fmt::format("[{}]", type
.length
));
281 virtual void visit(const lst::dynamic_length_array_type
& type
) override final
283 if (type
.alignment
!= 0) {
285 * Note that this doesn't support nested sequences. For
286 * the moment, tracers can't express those. However, we
287 * could wrap nested sequences in structures, which
288 * would allow us to express alignment constraints.
290 LTTNG_ASSERT(_escaped_current_field_name
.size() > 0);
291 _description
+= fmt::format(
292 "struct {{ }} align({alignment}) {field_name}_padding;\n",
293 fmt::arg("alignment", type
.alignment
),
294 fmt::arg("field_name", _escaped_current_field_name
));
295 _description
.resize(_description
.size() + _indentation_level
, '\t');
298 type
.element_type
->accept(*this);
299 _type_suffixes
.emplace(fmt::format(
300 "[{}]", escape_tsdl_identifier(type
.length_field_name
)));
303 virtual void visit(const lst::static_length_blob_type
& type
) override final
305 /* This type doesn't exist in CTF 1.x, express it as a static length array of uint8_t. */
306 std::unique_ptr
<const lst::type
> uint8_element
= lttng::make_unique
<lst::integer_type
>(8,
307 _trace_abi
.byte_order
, 8, lst::integer_type::signedness::UNSIGNED
,
308 lst::integer_type::base::HEXADECIMAL
);
309 const auto array
= lttng::make_unique
<lst::static_length_array_type
>(
310 type
.alignment
, std::move(uint8_element
), type
.length_bytes
);
315 virtual void visit(const lst::dynamic_length_blob_type
& type
) override final
317 /* This type doesn't exist in CTF 1.x, express it as a dynamic length array of uint8_t. */
318 std::unique_ptr
<const lst::type
> uint8_element
= lttng::make_unique
<lst::integer_type
>(0,
319 _trace_abi
.byte_order
, 8, lst::integer_type::signedness::UNSIGNED
,
320 lst::integer_type::base::HEXADECIMAL
);
321 const auto array
= lttng::make_unique
<lst::dynamic_length_array_type
>(
322 type
.alignment
, std::move(uint8_element
), type
.length_field_name
);
327 virtual void visit(const lst::null_terminated_string_type
& type
) override final
329 /* Defaults to UTF-8. */
330 if (type
.encoding_
== lst::null_terminated_string_type::encoding::ASCII
) {
331 _description
+= "string { encoding = ASCII }";
333 _description
+= "string";
337 virtual void visit(const lst::structure_type
& type
) override final
339 _indentation_level
++;
340 _description
+= "struct {";
342 for (const auto& field
: type
._fields
) {
343 _description
+= "\n";
344 _description
.resize(_description
.size() + _indentation_level
, '\t');
345 field
->accept(*this);
348 _indentation_level
--;
349 if (type
._fields
.size() != 0) {
350 _description
+= "\n";
351 _description
.resize(_description
.size() + _indentation_level
, '\t');
354 _description
+= "};";
357 virtual void visit(const lst::variant_type
& type
) override final
359 if (type
.alignment
!= 0) {
360 LTTNG_ASSERT(_escaped_current_field_name
.size() > 0);
361 _description
+= fmt::format(
362 "struct {{ }} align({alignment}) {field_name}_padding;\n",
363 fmt::arg("alignment", type
.alignment
),
364 fmt::arg("field_name", _escaped_current_field_name
));
365 _description
.resize(_description
.size() + _indentation_level
, '\t');
368 _indentation_level
++;
369 _description
+= fmt::format("variant <{}> {\n", escape_tsdl_identifier(type
.tag_name
));
371 bool first_field
= true;
372 for (const auto& field
: type
._choices
) {
374 _description
+= ",\n";
377 _description
.resize(_description
.size() + _indentation_level
, '\t');
378 field
->accept(*this);
382 _description
+= "\n";
383 _description
.resize(_description
.size() + _indentation_level
, '\t');
384 _description
+= "};";
385 _indentation_level
--;
388 lst::type::cuptr
create_character_type(enum lst::string_type::encoding encoding
)
390 _current_integer_encoding_override
= encoding
;
391 return lttng::make_unique
<lst::integer_type
>(8, _trace_abi
.byte_order
, 8,
392 lst::integer_type::signedness::UNSIGNED
,
393 lst::integer_type::base::DECIMAL
);
396 virtual void visit(const lst::static_length_string_type
& type
) override final
399 * TSDL expresses static-length strings as arrays of 8-bit integer with
400 * an encoding specified.
402 const auto char_array
= lttng::make_unique
<lst::static_length_array_type
>(
403 type
.alignment
, create_character_type(type
.encoding_
), type
.length
);
408 virtual void visit(const lst::dynamic_length_string_type
& type
) override final
411 * TSDL expresses dynamic-length strings as arrays of 8-bit integer with
412 * an encoding specified.
414 const auto char_sequence
= lttng::make_unique
<lst::dynamic_length_array_type
>(
415 type
.alignment
, create_character_type(type
.encoding_
),
416 type
.length_field_name
);
418 visit(*char_sequence
);
421 std::string _escaped_current_field_name
;
423 * Encoding to specify for the next serialized integer type.
424 * Since the integer_type does not allow an encoding to be specified (it is a TSDL-specific
425 * concept), this attribute is used when expressing static or dynamic length strings as
426 * arrays/sequences of bytes with an encoding.
428 nonstd::optional
<enum lst::string_type::encoding
> _current_integer_encoding_override
;
430 unsigned int _indentation_level
;
431 const lst::abi
& _trace_abi
;
433 std::queue
<std::string
> _type_suffixes
;
435 /* Description in TSDL format. */
436 std::string _description
;
440 tsdl::trace_class_visitor::trace_class_visitor(const lst::abi
& trace_abi
,
441 tsdl::append_metadata_fragment_function append_metadata_fragment
) :
442 _trace_abi
{trace_abi
}, _append_metadata_fragment(append_metadata_fragment
)
446 void tsdl::trace_class_visitor::append_metadata_fragment(const std::string
& fragment
) const
448 _append_metadata_fragment(fragment
);
451 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::trace_class
& trace_class
)
453 /* Declare type aliases, trace class, and packet header. */
454 auto trace_class_tsdl
= fmt::format(
455 "/* CTF {ctf_major}.{ctf_minor} */\n\n"
456 "typealias integer {{ size = 8; align = {uint8_t_alignment}; signed = false; }} := uint8_t;\n"
457 "typealias integer {{ size = 16; align = {uint16_t_alignment}; signed = false; }} := uint16_t;\n"
458 "typealias integer {{ size = 32; align = {uint32_t_alignment}; signed = false; }} := uint32_t;\n"
459 "typealias integer {{ size = 64; align = {uint64_t_alignment}; signed = false; }} := uint64_t;\n"
460 "typealias integer {{ size = {bits_per_long}; align = {long_alignment}; signed = false; }} := unsigned long;\n"
461 "typealias integer {{ size = 5; align = 1; signed = false; }} := uint5_t;\n"
462 "typealias integer {{ size = 27; align = 1; signed = false; }} := uint27_t;\n"
465 " major = {ctf_major};\n"
466 " minor = {ctf_minor};\n"
467 " uuid = \"{uuid}\";\n"
468 " byte_order = {byte_order};\n"
469 " packet.header := struct {{\n"
471 " uint8_t uuid[16];\n"
472 " uint32_t stream_id;\n"
473 " uint64_t stream_instance_id;\n"
476 fmt::arg("ctf_major", ctf_spec_major
),
477 fmt::arg("ctf_minor", ctf_spec_minor
),
478 fmt::arg("uint8_t_alignment", trace_class
.abi
.uint8_t_alignment
),
479 fmt::arg("uint16_t_alignment", trace_class
.abi
.uint16_t_alignment
),
480 fmt::arg("uint32_t_alignment", trace_class
.abi
.uint32_t_alignment
),
481 fmt::arg("uint64_t_alignment", trace_class
.abi
.uint64_t_alignment
),
482 fmt::arg("long_alignment", trace_class
.abi
.long_alignment
),
483 fmt::arg("long_size", trace_class
.abi
.long_alignment
),
484 fmt::arg("bits_per_long", trace_class
.abi
.bits_per_long
),
485 fmt::arg("uuid", lttng::utils::uuid_to_str(trace_class
.uuid
)),
486 fmt::arg("byte_order",
487 trace_class
.abi
.byte_order
== lst::byte_order::BIG_ENDIAN_
?
491 /* Declare trace scope and type aliases. */
492 append_metadata_fragment(std::move(trace_class_tsdl
));
495 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::clock_class
& clock_class
)
497 auto uuid_str
= clock_class
.uuid
?
498 fmt::format(" uuid = \"{}\";\n",
499 lttng::utils::uuid_to_str(*clock_class
.uuid
)) :
502 /* Assumes a single clock that maps to specific stream class fields/roles. */
503 auto clock_class_str
= fmt::format(
505 " name = \"{name}\";\n"
508 " description = \"{description}\";\n"
509 " freq = {frequency};\n"
510 " offset = {offset};\n"
513 "typealias integer {{\n"
514 " size = 27; align = 1; signed = false;\n"
515 " map = clock.{name}.value;\n"
516 "}} := uint27_clock_{name}_t;\n"
518 "typealias integer {{\n"
519 " size = 32; align = {uint32_t_alignment}; signed = false;\n"
520 " map = clock.{name}.value;\n"
521 "}} := uint32_clock_{name}_t;\n"
523 "typealias integer {{\n"
524 " size = 64; align = {uint64_t_alignment}; signed = false;\n"
525 " map = clock.{name}.value;\n"
526 "}} := uint64_clock_{name}_t;\n"
528 "struct packet_context {{\n"
529 " uint64_clock_{name}_t timestamp_begin;\n"
530 " uint64_clock_{name}_t timestamp_end;\n"
531 " uint64_t content_size;\n"
532 " uint64_t packet_size;\n"
533 " uint64_t packet_seq_num;\n"
534 " unsigned long events_discarded;\n"
535 " uint32_t cpu_id;\n"
538 "struct event_header_compact {{\n"
539 " enum : uint5_t {{ compact = 0 ... 30, extended = 31 }} id;\n"
542 " uint27_clock_{name}_t timestamp;\n"
546 " uint64_clock_{name}_t timestamp;\n"
549 "}} align({uint32_t_alignment});\n"
551 "struct event_header_large {{\n"
552 " enum : uint16_t {{ compact = 0 ... 65534, extended = 65535 }} id;\n"
555 " uint32_clock_{name}_t timestamp;\n"
559 " uint64_clock_{name}_t timestamp;\n"
562 "}} align({uint16_t_alignment});\n\n",
563 fmt::arg("name", clock_class
.name
),
564 fmt::arg("uuid", uuid_str
),
565 fmt::arg("description", clock_class
.description
),
566 fmt::arg("frequency", clock_class
.frequency
),
567 fmt::arg("offset", clock_class
.offset
),
568 fmt::arg("uint16_t_alignment", _trace_abi
.uint16_t_alignment
),
569 fmt::arg("uint32_t_alignment", _trace_abi
.uint32_t_alignment
),
570 fmt::arg("uint64_t_alignment", _trace_abi
.uint64_t_alignment
));
572 append_metadata_fragment(std::move(clock_class_str
));
575 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::stream_class
& stream_class
)
577 /* Declare stream. */
578 auto stream_class_str
= fmt::format("stream {{\n"
580 " event.header := {header_type};\n"
581 " packet.context := struct packet_context;\n",
582 fmt::arg("id", stream_class
.id
),
583 fmt::arg("header_type", stream_class
.header_type_
== lst::stream_class::header_type::COMPACT
?
584 "struct event_header_compact" :
585 "struct event_header_large"));
587 auto context_field_visitor
= tsdl_field_visitor(_trace_abi
, 1);
589 stream_class
.get_context().accept(static_cast<lst::type_visitor
&>(context_field_visitor
));
591 stream_class_str
+= fmt::format(" event.context := {}\n}};\n\n",
592 context_field_visitor
.get_description());
594 append_metadata_fragment(stream_class_str
);
597 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::event_class
& event_class
)
599 auto event_class_str
= fmt::format("event {{\n"
600 " name = \"{name}\";\n"
602 " stream_id = {stream_class_id};\n"
603 " loglevel = {log_level};\n",
604 fmt::arg("name", event_class
.name
),
605 fmt::arg("id", event_class
.id
),
606 fmt::arg("stream_class_id", event_class
.stream_class_id
),
607 fmt::arg("log_level", event_class
.log_level
));
609 if (event_class
.model_emf_uri
) {
610 event_class_str
+= fmt::format(
611 " model.emf.uri = \"{}\";\n", *event_class
.model_emf_uri
);
614 auto payload_visitor
= tsdl_field_visitor(_trace_abi
, 1);
616 event_class
.payload
->accept(static_cast<lst::type_visitor
&>(payload_visitor
));
618 event_class_str
+= fmt::format(
619 " fields := {}\n}};\n\n", payload_visitor
.get_description());
621 append_metadata_fragment(event_class_str
);
624 void tsdl::trace_class_visitor::environment_begin()
626 _environment
+= "env {\n";
629 void tsdl::trace_class_visitor::visit(
630 const lttng::sessiond::trace::environment_field
<int64_t>& field
)
632 _environment
+= fmt::format(" {} = {};\n", field
.name
, field
.value
);
635 void tsdl::trace_class_visitor::visit(
636 const lttng::sessiond::trace::environment_field
<const char *>& field
)
638 _environment
+= fmt::format(
639 " {} = \"{}\";\n", field
.name
, escape_tsdl_env_string_value(field
.value
));
642 void tsdl::trace_class_visitor::environment_end()
644 _environment
+= "};\n\n";
645 append_metadata_fragment(_environment
);
646 _environment
.clear();