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