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
) {
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::null_terminated_string_type
& type
) override final
305 /* Defaults to UTF-8. */
306 if (type
.encoding
== lst::null_terminated_string_type::encoding::ASCII
) {
307 _description
+= "string { encoding = ASCII }";
309 _description
+= "string";
313 virtual void visit(const lst::structure_type
& type
) override final
315 _indentation_level
++;
316 _description
+= "struct {";
318 for (const auto& field
: type
._fields
) {
319 _description
+= "\n";
320 _description
.resize(_description
.size() + _indentation_level
, '\t');
321 field
->accept(*this);
324 _indentation_level
--;
325 if (type
._fields
.size() != 0) {
326 _description
+= "\n";
327 _description
.resize(_description
.size() + _indentation_level
, '\t');
330 _description
+= "};";
333 virtual void visit(const lst::variant_type
& type
) override final
335 if (type
.alignment
!= 0) {
336 LTTNG_ASSERT(_escaped_current_field_name
.size() > 0);
337 _description
+= fmt::format(
338 "struct {{ }} align({alignment}) {field_name}_padding;\n",
339 fmt::arg("alignment", type
.alignment
),
340 fmt::arg("field_name", _escaped_current_field_name
));
341 _description
.resize(_description
.size() + _indentation_level
, '\t');
344 _indentation_level
++;
345 _description
+= fmt::format("variant <{}> {\n", escape_tsdl_identifier(type
.tag_name
));
347 bool first_field
= true;
348 for (const auto& field
: type
._choices
) {
350 _description
+= ",\n";
353 _description
.resize(_description
.size() + _indentation_level
, '\t');
354 field
->accept(*this);
358 _description
+= "\n";
359 _description
.resize(_description
.size() + _indentation_level
, '\t');
360 _description
+= "};";
361 _indentation_level
--;
364 lst::type::cuptr
create_character_type(enum lst::string_type::encoding encoding
)
366 _current_integer_encoding_override
= encoding
;
367 return lttng::make_unique
<lst::integer_type
>(8, _trace_abi
.byte_order
, 8,
368 lst::integer_type::signedness::UNSIGNED
,
369 lst::integer_type::base::DECIMAL
);
372 virtual void visit(const lst::static_length_string_type
& type
) override final
375 * TSDL expresses static-length strings as arrays of 8-bit integer with
376 * an encoding specified.
378 const auto char_array
= lttng::make_unique
<lst::static_length_array_type
>(
379 type
.alignment
, create_character_type(type
.encoding
), type
.length
);
384 virtual void visit(const lst::dynamic_length_string_type
& type
) override final
387 * TSDL expresses dynamic-length strings as arrays of 8-bit integer with
388 * an encoding specified.
390 const auto char_sequence
= lttng::make_unique
<lst::dynamic_length_array_type
>(
391 type
.alignment
, create_character_type(type
.encoding
),
392 type
.length_field_name
);
394 visit(*char_sequence
);
397 std::string _escaped_current_field_name
;
399 * Encoding to specify for the next serialized integer type.
400 * Since the integer_type does not allow an encoding to be specified (it is a TSDL-specific
401 * concept), this attribute is used when expressing static or dynamic length strings as
402 * arrays/sequences of bytes with an encoding.
404 nonstd::optional
<enum lst::string_type::encoding
> _current_integer_encoding_override
;
406 unsigned int _indentation_level
;
407 const lst::abi
& _trace_abi
;
409 std::queue
<std::string
> _type_suffixes
;
411 /* Description in TSDL format. */
412 std::string _description
;
416 tsdl::trace_class_visitor::trace_class_visitor(const lst::abi
& trace_abi
,
417 tsdl::append_metadata_fragment_function append_metadata_fragment
) :
418 _trace_abi
{trace_abi
}, _append_metadata_fragment(append_metadata_fragment
)
422 void tsdl::trace_class_visitor::append_metadata_fragment(const std::string
& fragment
) const
424 _append_metadata_fragment(fragment
);
427 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::trace_class
& trace_class
)
429 /* Declare type aliases, trace class, and packet header. */
430 auto trace_class_tsdl
= fmt::format(
431 "/* CTF {ctf_major}.{ctf_minor} */\n\n"
432 "typealias integer {{ size = 8; align = {uint8_t_alignment}; signed = false; }} := uint8_t;\n"
433 "typealias integer {{ size = 16; align = {uint16_t_alignment}; signed = false; }} := uint16_t;\n"
434 "typealias integer {{ size = 32; align = {uint32_t_alignment}; signed = false; }} := uint32_t;\n"
435 "typealias integer {{ size = 64; align = {uint64_t_alignment}; signed = false; }} := uint64_t;\n"
436 "typealias integer {{ size = {bits_per_long}; align = {long_alignment}; signed = false; }} := unsigned long;\n"
437 "typealias integer {{ size = 5; align = 1; signed = false; }} := uint5_t;\n"
438 "typealias integer {{ size = 27; align = 1; signed = false; }} := uint27_t;\n"
441 " major = {ctf_major};\n"
442 " minor = {ctf_minor};\n"
443 " uuid = \"{uuid}\";\n"
444 " byte_order = {byte_order};\n"
445 " packet.header := struct {{\n"
447 " uint8_t uuid[16];\n"
448 " uint32_t stream_id;\n"
449 " uint64_t stream_instance_id;\n"
452 fmt::arg("ctf_major", ctf_spec_major
),
453 fmt::arg("ctf_minor", ctf_spec_minor
),
454 fmt::arg("uint8_t_alignment", trace_class
.abi
.uint8_t_alignment
),
455 fmt::arg("uint16_t_alignment", trace_class
.abi
.uint16_t_alignment
),
456 fmt::arg("uint32_t_alignment", trace_class
.abi
.uint32_t_alignment
),
457 fmt::arg("uint64_t_alignment", trace_class
.abi
.uint64_t_alignment
),
458 fmt::arg("long_alignment", trace_class
.abi
.long_alignment
),
459 fmt::arg("long_size", trace_class
.abi
.long_alignment
),
460 fmt::arg("bits_per_long", trace_class
.abi
.bits_per_long
),
461 fmt::arg("uuid", lttng::utils::uuid_to_str(trace_class
.uuid
)),
462 fmt::arg("byte_order",
463 trace_class
.abi
.byte_order
== lst::byte_order::BIG_ENDIAN_
?
467 /* Declare trace scope and type aliases. */
468 append_metadata_fragment(std::move(trace_class_tsdl
));
471 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::clock_class
& clock_class
)
473 auto uuid_str
= clock_class
.uuid
?
474 fmt::format(" uuid = \"{}\";\n",
475 lttng::utils::uuid_to_str(*clock_class
.uuid
)) :
478 /* Assumes a single clock that maps to specific stream class fields/roles. */
479 auto clock_class_str
= fmt::format(
481 " name = \"{name}\";\n"
484 " description = \"{description}\";\n"
485 " freq = {frequency};\n"
486 " offset = {offset};\n"
489 "typealias integer {{\n"
490 " size = 27; align = 1; signed = false;\n"
491 " map = clock.{name}.value;\n"
492 "}} := uint27_clock_{name}_t;\n"
494 "typealias integer {{\n"
495 " size = 32; align = {uint32_t_alignment}; signed = false;\n"
496 " map = clock.{name}.value;\n"
497 "}} := uint32_clock_{name}_t;\n"
499 "typealias integer {{\n"
500 " size = 64; align = {uint64_t_alignment}; signed = false;\n"
501 " map = clock.{name}.value;\n"
502 "}} := uint64_clock_{name}_t;\n"
504 "struct packet_context {{\n"
505 " uint64_clock_{name}_t timestamp_begin;\n"
506 " uint64_clock_{name}_t timestamp_end;\n"
507 " uint64_t content_size;\n"
508 " uint64_t packet_size;\n"
509 " uint64_t packet_seq_num;\n"
510 " unsigned long events_discarded;\n"
511 " uint32_t cpu_id;\n"
514 "struct event_header_compact {{\n"
515 " enum : uint5_t {{ compact = 0 ... 30, extended = 31 }} id;\n"
518 " uint27_clock_{name}_t timestamp;\n"
522 " uint64_clock_{name}_t timestamp;\n"
525 "}} align({uint32_t_alignment});\n"
527 "struct event_header_large {{\n"
528 " enum : uint16_t {{ compact = 0 ... 65534, extended = 65535 }} id;\n"
531 " uint32_clock_{name}_t timestamp;\n"
535 " uint64_clock_{name}_t timestamp;\n"
538 "}} align({uint16_t_alignment});\n\n",
539 fmt::arg("name", clock_class
.name
),
540 fmt::arg("uuid", uuid_str
),
541 fmt::arg("description", clock_class
.description
),
542 fmt::arg("frequency", clock_class
.frequency
),
543 fmt::arg("offset", clock_class
.offset
),
544 fmt::arg("uint16_t_alignment", _trace_abi
.uint16_t_alignment
),
545 fmt::arg("uint32_t_alignment", _trace_abi
.uint32_t_alignment
),
546 fmt::arg("uint64_t_alignment", _trace_abi
.uint64_t_alignment
));
548 append_metadata_fragment(std::move(clock_class_str
));
551 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::stream_class
& stream_class
)
553 /* Declare stream. */
554 auto stream_class_str
= fmt::format("stream {{\n"
556 " event.header := {header_type};\n"
557 " packet.context := struct packet_context;\n",
558 fmt::arg("id", stream_class
.id
),
559 fmt::arg("header_type", stream_class
.header_type
== lst::stream_class::header_type::COMPACT
?
560 "struct event_header_compact" :
561 "struct event_header_large"));
563 auto context_field_visitor
= tsdl_field_visitor(_trace_abi
, 1);
565 stream_class
.get_context().accept(static_cast<lst::type_visitor
&>(context_field_visitor
));
567 stream_class_str
+= fmt::format(" event.context := {}\n}};\n\n",
568 context_field_visitor
.get_description());
570 append_metadata_fragment(stream_class_str
);
573 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::event_class
& event_class
)
575 auto event_class_str
= fmt::format("event {{\n"
576 " name = \"{name}\";\n"
578 " stream_id = {stream_class_id};\n"
579 " loglevel = {log_level};\n",
580 fmt::arg("name", event_class
.name
),
581 fmt::arg("id", event_class
.id
),
582 fmt::arg("stream_class_id", event_class
.stream_class_id
),
583 fmt::arg("log_level", event_class
.log_level
));
585 if (event_class
.model_emf_uri
) {
586 event_class_str
+= fmt::format(
587 " model.emf.uri = \"{}\";\n", *event_class
.model_emf_uri
);
590 auto payload_visitor
= tsdl_field_visitor(_trace_abi
, 1);
592 event_class
.payload
->accept(static_cast<lst::type_visitor
&>(payload_visitor
));
594 event_class_str
+= fmt::format(
595 " fields := {}\n}};\n\n", payload_visitor
.get_description());
597 append_metadata_fragment(event_class_str
);
600 void tsdl::trace_class_visitor::environment_begin()
602 _environment
+= "env {\n";
605 void tsdl::trace_class_visitor::visit(
606 const lttng::sessiond::trace::environment_field
<int64_t>& field
)
608 _environment
+= fmt::format(" {} = {};\n", field
.name
, field
.value
);
611 void tsdl::trace_class_visitor::visit(
612 const lttng::sessiond::trace::environment_field
<const char *>& field
)
614 _environment
+= fmt::format(
615 " {} = \"{}\";\n", field
.name
, escape_tsdl_env_string_value(field
.value
));
618 void tsdl::trace_class_visitor::environment_end()
620 _environment
+= "};\n\n";
621 append_metadata_fragment(_environment
);
622 _environment
.clear();