sessiond: transition from lttng-ust to tracer agnostic API
[lttng-tools.git] / src / bin / lttng-sessiond / tsdl-trace-class-visitor.cpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "clock-class.hpp"
9 #include "tsdl-trace-class-visitor.hpp"
10
11 #include <common/exception.hpp>
12 #include <common/format.hpp>
13 #include <common/make-unique.hpp>
14 #include <common/uuid.hpp>
15
16 #include <array>
17 #include <queue>
18 #include <locale>
19
20 namespace lst = lttng::sessiond::trace;
21 namespace tsdl = lttng::sessiond::tsdl;
22
23 namespace {
24 const auto ctf_spec_major = 1;
25 const auto ctf_spec_minor = 8;
26
27 /*
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.
31 *
32 * Changing this behaviour to a smarter algorithm would break readers that have
33 * come to expect this initial underscore.
34 */
35 std::string escape_tsdl_identifier(const std::string& original_identifier)
36 {
37 if (original_identifier.size() == 0) {
38 LTTNG_THROW_ERROR("Invalid 0-length identifier used in trace description");
39 }
40
41 std::string new_identifier;
42 /* Optimisticly assume most identifiers are valid and allocate the same length. */
43 new_identifier.reserve(original_identifier.size());
44 new_identifier = "_";
45
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 += '_';
51 } else {
52 new_identifier += current_char;
53 }
54 }
55
56 return new_identifier;
57 }
58
59 std::string escape_tsdl_env_string_value(const std::string& original_string)
60 {
61 std::string escaped_string;
62
63 escaped_string.reserve(original_string.size());
64
65 for (const auto c : original_string) {
66 switch (c) {
67 case '\n':
68 escaped_string += "\\n";
69 break;
70 case '\\':
71 escaped_string += "\\\\";
72 break;
73 case '"':
74 escaped_string += "\"";
75 break;
76 default:
77 escaped_string += c;
78 break;
79 }
80 }
81
82 return escaped_string;
83 }
84
85 class tsdl_field_visitor : public lttng::sessiond::trace::field_visitor,
86 public lttng::sessiond::trace::type_visitor {
87 public:
88 tsdl_field_visitor(const lst::abi& abi, unsigned int indentation_level) :
89 _indentation_level{indentation_level}, _trace_abi{abi}
90 {
91 }
92
93 std::string& get_description()
94 {
95 return _description;
96 }
97
98 private:
99 virtual void visit(const lst::field& field) override final
100 {
101 /*
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.
107 */
108 _escaped_current_field_name = escape_tsdl_identifier(field.name);
109
110 field._type->accept(*this);
111 _description += " ";
112 _description += _escaped_current_field_name;
113
114 /*
115 * Some types requires suffixes to be appended (e.g. the length of arrays
116 * and sequences, the mappings of enumerations).
117 */
118 while (!_type_suffixes.empty()) {
119 _description += _type_suffixes.front();
120 _type_suffixes.pop();
121 }
122
123 _description += ";";
124 _escaped_current_field_name.clear();
125 }
126
127 virtual void visit(const lst::integer_type& type) override final
128 {
129 _description += "integer { ";
130
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));
135
136 /* Defaults to unsigned. */
137 if (type.signedness == lst::integer_type::signedness::SIGNED) {
138 _description += " signed = true;";
139 }
140
141 /* Defaults to 10. */
142 if (type.base != lst::integer_type::base::DECIMAL) {
143 unsigned int base;
144
145 switch (type.base) {
146 case lst::integer_type::base::BINARY:
147 base = 2;
148 break;
149 case lst::integer_type::base::OCTAL:
150 base = 8;
151 break;
152 case lst::integer_type::base::HEXADECIMAL:
153 base = 16;
154 break;
155 default:
156 LTTNG_THROW_ERROR(fmt::format(
157 "Unexpected base encountered while serializing integer type to TSDL: base = {}",
158 (int) type.base));
159 }
160
161 _description += fmt::format(" base = {};", base);
162 }
163
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";
167
168 _description += fmt::format(" byte_order = {};", byte_order_str);
169 }
170
171 if (_current_integer_encoding_override) {
172 const char *encoding_str;
173
174 switch (*_current_integer_encoding_override) {
175 case lst::string_type::encoding::ASCII:
176 encoding_str = "ASCII";
177 break;
178 case lst::string_type::encoding::UTF8:
179 encoding_str = "UTF8";
180 break;
181 default:
182 LTTNG_THROW_ERROR(fmt::format(
183 "Unexpected encoding encountered while serializing integer type to TSDL: encoding = {}",
184 (int) *_current_integer_encoding_override));
185 }
186
187 _description += fmt::format(" encoding = {};", encoding_str);
188 _current_integer_encoding_override.reset();
189 }
190
191 _description += " }";
192 }
193
194 virtual void visit(const lst::floating_point_type& type) override final
195 {
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));
201
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";
205
206 _description += fmt::format(" byte_order = {};", byte_order_str);
207 }
208
209 _description += " }";
210 }
211
212 template <class EnumerationType>
213 void visit_enumeration(const EnumerationType& type)
214 {
215 /* name follows, when applicable. */
216 _description += "enum : ";
217
218 tsdl_field_visitor integer_visitor{_trace_abi, _indentation_level};
219
220 integer_visitor.visit(static_cast<const lst::integer_type&>(type));
221 _description += integer_visitor.get_description() + " {\n";
222
223 const auto mappings_indentation_level = _indentation_level + 1;
224
225 bool first_mapping = true;
226 for (const auto& mapping : *type._mappings) {
227 if (!first_mapping) {
228 _description += ",\n";
229 }
230
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));
239 } else {
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));
246 }
247
248 first_mapping = false;
249 }
250
251 _description += "\n";
252 _description.resize(_description.size() + _indentation_level, '\t');
253 _description += "}";
254 }
255
256 virtual void visit(const lst::signed_enumeration_type& type) override final
257 {
258 visit_enumeration(type);
259 }
260
261 virtual void visit(const lst::unsigned_enumeration_type& type) override final
262 {
263 visit_enumeration(type);
264 }
265
266 virtual void visit(const lst::static_length_array_type& type) override final
267 {
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');
275 }
276
277 type.element_type->accept(*this);
278 _type_suffixes.emplace(fmt::format("[{}]", type.length));
279 }
280
281 virtual void visit(const lst::dynamic_length_array_type& type) override final
282 {
283 if (type.alignment != 0) {
284 /*
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.
289 */
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');
296 }
297
298 type.element_type->accept(*this);
299 _type_suffixes.emplace(fmt::format(
300 "[{}]", escape_tsdl_identifier(type.length_field_name)));
301 }
302
303 virtual void visit(const lst::null_terminated_string_type& type) override final
304 {
305 /* Defaults to UTF-8. */
306 if (type.encoding == lst::null_terminated_string_type::encoding::ASCII) {
307 _description += "string { encoding = ASCII }";
308 } else {
309 _description += "string";
310 }
311 }
312
313 virtual void visit(const lst::structure_type& type) override final
314 {
315 _indentation_level++;
316 _description += "struct {";
317
318 for (const auto& field : type._fields) {
319 _description += "\n";
320 _description.resize(_description.size() + _indentation_level, '\t');
321 field->accept(*this);
322 }
323
324 _indentation_level--;
325 if (type._fields.size() != 0) {
326 _description += "\n";
327 _description.resize(_description.size() + _indentation_level, '\t');
328 }
329
330 _description += "};";
331 }
332
333 virtual void visit(const lst::variant_type& type) override final
334 {
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');
342 }
343
344 _indentation_level++;
345 _description += fmt::format("variant <{}> {\n", escape_tsdl_identifier(type.tag_name));
346
347 bool first_field = true;
348 for (const auto& field : type._choices) {
349 if (!first_field) {
350 _description += ",\n";
351 }
352
353 _description.resize(_description.size() + _indentation_level, '\t');
354 field->accept(*this);
355 first_field = false;
356 }
357
358 _description += "\n";
359 _description.resize(_description.size() + _indentation_level, '\t');
360 _description += "};";
361 _indentation_level--;
362 }
363
364 lst::type::cuptr create_character_type(enum lst::string_type::encoding encoding)
365 {
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);
370 }
371
372 virtual void visit(const lst::static_length_string_type& type) override final
373 {
374 /*
375 * TSDL expresses static-length strings as arrays of 8-bit integer with
376 * an encoding specified.
377 */
378 const auto char_array = lttng::make_unique<lst::static_length_array_type>(
379 type.alignment, create_character_type(type.encoding), type.length);
380
381 visit(*char_array);
382 }
383
384 virtual void visit(const lst::dynamic_length_string_type& type) override final
385 {
386 /*
387 * TSDL expresses dynamic-length strings as arrays of 8-bit integer with
388 * an encoding specified.
389 */
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);
393
394 visit(*char_sequence);
395 }
396
397 std::string _escaped_current_field_name;
398 /*
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.
403 */
404 nonstd::optional<enum lst::string_type::encoding> _current_integer_encoding_override;
405
406 unsigned int _indentation_level;
407 const lst::abi& _trace_abi;
408
409 std::queue<std::string> _type_suffixes;
410
411 /* Description in TSDL format. */
412 std::string _description;
413 };
414 } /* namespace */
415
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)
419 {
420 }
421
422 void tsdl::trace_class_visitor::append_metadata_fragment(const std::string& fragment) const
423 {
424 _append_metadata_fragment(fragment);
425 }
426
427 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::trace_class& trace_class)
428 {
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"
439 "\n"
440 "trace {{\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"
446 " uint32_t magic;\n"
447 " uint8_t uuid[16];\n"
448 " uint32_t stream_id;\n"
449 " uint64_t stream_instance_id;\n"
450 " }};\n"
451 "}};\n\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_ ?
464 "be" :
465 "le"));
466
467 /* Declare trace scope and type aliases. */
468 append_metadata_fragment(std::move(trace_class_tsdl));
469 }
470
471 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::clock_class& clock_class)
472 {
473 auto uuid_str = clock_class.uuid ?
474 fmt::format(" uuid = \"{}\";\n",
475 lttng::utils::uuid_to_str(*clock_class.uuid)) :
476 "";
477
478 /* Assumes a single clock that maps to specific stream class fields/roles. */
479 auto clock_class_str = fmt::format(
480 "clock {{\n"
481 " name = \"{name}\";\n"
482 /* Optional uuid. */
483 "{uuid}"
484 " description = \"{description}\";\n"
485 " freq = {frequency};\n"
486 " offset = {offset};\n"
487 "}};\n"
488 "\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"
493 "\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"
498 "\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"
503 "\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"
512 "}};\n"
513 "\n"
514 "struct event_header_compact {{\n"
515 " enum : uint5_t {{ compact = 0 ... 30, extended = 31 }} id;\n"
516 " variant <id> {{\n"
517 " struct {{\n"
518 " uint27_clock_{name}_t timestamp;\n"
519 " }} compact;\n"
520 " struct {{\n"
521 " uint32_t id;\n"
522 " uint64_clock_{name}_t timestamp;\n"
523 " }} extended;\n"
524 " }} v;\n"
525 "}} align({uint32_t_alignment});\n"
526 "\n"
527 "struct event_header_large {{\n"
528 " enum : uint16_t {{ compact = 0 ... 65534, extended = 65535 }} id;\n"
529 " variant <id> {{\n"
530 " struct {{\n"
531 " uint32_clock_{name}_t timestamp;\n"
532 " }} compact;\n"
533 " struct {{\n"
534 " uint32_t id;\n"
535 " uint64_clock_{name}_t timestamp;\n"
536 " }} extended;\n"
537 " }} v;\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));
547
548 append_metadata_fragment(std::move(clock_class_str));
549 }
550
551 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::stream_class& stream_class)
552 {
553 /* Declare stream. */
554 auto stream_class_str = fmt::format("stream {{\n"
555 " id = {id};\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"));
562
563 auto context_field_visitor = tsdl_field_visitor(_trace_abi, 1);
564
565 stream_class.get_context().accept(static_cast<lst::type_visitor&>(context_field_visitor));
566
567 stream_class_str += fmt::format(" event.context := {}\n}};\n\n",
568 context_field_visitor.get_description());
569
570 append_metadata_fragment(stream_class_str);
571 }
572
573 void tsdl::trace_class_visitor::visit(const lttng::sessiond::trace::event_class& event_class)
574 {
575 auto event_class_str = fmt::format("event {{\n"
576 " name = \"{name}\";\n"
577 " id = {id};\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));
584
585 if (event_class.model_emf_uri) {
586 event_class_str += fmt::format(
587 " model.emf.uri = \"{}\";\n", *event_class.model_emf_uri);
588 }
589
590 auto payload_visitor = tsdl_field_visitor(_trace_abi, 1);
591
592 event_class.payload->accept(static_cast<lst::type_visitor&>(payload_visitor));
593
594 event_class_str += fmt::format(
595 " fields := {}\n}};\n\n", payload_visitor.get_description());
596
597 append_metadata_fragment(event_class_str);
598 }
599
600 void tsdl::trace_class_visitor::environment_begin()
601 {
602 _environment += "env {\n";
603 }
604
605 void tsdl::trace_class_visitor::visit(
606 const lttng::sessiond::trace::environment_field<int64_t>& field)
607 {
608 _environment += fmt::format(" {} = {};\n", field.name, field.value);
609 }
610
611 void tsdl::trace_class_visitor::visit(
612 const lttng::sessiond::trace::environment_field<const char *>& field)
613 {
614 _environment += fmt::format(
615 " {} = \"{}\";\n", field.name, escape_tsdl_env_string_value(field.value));
616 }
617
618 void tsdl::trace_class_visitor::environment_end()
619 {
620 _environment += "};\n\n";
621 append_metadata_fragment(_environment);
622 _environment.clear();
623 }
This page took 0.04625 seconds and 4 git commands to generate.