Change backing type of lttng_uuid to std::array
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.cpp
1 /*
2 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <stdint.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <limits.h>
14 #include <unistd.h>
15 #include <inttypes.h>
16 #include <common/common.hpp>
17 #include <common/time.hpp>
18 #include <vector>
19
20 #include "ust-registry.hpp"
21 #include "ust-clock.hpp"
22 #include "ust-app.hpp"
23
24 #define NR_CLOCK_OFFSET_SAMPLES 10
25
26 namespace {
27 struct offset_sample {
28 /* correlation offset */
29 int64_t offset;
30 /* lower is better */
31 uint64_t measure_delta;
32 };
33 } /* namespace */
34
35 static
36 int _lttng_field_statedump(ust_registry_session *session,
37 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
38 size_t *iter_field, size_t nesting);
39
40 static inline
41 int get_count_order(unsigned int count)
42 {
43 int order;
44
45 order = lttng_fls(count) - 1;
46 if (count & (count - 1)) {
47 order++;
48 }
49 LTTNG_ASSERT(order >= 0);
50 return order;
51 }
52
53 /*
54 * Returns offset where to write in metadata array, or negative error value on error.
55 */
56 static
57 ssize_t metadata_reserve(ust_registry_session *session, size_t len)
58 {
59 size_t new_len = session->_metadata_len + len;
60 size_t new_alloc_len = new_len;
61 size_t old_alloc_len = session->_metadata_alloc_len;
62 ssize_t ret;
63
64 if (new_alloc_len > (UINT32_MAX >> 1))
65 return -EINVAL;
66 if ((old_alloc_len << 1) > (UINT32_MAX >> 1))
67 return -EINVAL;
68
69 if (new_alloc_len > old_alloc_len) {
70 char *newptr;
71
72 new_alloc_len =
73 std::max<size_t>(1U << get_count_order(new_alloc_len), old_alloc_len << 1);
74 newptr = (char *) realloc(session->_metadata, new_alloc_len);
75 if (!newptr)
76 return -ENOMEM;
77 session->_metadata = newptr;
78 /* We zero directly the memory from start of allocation. */
79 memset(&session->_metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
80 session->_metadata_alloc_len = new_alloc_len;
81 }
82 ret = session->_metadata_len;
83 session->_metadata_len += len;
84 return ret;
85 }
86
87 static
88 int metadata_file_append(ust_registry_session *session,
89 const char *str, size_t len)
90 {
91 ssize_t written;
92
93 if (session->_metadata_fd < 0) {
94 return 0;
95 }
96 /* Write to metadata file */
97 written = lttng_write(session->_metadata_fd, str, len);
98 if (written != len) {
99 return -1;
100 }
101 return 0;
102 }
103
104 /*
105 * We have exclusive access to our metadata buffer (protected by the
106 * ust_lock), so we can do racy operations such as looking for
107 * remaining space left in packet and write, since mutual exclusion
108 * protects us from concurrent writes.
109 */
110 static ATTR_FORMAT_PRINTF(2, 3)
111 int lttng_metadata_printf(ust_registry_session *session,
112 const char *fmt, ...)
113 {
114 char *str = NULL;
115 size_t len;
116 va_list ap;
117 ssize_t offset;
118 int ret;
119
120 va_start(ap, fmt);
121 ret = vasprintf(&str, fmt, ap);
122 va_end(ap);
123 if (ret < 0)
124 return -ENOMEM;
125
126 len = strlen(str);
127 offset = metadata_reserve(session, len);
128 if (offset < 0) {
129 ret = offset;
130 goto end;
131 }
132 memcpy(&session->_metadata[offset], str, len);
133 ret = metadata_file_append(session, str, len);
134 if (ret) {
135 PERROR("Error appending to metadata file");
136 goto end;
137 }
138 DBG3("Append to metadata: \"%s\"", str);
139 ret = 0;
140
141 end:
142 free(str);
143 return ret;
144 }
145
146 static
147 int print_tabs(ust_registry_session *session, size_t nesting)
148 {
149 size_t i;
150
151 for (i = 0; i < nesting; i++) {
152 int ret;
153
154 ret = lttng_metadata_printf(session, " ");
155 if (ret) {
156 return ret;
157 }
158 }
159 return 0;
160 }
161
162 static
163 void sanitize_ctf_identifier(char *out, const char *in)
164 {
165 size_t i;
166
167 for (i = 0; i < LTTNG_UST_ABI_SYM_NAME_LEN; i++) {
168 switch (in[i]) {
169 case '.':
170 case '$':
171 case ':':
172 out[i] = '_';
173 break;
174 default:
175 out[i] = in[i];
176 }
177 }
178 }
179
180 static
181 int print_escaped_ctf_string(ust_registry_session *session, const char *string)
182 {
183 int ret = 0;
184 size_t i;
185 char cur;
186
187 i = 0;
188 cur = string[i];
189 while (cur != '\0') {
190 switch (cur) {
191 case '\n':
192 ret = lttng_metadata_printf(session, "%s", "\\n");
193 break;
194 case '\\':
195 case '"':
196 ret = lttng_metadata_printf(session, "%c", '\\');
197 if (ret) {
198 goto error;
199 }
200 /* We still print the current char */
201 /* Fallthrough */
202 default:
203 ret = lttng_metadata_printf(session, "%c", cur);
204 break;
205 }
206
207 if (ret) {
208 goto error;
209 }
210
211 cur = string[++i];
212 }
213 error:
214 return ret;
215 }
216
217 /* Called with session registry mutex held. */
218 static
219 int ust_metadata_enum_statedump(ust_registry_session *session,
220 const char *enum_name,
221 uint64_t enum_id,
222 const struct lttng_ust_ctl_integer_type *container_type,
223 const char *field_name, size_t *iter_field, size_t nesting)
224 {
225 struct ust_registry_enum *reg_enum;
226 const struct lttng_ust_ctl_enum_entry *entries;
227 size_t nr_entries;
228 int ret = 0;
229 size_t i;
230 char identifier[LTTNG_UST_ABI_SYM_NAME_LEN];
231
232 rcu_read_lock();
233 reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id);
234 rcu_read_unlock();
235 /* reg_enum can still be used because session registry mutex is held. */
236 if (!reg_enum) {
237 ret = -ENOENT;
238 goto end;
239 }
240 entries = reg_enum->entries;
241 nr_entries = reg_enum->nr_entries;
242
243 ret = print_tabs(session, nesting);
244 if (ret) {
245 goto end;
246 }
247 ret = lttng_metadata_printf(session,
248 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
249 container_type->size,
250 container_type->alignment,
251 container_type->signedness,
252 (container_type->encoding == lttng_ust_ctl_encode_none)
253 ? "none"
254 : (container_type->encoding == lttng_ust_ctl_encode_UTF8)
255 ? "UTF8"
256 : "ASCII",
257 container_type->base);
258 if (ret) {
259 goto end;
260 }
261 nesting++;
262 /* Dump all entries */
263 for (i = 0; i < nr_entries; i++) {
264 const struct lttng_ust_ctl_enum_entry *entry = &entries[i];
265 int j, len;
266
267 ret = print_tabs(session, nesting);
268 if (ret) {
269 goto end;
270 }
271 ret = lttng_metadata_printf(session,
272 "\"");
273 if (ret) {
274 goto end;
275 }
276 len = strlen(entry->string);
277 /* Escape the character '"' */
278 for (j = 0; j < len; j++) {
279 char c = entry->string[j];
280
281 switch (c) {
282 case '"':
283 ret = lttng_metadata_printf(session,
284 "\\\"");
285 break;
286 case '\\':
287 ret = lttng_metadata_printf(session,
288 "\\\\");
289 break;
290 default:
291 ret = lttng_metadata_printf(session,
292 "%c", c);
293 break;
294 }
295 if (ret) {
296 goto end;
297 }
298 }
299 ret = lttng_metadata_printf(session, "\"");
300 if (ret) {
301 goto end;
302 }
303
304 if (entry->u.extra.options &
305 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
306 ret = lttng_metadata_printf(session, ",\n");
307 if (ret) {
308 goto end;
309 }
310 } else {
311 ret = lttng_metadata_printf(session,
312 " = ");
313 if (ret) {
314 goto end;
315 }
316
317 if (entry->start.signedness) {
318 ret = lttng_metadata_printf(session,
319 "%" PRId64, entry->start.value);
320 } else {
321 ret = lttng_metadata_printf(session,
322 "%" PRIu64, entry->start.value);
323 }
324 if (ret) {
325 goto end;
326 }
327
328 if (entry->start.signedness == entry->end.signedness &&
329 entry->start.value ==
330 entry->end.value) {
331 ret = lttng_metadata_printf(session, ",\n");
332 } else {
333 if (entry->end.signedness) {
334 ret = lttng_metadata_printf(session,
335 " ... %" PRId64 ",\n",
336 entry->end.value);
337 } else {
338 ret = lttng_metadata_printf(session,
339 " ... %" PRIu64 ",\n",
340 entry->end.value);
341 }
342 }
343 if (ret) {
344 goto end;
345 }
346 }
347 }
348 nesting--;
349 sanitize_ctf_identifier(identifier, field_name);
350 ret = print_tabs(session, nesting);
351 if (ret) {
352 goto end;
353 }
354 ret = lttng_metadata_printf(session, "} _%s;\n",
355 identifier);
356 end:
357 (*iter_field)++;
358 return ret;
359 }
360
361 static
362 int _lttng_variant_statedump(ust_registry_session *session,
363 uint32_t nr_choices, const char *tag_name,
364 uint32_t alignment,
365 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
366 size_t *iter_field, size_t nesting)
367 {
368 const struct lttng_ust_ctl_field *variant = &fields[*iter_field];
369 uint32_t i;
370 int ret;
371 char identifier[LTTNG_UST_ABI_SYM_NAME_LEN];
372
373 (*iter_field)++;
374 sanitize_ctf_identifier(identifier, tag_name);
375 if (alignment) {
376 ret = print_tabs(session, nesting);
377 if (ret) {
378 goto end;
379 }
380 ret = lttng_metadata_printf(session,
381 "struct { } align(%u) _%s_padding;\n",
382 alignment * CHAR_BIT,
383 variant->name);
384 if (ret) {
385 goto end;
386 }
387 }
388 ret = print_tabs(session, nesting);
389 if (ret) {
390 goto end;
391 }
392 ret = lttng_metadata_printf(session,
393 "variant <_%s> {\n",
394 identifier);
395 if (ret) {
396 goto end;
397 }
398
399 for (i = 0; i < nr_choices; i++) {
400 if (*iter_field >= nr_fields) {
401 ret = -EOVERFLOW;
402 goto end;
403 }
404 ret = _lttng_field_statedump(session,
405 fields, nr_fields,
406 iter_field, nesting + 1);
407 if (ret) {
408 goto end;
409 }
410 }
411 sanitize_ctf_identifier(identifier, variant->name);
412 ret = print_tabs(session, nesting);
413 if (ret) {
414 goto end;
415 }
416 ret = lttng_metadata_printf(session,
417 "} _%s;\n",
418 identifier);
419 if (ret) {
420 goto end;
421 }
422 end:
423 return ret;
424 }
425
426 static
427 int _lttng_field_statedump(ust_registry_session *session,
428 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
429 size_t *iter_field, size_t nesting)
430 {
431 int ret = 0;
432 const char *bo_be = " byte_order = be;";
433 const char *bo_le = " byte_order = le;";
434 const char *bo_native = "";
435 const char *bo_reverse;
436 const struct lttng_ust_ctl_field *field;
437
438 if (*iter_field >= nr_fields) {
439 ret = -EOVERFLOW;
440 goto end;
441 }
442 field = &fields[*iter_field];
443
444 if (session->_byte_order == BIG_ENDIAN) {
445 bo_reverse = bo_le;
446 } else {
447 bo_reverse = bo_be;
448 }
449
450 switch (field->type.atype) {
451 case lttng_ust_ctl_atype_integer:
452 ret = print_tabs(session, nesting);
453 if (ret) {
454 goto end;
455 }
456 ret = lttng_metadata_printf(session,
457 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
458 field->type.u.integer.size,
459 field->type.u.integer.alignment,
460 field->type.u.integer.signedness,
461 (field->type.u.integer.encoding == lttng_ust_ctl_encode_none)
462 ? "none"
463 : (field->type.u.integer.encoding == lttng_ust_ctl_encode_UTF8)
464 ? "UTF8"
465 : "ASCII",
466 field->type.u.integer.base,
467 field->type.u.integer.reverse_byte_order ? bo_reverse : bo_native,
468 field->name);
469 (*iter_field)++;
470 break;
471 case lttng_ust_ctl_atype_enum:
472 ret = ust_metadata_enum_statedump(session,
473 field->type.u.legacy.basic.enumeration.name,
474 field->type.u.legacy.basic.enumeration.id,
475 &field->type.u.legacy.basic.enumeration.container_type,
476 field->name, iter_field, nesting);
477 break;
478 case lttng_ust_ctl_atype_float:
479 ret = print_tabs(session, nesting);
480 if (ret) {
481 goto end;
482 }
483 ret = lttng_metadata_printf(session,
484 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
485 field->type.u._float.exp_dig,
486 field->type.u._float.mant_dig,
487 field->type.u._float.alignment,
488 field->type.u._float.reverse_byte_order ? bo_reverse : bo_native,
489 field->name);
490 (*iter_field)++;
491 break;
492 case lttng_ust_ctl_atype_array:
493 {
494 const struct lttng_ust_ctl_basic_type *elem_type;
495
496 ret = print_tabs(session, nesting);
497 if (ret) {
498 goto end;
499 }
500 elem_type = &field->type.u.legacy.array.elem_type;
501 /* Only integers are currently supported in arrays. */
502 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
503 ret = -EINVAL;
504 goto end;
505 }
506 ret = lttng_metadata_printf(session,
507 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
508 elem_type->u.basic.integer.size,
509 elem_type->u.basic.integer.alignment,
510 elem_type->u.basic.integer.signedness,
511 (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
512 ? "none"
513 : (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
514 ? "UTF8"
515 : "ASCII",
516 elem_type->u.basic.integer.base,
517 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
518 field->name, field->type.u.legacy.array.length);
519 (*iter_field)++;
520 break;
521 }
522 case lttng_ust_ctl_atype_array_nestable:
523 {
524 uint32_t array_length;
525 const struct lttng_ust_ctl_field *array_nestable;
526 const struct lttng_ust_ctl_type *elem_type;
527
528 array_length = field->type.u.array_nestable.length;
529 (*iter_field)++;
530
531 if (*iter_field >= nr_fields) {
532 ret = -EOVERFLOW;
533 goto end;
534 }
535 array_nestable = &fields[*iter_field];
536 elem_type = &array_nestable->type;
537
538 /* Only integers are currently supported in arrays. */
539 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
540 ret = -EINVAL;
541 goto end;
542 }
543
544 if (field->type.u.array_nestable.alignment) {
545 ret = print_tabs(session, nesting);
546 if (ret) {
547 goto end;
548 }
549 ret = lttng_metadata_printf(session,
550 "struct { } align(%u) _%s_padding;\n",
551 field->type.u.array_nestable.alignment * CHAR_BIT,
552 field->name);
553 if (ret) {
554 goto end;
555 }
556 }
557
558 ret = print_tabs(session, nesting);
559 if (ret) {
560 goto end;
561 }
562 ret = lttng_metadata_printf(session,
563 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
564 elem_type->u.integer.size,
565 elem_type->u.integer.alignment,
566 elem_type->u.integer.signedness,
567 (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none)
568 ? "none"
569 : (elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8)
570 ? "UTF8"
571 : "ASCII",
572 elem_type->u.integer.base,
573 elem_type->u.integer.reverse_byte_order ? bo_reverse : bo_native,
574 field->name, array_length);
575 (*iter_field)++;
576 break;
577 }
578 case lttng_ust_ctl_atype_sequence:
579 {
580 const struct lttng_ust_ctl_basic_type *elem_type;
581 const struct lttng_ust_ctl_basic_type *length_type;
582
583 elem_type = &field->type.u.legacy.sequence.elem_type;
584 length_type = &field->type.u.legacy.sequence.length_type;
585 ret = print_tabs(session, nesting);
586 if (ret) {
587 goto end;
588 }
589
590 /* Only integers are currently supported in sequences. */
591 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
592 ret = -EINVAL;
593 goto end;
594 }
595
596 ret = lttng_metadata_printf(session,
597 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
598 length_type->u.basic.integer.size,
599 (unsigned int) length_type->u.basic.integer.alignment,
600 length_type->u.basic.integer.signedness,
601 (length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
602 ? "none"
603 : ((length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
604 ? "UTF8"
605 : "ASCII"),
606 length_type->u.basic.integer.base,
607 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
608 field->name);
609 if (ret) {
610 goto end;
611 }
612
613 ret = print_tabs(session, nesting);
614 if (ret) {
615 goto end;
616 }
617 ret = lttng_metadata_printf(session,
618 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
619 elem_type->u.basic.integer.size,
620 (unsigned int) elem_type->u.basic.integer.alignment,
621 elem_type->u.basic.integer.signedness,
622 (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
623 ? "none"
624 : ((elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
625 ? "UTF8"
626 : "ASCII"),
627 elem_type->u.basic.integer.base,
628 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
629 field->name,
630 field->name);
631 (*iter_field)++;
632 break;
633 }
634 case lttng_ust_ctl_atype_sequence_nestable:
635 {
636 const struct lttng_ust_ctl_field *sequence_nestable;
637 const struct lttng_ust_ctl_type *elem_type;
638
639 (*iter_field)++;
640 if (*iter_field >= nr_fields) {
641 ret = -EOVERFLOW;
642 goto end;
643 }
644 sequence_nestable = &fields[*iter_field];
645 elem_type = &sequence_nestable->type;
646
647 /* Only integers are currently supported in sequences. */
648 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
649 ret = -EINVAL;
650 goto end;
651 }
652
653 if (field->type.u.sequence_nestable.alignment) {
654 ret = print_tabs(session, nesting);
655 if (ret) {
656 goto end;
657 }
658 ret = lttng_metadata_printf(session,
659 "struct { } align(%u) _%s_padding;\n",
660 field->type.u.sequence_nestable.alignment * CHAR_BIT,
661 field->name);
662 if (ret) {
663 goto end;
664 }
665 }
666
667 ret = print_tabs(session, nesting);
668 if (ret) {
669 goto end;
670 }
671 ret = lttng_metadata_printf(session,
672 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ _%s ];\n",
673 elem_type->u.integer.size,
674 (unsigned int) elem_type->u.integer.alignment,
675 elem_type->u.integer.signedness,
676 (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none)
677 ? "none"
678 : ((elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8)
679 ? "UTF8"
680 : "ASCII"),
681 elem_type->u.integer.base,
682 elem_type->u.integer.reverse_byte_order ? bo_reverse : bo_native,
683 field->name,
684 field->type.u.sequence_nestable.length_name);
685 (*iter_field)++;
686 break;
687 }
688 case lttng_ust_ctl_atype_string:
689 /* Default encoding is UTF8 */
690 ret = print_tabs(session, nesting);
691 if (ret) {
692 goto end;
693 }
694 ret = lttng_metadata_printf(session,
695 "string%s _%s;\n",
696 field->type.u.string.encoding == lttng_ust_ctl_encode_ASCII ?
697 " { encoding = ASCII; }" : "",
698 field->name);
699 (*iter_field)++;
700 break;
701 case lttng_ust_ctl_atype_variant:
702 ret = _lttng_variant_statedump(session,
703 field->type.u.legacy.variant.nr_choices,
704 field->type.u.legacy.variant.tag_name,
705 0,
706 fields, nr_fields, iter_field, nesting);
707 if (ret) {
708 goto end;
709 }
710 break;
711 case lttng_ust_ctl_atype_variant_nestable:
712 ret = _lttng_variant_statedump(session,
713 field->type.u.variant_nestable.nr_choices,
714 field->type.u.variant_nestable.tag_name,
715 field->type.u.variant_nestable.alignment,
716 fields, nr_fields, iter_field, nesting);
717 if (ret) {
718 goto end;
719 }
720 break;
721 case lttng_ust_ctl_atype_struct:
722 if (field->type.u.legacy._struct.nr_fields != 0) {
723 /* Currently only 0-length structures are supported. */
724 ret = -EINVAL;
725 goto end;
726 }
727 ret = print_tabs(session, nesting);
728 if (ret) {
729 goto end;
730 }
731 ret = lttng_metadata_printf(session,
732 "struct {} _%s;\n",
733 field->name);
734 (*iter_field)++;
735 break;
736 case lttng_ust_ctl_atype_struct_nestable:
737 if (field->type.u.struct_nestable.nr_fields != 0) {
738 /* Currently only 0-length structures are supported. */
739 ret = -EINVAL;
740 goto end;
741 }
742 ret = print_tabs(session, nesting);
743 if (ret) {
744 goto end;
745 }
746 if (field->type.u.struct_nestable.alignment) {
747 ret = lttng_metadata_printf(session,
748 "struct {} align(%u) _%s;\n",
749 field->type.u.struct_nestable.alignment * CHAR_BIT,
750 field->name);
751 if (ret) {
752 goto end;
753 }
754 } else {
755 ret = lttng_metadata_printf(session,
756 "struct {} _%s;\n",
757 field->name);
758 }
759 (*iter_field)++;
760 break;
761 case lttng_ust_ctl_atype_enum_nestable:
762 {
763 const struct lttng_ust_ctl_field *container_field;
764 const struct lttng_ust_ctl_type *container_type;
765
766 (*iter_field)++;
767 if (*iter_field >= nr_fields) {
768 ret = -EOVERFLOW;
769 goto end;
770 }
771 container_field = &fields[*iter_field];
772 container_type = &container_field->type;
773
774 /* Only integers are supported as container types. */
775 if (container_type->atype != lttng_ust_ctl_atype_integer) {
776 ret = -EINVAL;
777 goto end;
778 }
779 ret = ust_metadata_enum_statedump(session,
780 field->type.u.enum_nestable.name,
781 field->type.u.enum_nestable.id,
782 &container_type->u.integer,
783 field->name, iter_field, nesting);
784 break;
785 }
786 default:
787 ret = -EINVAL;
788 }
789 end:
790 return ret;
791 }
792
793 static
794 int _lttng_context_metadata_statedump(ust_registry_session *session,
795 size_t nr_ctx_fields,
796 struct lttng_ust_ctl_field *ctx)
797 {
798 int ret = 0;
799 size_t i = 0;
800
801 if (!ctx)
802 return 0;
803 for (;;) {
804 if (i >= nr_ctx_fields) {
805 break;
806 }
807 ret = _lttng_field_statedump(session, ctx,
808 nr_ctx_fields, &i, 2);
809 if (ret) {
810 break;
811 }
812 }
813 return ret;
814 }
815
816 static
817 int _lttng_fields_metadata_statedump(ust_registry_session *session,
818 struct ust_registry_event *event)
819 {
820 int ret = 0;
821 size_t i = 0;
822
823 for (;;) {
824 if (i >= event->nr_fields) {
825 break;
826 }
827 ret = _lttng_field_statedump(session, event->fields,
828 event->nr_fields, &i, 2);
829 if (ret) {
830 break;
831 }
832 }
833 return ret;
834 }
835
836 /*
837 * Should be called with session registry mutex held.
838 */
839 int ust_metadata_event_statedump(ust_registry_session *session,
840 struct ust_registry_channel *chan,
841 struct ust_registry_event *event)
842 {
843 int ret = 0;
844
845 /* Don't dump metadata events */
846 if (chan->chan_id == -1U)
847 return 0;
848
849 /*
850 * We don't want to output an event's metadata before its parent
851 * stream's metadata. If the stream's metadata hasn't been output yet,
852 * skip this event. Its metadata will be output when we output the
853 * stream's metadata.
854 */
855 if (!chan->metadata_dumped || event->metadata_dumped) {
856 return 0;
857 }
858
859 ret = lttng_metadata_printf(session,
860 "event {\n"
861 " name = \"%s\";\n"
862 " id = %u;\n"
863 " stream_id = %u;\n",
864 event->name,
865 event->id,
866 chan->chan_id);
867 if (ret) {
868 goto end;
869 }
870
871 ret = lttng_metadata_printf(session,
872 " loglevel = %d;\n",
873 event->loglevel_value);
874 if (ret) {
875 goto end;
876 }
877
878 if (event->model_emf_uri) {
879 ret = lttng_metadata_printf(session,
880 " model.emf.uri = \"%s\";\n",
881 event->model_emf_uri);
882 if (ret) {
883 goto end;
884 }
885 }
886
887 ret = lttng_metadata_printf(session,
888 " fields := struct {\n"
889 );
890 if (ret) {
891 goto end;
892 }
893
894 ret = _lttng_fields_metadata_statedump(session, event);
895 if (ret) {
896 goto end;
897 }
898
899 ret = lttng_metadata_printf(session,
900 " };\n"
901 "};\n\n");
902 if (ret) {
903 goto end;
904 }
905 event->metadata_dumped = 1;
906
907 end:
908 return ret;
909 }
910
911 /*
912 * Should be called with session registry mutex held.
913 *
914 * RCU read lock must be held by the caller.
915 */
916 int ust_metadata_channel_statedump(ust_registry_session *session,
917 struct ust_registry_channel *chan)
918 {
919 int ret;
920
921 ASSERT_RCU_READ_LOCKED();
922
923 /* Don't dump metadata events */
924 if (chan->chan_id == -1U)
925 return 0;
926
927 if (!chan->header_type)
928 return -EINVAL;
929
930 ret = lttng_metadata_printf(session,
931 "stream {\n"
932 " id = %u;\n"
933 " event.header := %s;\n"
934 " packet.context := struct packet_context;\n",
935 chan->chan_id,
936 chan->header_type == LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT ?
937 "struct event_header_compact" :
938 "struct event_header_large");
939 if (ret) {
940 return ret;
941 }
942
943 if (chan->ctx_fields) {
944 ret = lttng_metadata_printf(session,
945 " event.context := struct {\n");
946 if (ret) {
947 return ret;
948 }
949 }
950 ret = _lttng_context_metadata_statedump(session,
951 chan->nr_ctx_fields,
952 chan->ctx_fields);
953 if (ret) {
954 return ret;
955 }
956 if (chan->ctx_fields) {
957 ret = lttng_metadata_printf(session,
958 " };\n");
959 if (ret) {
960 return ret;
961 }
962 }
963
964 ret = lttng_metadata_printf(session,
965 "};\n\n");
966 if (ret) {
967 return ret;
968 }
969
970 /* Flag success of metadata dump. */
971 chan->metadata_dumped = 1;
972
973 /*
974 * Output the metadata of any existing event.
975 *
976 * Sort the events by id. This is not necessary, but it's nice to have
977 * a more predictable order in the metadata file.
978 */
979 std::vector<ust_registry_event *> events;
980 {
981 cds_lfht_iter event_iter;
982 ust_registry_event *event;
983 cds_lfht_for_each_entry(chan->events->ht, &event_iter, event,
984 node.node) {
985 events.push_back(event);
986 }
987 }
988
989 std::sort(events.begin(), events.end(),
990 [] (ust_registry_event *a, ust_registry_event *b) {
991 return a->id < b->id;
992 });
993
994 for (ust_registry_event *event : events) {
995 ust_metadata_event_statedump(session, chan, event);
996 }
997
998 return 0;
999 }
1000
1001 static
1002 int _lttng_stream_packet_context_declare(ust_registry_session *session)
1003 {
1004 return lttng_metadata_printf(session,
1005 "struct packet_context {\n"
1006 " uint64_clock_monotonic_t timestamp_begin;\n"
1007 " uint64_clock_monotonic_t timestamp_end;\n"
1008 " uint64_t content_size;\n"
1009 " uint64_t packet_size;\n"
1010 " uint64_t packet_seq_num;\n"
1011 " unsigned long events_discarded;\n"
1012 " uint32_t cpu_id;\n"
1013 "};\n\n"
1014 );
1015 }
1016
1017 /*
1018 * Compact header:
1019 * id: range: 0 - 30.
1020 * id 31 is reserved to indicate an extended header.
1021 *
1022 * Large header:
1023 * id: range: 0 - 65534.
1024 * id 65535 is reserved to indicate an extended header.
1025 */
1026 static
1027 int _lttng_event_header_declare(ust_registry_session *session)
1028 {
1029 return lttng_metadata_printf(session,
1030 "struct event_header_compact {\n"
1031 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1032 " variant <id> {\n"
1033 " struct {\n"
1034 " uint27_clock_monotonic_t timestamp;\n"
1035 " } compact;\n"
1036 " struct {\n"
1037 " uint32_t id;\n"
1038 " uint64_clock_monotonic_t timestamp;\n"
1039 " } extended;\n"
1040 " } v;\n"
1041 "} align(%u);\n"
1042 "\n"
1043 "struct event_header_large {\n"
1044 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1045 " variant <id> {\n"
1046 " struct {\n"
1047 " uint32_clock_monotonic_t timestamp;\n"
1048 " } compact;\n"
1049 " struct {\n"
1050 " uint32_t id;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1052 " } extended;\n"
1053 " } v;\n"
1054 "} align(%u);\n\n",
1055 session->_uint32_t_alignment,
1056 session->_uint16_t_alignment
1057 );
1058 }
1059
1060 /*
1061 * The offset between monotonic and realtime clock can be negative if
1062 * the system sets the REALTIME clock to 0 after boot.
1063 */
1064 static
1065 int measure_single_clock_offset(struct offset_sample *sample)
1066 {
1067 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
1068 uint64_t tcf = trace_clock_freq();
1069 struct timespec rts = { 0, 0 };
1070 int ret;
1071
1072 monotonic[0] = trace_clock_read64();
1073 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
1074 if (ret < 0) {
1075 return ret;
1076 }
1077 monotonic[1] = trace_clock_read64();
1078 measure_delta = monotonic[1] - monotonic[0];
1079 if (measure_delta > sample->measure_delta) {
1080 /*
1081 * Discard value if it took longer to read than the best
1082 * sample so far.
1083 */
1084 return 0;
1085 }
1086 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
1087 realtime = (uint64_t) rts.tv_sec * tcf;
1088 if (tcf == NSEC_PER_SEC) {
1089 realtime += rts.tv_nsec;
1090 } else {
1091 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
1092 }
1093 sample->offset = (int64_t) realtime - monotonic_avg;
1094 sample->measure_delta = measure_delta;
1095 return 0;
1096 }
1097
1098 /*
1099 * Approximation of NTP time of day to clock monotonic correlation,
1100 * taken at start of trace. Keep the measurement that took the less time
1101 * to complete, thus removing imprecision caused by preemption.
1102 * May return a negative offset.
1103 */
1104 static
1105 int64_t measure_clock_offset(void)
1106 {
1107 int i;
1108 struct offset_sample offset_best_sample = {
1109 .offset = 0,
1110 .measure_delta = UINT64_MAX,
1111 };
1112
1113 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
1114 if (measure_single_clock_offset(&offset_best_sample)) {
1115 return 0;
1116 }
1117 }
1118 return offset_best_sample.offset;
1119 }
1120
1121 static
1122 int print_metadata_session_information(ust_registry_session *registry)
1123 {
1124 int ret;
1125 struct ltt_session *session = NULL;
1126 char creation_datetime[ISO8601_STR_LEN];
1127
1128 rcu_read_lock();
1129 session = session_find_by_id(registry->_tracing_id);
1130 if (!session) {
1131 ret = -1;
1132 goto error;
1133 }
1134
1135 /* Print the trace name */
1136 ret = lttng_metadata_printf(registry, " trace_name = \"");
1137 if (ret) {
1138 goto error;
1139 }
1140
1141 /*
1142 * This is necessary since the creation time is present in the session
1143 * name when it is generated.
1144 */
1145 if (session->has_auto_generated_name) {
1146 ret = print_escaped_ctf_string(registry, DEFAULT_SESSION_NAME);
1147 } else {
1148 ret = print_escaped_ctf_string(registry, session->name);
1149 }
1150 if (ret) {
1151 goto error;
1152 }
1153
1154 ret = lttng_metadata_printf(registry, "\";\n");
1155 if (ret) {
1156 goto error;
1157 }
1158
1159 /* Prepare creation time */
1160 ret = time_to_iso8601_str(session->creation_time, creation_datetime,
1161 sizeof(creation_datetime));
1162 if (ret) {
1163 goto error;
1164 }
1165
1166 /* Output the reste of the information */
1167 ret = lttng_metadata_printf(registry,
1168 " trace_creation_datetime = \"%s\";\n"
1169 " hostname = \"%s\";\n",
1170 creation_datetime, session->hostname);
1171 if (ret) {
1172 goto error;
1173 }
1174
1175 error:
1176 if (session) {
1177 session_put(session);
1178 }
1179 rcu_read_unlock();
1180 return ret;
1181 }
1182
1183 static
1184 int print_metadata_app_information(ust_registry_session *registry)
1185 {
1186 if (registry->get_buffering_scheme() != LTTNG_BUFFER_PER_PID) {
1187 return 0;
1188 }
1189
1190 const auto *per_pid_session = static_cast<const ust_registry_session_per_pid *>(registry);
1191
1192 char datetime[ISO8601_STR_LEN];
1193 int ret = time_to_iso8601_str(
1194 per_pid_session->_app_creation_time, datetime, sizeof(datetime));
1195 if (ret) {
1196 return ret;
1197 }
1198
1199 ret = lttng_metadata_printf(registry,
1200 " tracer_patchlevel = %u;\n"
1201 " vpid = %d;\n"
1202 " procname = \"%s\";\n"
1203 " vpid_datetime = \"%s\";\n",
1204 per_pid_session->_tracer_patch_level_version, (int) per_pid_session->_vpid,
1205 per_pid_session->_procname.c_str(), datetime);
1206 return ret;
1207 }
1208
1209 /*
1210 * Should be called with session registry mutex held.
1211 */
1212 int ust_metadata_session_statedump(ust_registry_session *session)
1213 {
1214 char uuid_s[LTTNG_UUID_STR_LEN], clock_uuid_s[LTTNG_UUID_STR_LEN];
1215 int ret = 0;
1216
1217 LTTNG_ASSERT(session);
1218
1219 lttng_uuid_to_str(session->_uuid, uuid_s);
1220
1221 /* For crash ABI */
1222 ret = lttng_metadata_printf(session,
1223 "/* CTF %u.%u */\n\n",
1224 CTF_SPEC_MAJOR,
1225 CTF_SPEC_MINOR);
1226 if (ret) {
1227 goto end;
1228 }
1229
1230 ret = lttng_metadata_printf(session,
1231 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1232 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1233 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1234 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1235 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1236 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1237 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1238 "\n"
1239 "trace {\n"
1240 " major = %u;\n"
1241 " minor = %u;\n"
1242 " uuid = \"%s\";\n"
1243 " byte_order = %s;\n"
1244 " packet.header := struct {\n"
1245 " uint32_t magic;\n"
1246 " uint8_t uuid[16];\n"
1247 " uint32_t stream_id;\n"
1248 " uint64_t stream_instance_id;\n"
1249 " };\n"
1250 "};\n\n",
1251 session->_uint8_t_alignment,
1252 session->_uint16_t_alignment,
1253 session->_uint32_t_alignment,
1254 session->_uint64_t_alignment,
1255 session->_bits_per_long,
1256 session->_long_alignment,
1257 CTF_SPEC_MAJOR,
1258 CTF_SPEC_MINOR,
1259 uuid_s,
1260 session->_byte_order == BIG_ENDIAN ? "be" : "le"
1261 );
1262 if (ret) {
1263 goto end;
1264 }
1265
1266 ret = lttng_metadata_printf(session,
1267 "env {\n"
1268 " domain = \"ust\";\n"
1269 " tracer_name = \"lttng-ust\";\n"
1270 " tracer_major = %u;\n"
1271 " tracer_minor = %u;\n"
1272 " tracer_buffering_scheme = \"%s\";\n"
1273 " tracer_buffering_id = %u;\n"
1274 " architecture_bit_width = %u;\n",
1275 session->_app_tracer_version_major, session->_app_tracer_version_minor,
1276 session->get_buffering_scheme() == LTTNG_BUFFER_PER_PID ? "pid" : "uid",
1277 session->get_buffering_scheme() == LTTNG_BUFFER_PER_PID ?
1278 (int) static_cast<ust_registry_session_per_pid *>(session)->_vpid :
1279 (int) static_cast<ust_registry_session_per_uid *>(session)->_tracing_uid,
1280 session->_bits_per_long);
1281 if (ret) {
1282 goto end;
1283 }
1284
1285 ret = print_metadata_session_information(session);
1286 if (ret) {
1287 goto end;
1288 }
1289
1290 /*
1291 * If per-application registry, we can output extra information
1292 * about the application.
1293 */
1294 ret = print_metadata_app_information(session);
1295 if (ret) {
1296 goto end;
1297 }
1298
1299 ret = lttng_metadata_printf(session,
1300 "};\n\n"
1301 );
1302 if (ret) {
1303 goto end;
1304 }
1305
1306 ret = lttng_metadata_printf(session,
1307 "clock {\n"
1308 " name = \"%s\";\n",
1309 trace_clock_name()
1310 );
1311 if (ret) {
1312 goto end;
1313 }
1314
1315 if (!trace_clock_uuid(clock_uuid_s)) {
1316 ret = lttng_metadata_printf(session,
1317 " uuid = \"%s\";\n",
1318 clock_uuid_s
1319 );
1320 if (ret) {
1321 goto end;
1322 }
1323 }
1324
1325 ret = lttng_metadata_printf(session,
1326 " description = \"%s\";\n"
1327 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1328 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1329 " offset = %" PRId64 ";\n"
1330 "};\n\n",
1331 trace_clock_description(),
1332 trace_clock_freq(),
1333 measure_clock_offset()
1334 );
1335 if (ret) {
1336 goto end;
1337 }
1338
1339 ret = lttng_metadata_printf(session,
1340 "typealias integer {\n"
1341 " size = 27; align = 1; signed = false;\n"
1342 " map = clock.%s.value;\n"
1343 "} := uint27_clock_monotonic_t;\n"
1344 "\n"
1345 "typealias integer {\n"
1346 " size = 32; align = %u; signed = false;\n"
1347 " map = clock.%s.value;\n"
1348 "} := uint32_clock_monotonic_t;\n"
1349 "\n"
1350 "typealias integer {\n"
1351 " size = 64; align = %u; signed = false;\n"
1352 " map = clock.%s.value;\n"
1353 "} := uint64_clock_monotonic_t;\n\n",
1354 trace_clock_name(),
1355 session->_uint32_t_alignment,
1356 trace_clock_name(),
1357 session->_uint64_t_alignment,
1358 trace_clock_name()
1359 );
1360 if (ret) {
1361 goto end;
1362 }
1363
1364 ret = _lttng_stream_packet_context_declare(session);
1365 if (ret) {
1366 goto end;
1367 }
1368
1369 ret = _lttng_event_header_declare(session);
1370 if (ret) {
1371 goto end;
1372 }
1373
1374 end:
1375 return ret;
1376 }
This page took 0.082551 seconds and 4 git commands to generate.