Fix: missing error handling in use of print_tabs()
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.c
CommitLineData
d0b96690
DG
1/*
2 * ust-metadata.c
3 *
4 * LTTng-UST metadata generation
5 *
6 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License, version 2 only,
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
6c1c0768 22#define _LGPL_SOURCE
d0b96690
DG
23#include <stdint.h>
24#include <string.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <limits.h>
28#include <unistd.h>
29#include <inttypes.h>
30#include <common/common.h>
395d6b02 31#include <common/time.h>
d0b96690
DG
32
33#include "ust-registry.h"
34#include "ust-clock.h"
35#include "ust-app.h"
36
37#ifndef max_t
38#define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
39#endif
40
8c645bb0
MD
41#define NR_CLOCK_OFFSET_SAMPLES 10
42
43struct offset_sample {
c2636b57 44 int64_t offset; /* correlation offset */
8c645bb0
MD
45 uint64_t measure_delta; /* lower is better */
46};
47
da860cab
MD
48static
49int _lttng_field_statedump(struct ust_registry_session *session,
50 const struct ustctl_field *fields, size_t nr_fields,
51 size_t *iter_field, size_t nesting);
52
d0b96690
DG
53static inline
54int fls(unsigned int x)
55{
56 int r = 32;
57
58 if (!x)
59 return 0;
60 if (!(x & 0xFFFF0000U)) {
61 x <<= 16;
62 r -= 16;
63 }
64 if (!(x & 0xFF000000U)) {
65 x <<= 8;
66 r -= 8;
67 }
68 if (!(x & 0xF0000000U)) {
69 x <<= 4;
70 r -= 4;
71 }
72 if (!(x & 0xC0000000U)) {
73 x <<= 2;
74 r -= 2;
75 }
76 if (!(x & 0x80000000U)) {
77 x <<= 1;
78 r -= 1;
79 }
80 return r;
81}
82
83static inline
84int get_count_order(unsigned int count)
85{
86 int order;
87
88 order = fls(count) - 1;
89 if (count & (count - 1))
90 order++;
91 return order;
92}
93
94/*
95 * Returns offset where to write in metadata array, or negative error value on error.
96 */
97static
98ssize_t metadata_reserve(struct ust_registry_session *session, size_t len)
99{
100 size_t new_len = session->metadata_len + len;
101 size_t new_alloc_len = new_len;
102 size_t old_alloc_len = session->metadata_alloc_len;
103 ssize_t ret;
104
105 if (new_alloc_len > (UINT32_MAX >> 1))
106 return -EINVAL;
107 if ((old_alloc_len << 1) > (UINT32_MAX >> 1))
108 return -EINVAL;
109
110 if (new_alloc_len > old_alloc_len) {
111 char *newptr;
112
113 new_alloc_len =
114 max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
115 newptr = realloc(session->metadata, new_alloc_len);
116 if (!newptr)
117 return -ENOMEM;
118 session->metadata = newptr;
119 /* We zero directly the memory from start of allocation. */
120 memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
121 session->metadata_alloc_len = new_alloc_len;
122 }
123 ret = session->metadata_len;
124 session->metadata_len += len;
125 return ret;
126}
127
d7ba1388
MD
128static
129int metadata_file_append(struct ust_registry_session *session,
130 const char *str, size_t len)
131{
132 ssize_t written;
133
134 if (session->metadata_fd < 0) {
135 return 0;
136 }
137 /* Write to metadata file */
138 written = lttng_write(session->metadata_fd, str, len);
139 if (written != len) {
140 return -1;
141 }
142 return 0;
143}
144
d0b96690
DG
145/*
146 * We have exclusive access to our metadata buffer (protected by the
147 * ust_lock), so we can do racy operations such as looking for
148 * remaining space left in packet and write, since mutual exclusion
149 * protects us from concurrent writes.
150 */
151static
152int lttng_metadata_printf(struct ust_registry_session *session,
153 const char *fmt, ...)
154{
155 char *str = NULL;
156 size_t len;
157 va_list ap;
158 ssize_t offset;
159 int ret;
160
161 va_start(ap, fmt);
162 ret = vasprintf(&str, fmt, ap);
163 va_end(ap);
164 if (ret < 0)
165 return -ENOMEM;
166
167 len = strlen(str);
168 offset = metadata_reserve(session, len);
169 if (offset < 0) {
170 ret = offset;
171 goto end;
172 }
173 memcpy(&session->metadata[offset], str, len);
d7ba1388
MD
174 ret = metadata_file_append(session, str, len);
175 if (ret) {
176 PERROR("Error appending to metadata file");
177 goto end;
178 }
d0b96690
DG
179 DBG3("Append to metadata: \"%s\"", str);
180 ret = 0;
181
182end:
183 free(str);
184 return ret;
185}
186
da860cab
MD
187static
188int print_tabs(struct ust_registry_session *session, size_t nesting)
189{
190 size_t i;
191
192 for (i = 0; i < nesting; i++) {
193 int ret;
194
195 ret = lttng_metadata_printf(session, " ");
196 if (ret) {
197 return ret;
198 }
199 }
200 return 0;
201}
202
a1f68b22
MD
203static
204void sanitize_ctf_identifier(char *out, const char *in)
205{
206 size_t i;
207
208 for (i = 0; i < LTTNG_UST_SYM_NAME_LEN; i++) {
209 switch (in[i]) {
210 case '.':
211 case '$':
212 case ':':
213 out[i] = '_';
214 break;
215 default:
216 out[i] = in[i];
217 }
218 }
219}
220
10b56aef
MD
221/* Called with session registry mutex held. */
222static
223int ust_metadata_enum_statedump(struct ust_registry_session *session,
224 const char *enum_name,
225 uint64_t enum_id,
226 const struct ustctl_integer_type *container_type,
da860cab 227 const char *field_name, size_t *iter_field, size_t nesting)
10b56aef
MD
228{
229 struct ust_registry_enum *reg_enum;
230 const struct ustctl_enum_entry *entries;
231 size_t nr_entries;
232 int ret = 0;
233 size_t i;
a1f68b22 234 char identifier[LTTNG_UST_SYM_NAME_LEN];
10b56aef
MD
235
236 rcu_read_lock();
237 reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id);
238 rcu_read_unlock();
239 /* reg_enum can still be used because session registry mutex is held. */
240 if (!reg_enum) {
241 ret = -ENOENT;
242 goto end;
243 }
244 entries = reg_enum->entries;
245 nr_entries = reg_enum->nr_entries;
246
da860cab
MD
247 ret = print_tabs(session, nesting);
248 if (ret) {
249 goto end;
250 }
10b56aef 251 ret = lttng_metadata_printf(session,
da860cab 252 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
10b56aef
MD
253 container_type->size,
254 container_type->alignment,
255 container_type->signedness,
256 (container_type->encoding == ustctl_encode_none)
257 ? "none"
258 : (container_type->encoding == ustctl_encode_UTF8)
259 ? "UTF8"
260 : "ASCII",
261 container_type->base);
262 if (ret) {
263 goto end;
264 }
1ddb0e8a 265 nesting++;
10b56aef
MD
266 /* Dump all entries */
267 for (i = 0; i < nr_entries; i++) {
268 const struct ustctl_enum_entry *entry = &entries[i];
269 int j, len;
270
a1f68b22
MD
271 ret = print_tabs(session, nesting);
272 if (ret) {
273 goto end;
274 }
10b56aef 275 ret = lttng_metadata_printf(session,
a1f68b22 276 "\"");
10b56aef
MD
277 if (ret) {
278 goto end;
279 }
280 len = strlen(entry->string);
281 /* Escape the character '"' */
282 for (j = 0; j < len; j++) {
283 char c = entry->string[j];
284
285 switch (c) {
286 case '"':
287 ret = lttng_metadata_printf(session,
288 "\\\"");
289 break;
290 case '\\':
291 ret = lttng_metadata_printf(session,
292 "\\\\");
293 break;
294 default:
295 ret = lttng_metadata_printf(session,
296 "%c", c);
297 break;
298 }
299 if (ret) {
300 goto end;
301 }
302 }
e85e3723 303 ret = lttng_metadata_printf(session, "\"");
10b56aef
MD
304 if (ret) {
305 goto end;
306 }
3b016e58 307
9d27cec7
PP
308 if (entry->u.extra.options &
309 USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
e85e3723
PP
310 ret = lttng_metadata_printf(session, ",\n");
311 if (ret) {
312 goto end;
313 }
10b56aef
MD
314 } else {
315 ret = lttng_metadata_printf(session,
e85e3723
PP
316 " = ");
317 if (ret) {
318 goto end;
319 }
3b016e58 320
e85e3723 321 if (entry->start.signedness) {
3b016e58 322 ret = lttng_metadata_printf(session,
e85e3723 323 "%lld", (long long) entry->start.value);
3b016e58
MD
324 } else {
325 ret = lttng_metadata_printf(session,
e85e3723
PP
326 "%llu", entry->start.value);
327 }
328 if (ret) {
329 goto end;
330 }
331
332 if (entry->start.signedness == entry->end.signedness &&
333 entry->start.value ==
334 entry->end.value) {
335 ret = lttng_metadata_printf(session, ",\n");
336 } else {
337 if (entry->end.signedness) {
338 ret = lttng_metadata_printf(session,
339 " ... %lld,\n",
340 (long long) entry->end.value);
341 } else {
342 ret = lttng_metadata_printf(session,
343 " ... %llu,\n",
344 entry->end.value);
345 }
346 }
347 if (ret) {
348 goto end;
3b016e58 349 }
10b56aef
MD
350 }
351 }
1ddb0e8a 352 nesting--;
a1f68b22
MD
353 sanitize_ctf_identifier(identifier, field_name);
354 ret = print_tabs(session, nesting);
355 if (ret) {
356 goto end;
357 }
358 ret = lttng_metadata_printf(session, "} _%s;\n",
359 identifier);
da860cab
MD
360end:
361 (*iter_field)++;
362 return ret;
363}
364
da860cab
MD
365static
366int _lttng_variant_statedump(struct ust_registry_session *session,
367 const struct ustctl_field *fields, size_t nr_fields,
368 size_t *iter_field, size_t nesting)
369{
370 const struct ustctl_field *variant = &fields[*iter_field];
371 uint32_t nr_choices, i;
372 int ret;
a1f68b22 373 char identifier[LTTNG_UST_SYM_NAME_LEN];
da860cab
MD
374
375 if (variant->type.atype != ustctl_atype_variant) {
376 ret = -EINVAL;
377 goto end;
378 }
379 nr_choices = variant->type.u.variant.nr_choices;
380 (*iter_field)++;
a1f68b22
MD
381 sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
382 ret = print_tabs(session, nesting);
383 if (ret) {
384 goto end;
385 }
da860cab 386 ret = lttng_metadata_printf(session,
a1f68b22
MD
387 "variant <_%s> {\n",
388 identifier);
da860cab
MD
389 if (ret) {
390 goto end;
391 }
392
393 for (i = 0; i < nr_choices; i++) {
394 if (*iter_field >= nr_fields) {
395 ret = -EOVERFLOW;
396 goto end;
397 }
398 ret = _lttng_field_statedump(session,
399 fields, nr_fields,
400 iter_field, nesting + 1);
dc6403f3
JG
401 if (ret) {
402 goto end;
403 }
da860cab 404 }
a1f68b22
MD
405 sanitize_ctf_identifier(identifier, variant->name);
406 ret = print_tabs(session, nesting);
ef5a8bcf
JR
407 if (ret) {
408 goto end;
409 }
da860cab 410 ret = lttng_metadata_printf(session,
a1f68b22
MD
411 "} _%s;\n",
412 identifier);
da860cab
MD
413 if (ret) {
414 goto end;
415 }
10b56aef
MD
416end:
417 return ret;
418}
419
d0b96690
DG
420static
421int _lttng_field_statedump(struct ust_registry_session *session,
da860cab
MD
422 const struct ustctl_field *fields, size_t nr_fields,
423 size_t *iter_field, size_t nesting)
d0b96690
DG
424{
425 int ret = 0;
426 const char *bo_be = " byte_order = be;";
427 const char *bo_le = " byte_order = le;";
428 const char *bo_native = "";
429 const char *bo_reverse;
da860cab 430 const struct ustctl_field *field;
d0b96690 431
da860cab
MD
432 if (*iter_field >= nr_fields) {
433 ret = -EOVERFLOW;
434 goto end;
435 }
436 field = &fields[*iter_field];
437
438 if (session->byte_order == BIG_ENDIAN) {
d0b96690 439 bo_reverse = bo_le;
da860cab 440 } else {
d0b96690 441 bo_reverse = bo_be;
da860cab 442 }
d0b96690
DG
443
444 switch (field->type.atype) {
445 case ustctl_atype_integer:
da860cab
MD
446 ret = print_tabs(session, nesting);
447 if (ret) {
448 goto end;
449 }
d0b96690 450 ret = lttng_metadata_printf(session,
da860cab 451 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
d0b96690
DG
452 field->type.u.basic.integer.size,
453 field->type.u.basic.integer.alignment,
454 field->type.u.basic.integer.signedness,
455 (field->type.u.basic.integer.encoding == ustctl_encode_none)
456 ? "none"
457 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
458 ? "UTF8"
459 : "ASCII",
460 field->type.u.basic.integer.base,
461 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
462 field->name);
da860cab 463 (*iter_field)++;
d0b96690 464 break;
10b56aef
MD
465 case ustctl_atype_enum:
466 ret = ust_metadata_enum_statedump(session,
467 field->type.u.basic.enumeration.name,
468 field->type.u.basic.enumeration.id,
469 &field->type.u.basic.enumeration.container_type,
da860cab 470 field->name, iter_field, nesting);
10b56aef 471 break;
d0b96690 472 case ustctl_atype_float:
da860cab
MD
473 ret = print_tabs(session, nesting);
474 if (ret) {
475 goto end;
476 }
d0b96690 477 ret = lttng_metadata_printf(session,
da860cab 478 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
d0b96690
DG
479 field->type.u.basic._float.exp_dig,
480 field->type.u.basic._float.mant_dig,
481 field->type.u.basic._float.alignment,
482 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
483 field->name);
da860cab 484 (*iter_field)++;
d0b96690 485 break;
d0b96690
DG
486 case ustctl_atype_array:
487 {
488 const struct ustctl_basic_type *elem_type;
489
da860cab
MD
490 ret = print_tabs(session, nesting);
491 if (ret) {
492 goto end;
493 }
d0b96690
DG
494 elem_type = &field->type.u.array.elem_type;
495 ret = lttng_metadata_printf(session,
da860cab 496 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
d0b96690
DG
497 elem_type->u.basic.integer.size,
498 elem_type->u.basic.integer.alignment,
499 elem_type->u.basic.integer.signedness,
500 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
501 ? "none"
502 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
503 ? "UTF8"
504 : "ASCII",
505 elem_type->u.basic.integer.base,
506 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
507 field->name, field->type.u.array.length);
da860cab 508 (*iter_field)++;
d0b96690
DG
509 break;
510 }
511 case ustctl_atype_sequence:
512 {
513 const struct ustctl_basic_type *elem_type;
514 const struct ustctl_basic_type *length_type;
515
516 elem_type = &field->type.u.sequence.elem_type;
517 length_type = &field->type.u.sequence.length_type;
da860cab
MD
518 ret = print_tabs(session, nesting);
519 if (ret) {
520 goto end;
521 }
d0b96690 522 ret = lttng_metadata_printf(session,
da860cab 523 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
d0b96690
DG
524 length_type->u.basic.integer.size,
525 (unsigned int) length_type->u.basic.integer.alignment,
526 length_type->u.basic.integer.signedness,
527 (length_type->u.basic.integer.encoding == ustctl_encode_none)
528 ? "none"
529 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
530 ? "UTF8"
531 : "ASCII"),
532 length_type->u.basic.integer.base,
533 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
534 field->name);
da860cab
MD
535 if (ret) {
536 goto end;
537 }
d0b96690 538
da860cab
MD
539 ret = print_tabs(session, nesting);
540 if (ret) {
541 goto end;
542 }
d0b96690 543 ret = lttng_metadata_printf(session,
da860cab 544 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
d0b96690
DG
545 elem_type->u.basic.integer.size,
546 (unsigned int) elem_type->u.basic.integer.alignment,
547 elem_type->u.basic.integer.signedness,
548 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
549 ? "none"
550 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
551 ? "UTF8"
552 : "ASCII"),
553 elem_type->u.basic.integer.base,
554 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
555 field->name,
556 field->name);
da860cab 557 (*iter_field)++;
d0b96690
DG
558 break;
559 }
560
561 case ustctl_atype_string:
562 /* Default encoding is UTF8 */
da860cab
MD
563 ret = print_tabs(session, nesting);
564 if (ret) {
565 goto end;
566 }
d0b96690 567 ret = lttng_metadata_printf(session,
da860cab 568 "string%s _%s;\n",
d0b96690
DG
569 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
570 " { encoding = ASCII; }" : "",
571 field->name);
da860cab
MD
572 (*iter_field)++;
573 break;
574 case ustctl_atype_variant:
575 ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting);
576 if (ret) {
577 goto end;
578 }
579 break;
580 case ustctl_atype_struct:
581 ret = print_tabs(session, nesting);
582 if (ret) {
583 goto end;
584 }
585 ret = lttng_metadata_printf(session,
586 "struct {} _%s;\n",
587 field->name);
588 (*iter_field)++;
d0b96690
DG
589 break;
590 default:
da860cab 591 ret = -EINVAL;
d0b96690 592 }
da860cab 593end:
d0b96690
DG
594 return ret;
595}
596
597static
598int _lttng_context_metadata_statedump(struct ust_registry_session *session,
599 size_t nr_ctx_fields,
600 struct ustctl_field *ctx)
601{
602 int ret = 0;
da860cab 603 size_t i = 0;
d0b96690
DG
604
605 if (!ctx)
606 return 0;
da860cab
MD
607 for (;;) {
608 if (i >= nr_ctx_fields) {
609 break;
610 }
611 ret = _lttng_field_statedump(session, ctx,
612 nr_ctx_fields, &i, 2);
613 if (ret) {
614 break;
615 }
d0b96690
DG
616 }
617 return ret;
618}
619
620static
621int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
622 struct ust_registry_event *event)
623{
624 int ret = 0;
da860cab 625 size_t i = 0;
d0b96690 626
da860cab
MD
627 for (;;) {
628 if (i >= event->nr_fields) {
629 break;
630 }
631 ret = _lttng_field_statedump(session, event->fields,
632 event->nr_fields, &i, 2);
633 if (ret) {
634 break;
635 }
d0b96690
DG
636 }
637 return ret;
638}
639
640/*
641 * Should be called with session registry mutex held.
642 */
643int ust_metadata_event_statedump(struct ust_registry_session *session,
644 struct ust_registry_channel *chan,
645 struct ust_registry_event *event)
646{
647 int ret = 0;
648
649 /* Don't dump metadata events */
650 if (chan->chan_id == -1U)
651 return 0;
652
653 ret = lttng_metadata_printf(session,
654 "event {\n"
655 " name = \"%s\";\n"
656 " id = %u;\n"
657 " stream_id = %u;\n",
658 event->name,
659 event->id,
660 chan->chan_id);
661 if (ret)
662 goto end;
663
664 ret = lttng_metadata_printf(session,
665 " loglevel = %d;\n",
2106efa0 666 event->loglevel_value);
d0b96690
DG
667 if (ret)
668 goto end;
669
670 if (event->model_emf_uri) {
671 ret = lttng_metadata_printf(session,
672 " model.emf.uri = \"%s\";\n",
673 event->model_emf_uri);
674 if (ret)
675 goto end;
676 }
677
d0b96690
DG
678 ret = lttng_metadata_printf(session,
679 " fields := struct {\n"
680 );
681 if (ret)
682 goto end;
683
684 ret = _lttng_fields_metadata_statedump(session, event);
685 if (ret)
686 goto end;
687
688 ret = lttng_metadata_printf(session,
689 " };\n"
690 "};\n\n");
691 if (ret)
692 goto end;
7972aab2 693 event->metadata_dumped = 1;
d0b96690
DG
694
695end:
696 return ret;
697}
698
699/*
700 * Should be called with session registry mutex held.
701 */
702int ust_metadata_channel_statedump(struct ust_registry_session *session,
703 struct ust_registry_channel *chan)
704{
705 int ret = 0;
706
707 /* Don't dump metadata events */
708 if (chan->chan_id == -1U)
709 return 0;
710
711 if (!chan->header_type)
712 return -EINVAL;
713
714 ret = lttng_metadata_printf(session,
715 "stream {\n"
716 " id = %u;\n"
717 " event.header := %s;\n"
718 " packet.context := struct packet_context;\n",
719 chan->chan_id,
720 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
721 "struct event_header_compact" :
722 "struct event_header_large");
723 if (ret)
724 goto end;
725
726 if (chan->ctx_fields) {
727 ret = lttng_metadata_printf(session,
728 " event.context := struct {\n");
729 if (ret)
730 goto end;
731 }
732 ret = _lttng_context_metadata_statedump(session,
733 chan->nr_ctx_fields,
734 chan->ctx_fields);
735 if (ret)
736 goto end;
737 if (chan->ctx_fields) {
738 ret = lttng_metadata_printf(session,
739 " };\n");
740 if (ret)
741 goto end;
742 }
743
744 ret = lttng_metadata_printf(session,
745 "};\n\n");
7972aab2
DG
746 /* Flag success of metadata dump. */
747 chan->metadata_dumped = 1;
d0b96690
DG
748
749end:
750 return ret;
751}
752
753static
754int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
755{
756 return lttng_metadata_printf(session,
757 "struct packet_context {\n"
758 " uint64_clock_monotonic_t timestamp_begin;\n"
759 " uint64_clock_monotonic_t timestamp_end;\n"
760 " uint64_t content_size;\n"
761 " uint64_t packet_size;\n"
0793bcc6 762 " uint64_t packet_seq_num;\n"
d0b96690
DG
763 " unsigned long events_discarded;\n"
764 " uint32_t cpu_id;\n"
765 "};\n\n"
766 );
767}
768
769/*
770 * Compact header:
771 * id: range: 0 - 30.
772 * id 31 is reserved to indicate an extended header.
773 *
774 * Large header:
775 * id: range: 0 - 65534.
776 * id 65535 is reserved to indicate an extended header.
777 */
778static
779int _lttng_event_header_declare(struct ust_registry_session *session)
780{
781 return lttng_metadata_printf(session,
782 "struct event_header_compact {\n"
783 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
784 " variant <id> {\n"
785 " struct {\n"
786 " uint27_clock_monotonic_t timestamp;\n"
787 " } compact;\n"
788 " struct {\n"
789 " uint32_t id;\n"
790 " uint64_clock_monotonic_t timestamp;\n"
791 " } extended;\n"
792 " } v;\n"
793 "} align(%u);\n"
794 "\n"
795 "struct event_header_large {\n"
796 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
797 " variant <id> {\n"
798 " struct {\n"
799 " uint32_clock_monotonic_t timestamp;\n"
800 " } compact;\n"
801 " struct {\n"
802 " uint32_t id;\n"
803 " uint64_clock_monotonic_t timestamp;\n"
804 " } extended;\n"
805 " } v;\n"
806 "} align(%u);\n\n",
807 session->uint32_t_alignment,
808 session->uint16_t_alignment
809 );
810}
811
dc113ec7
MD
812/*
813 * The offset between monotonic and realtime clock can be negative if
814 * the system sets the REALTIME clock to 0 after boot.
dc113ec7 815 */
d0b96690 816static
8c645bb0 817int measure_single_clock_offset(struct offset_sample *sample)
d0b96690 818{
dc113ec7 819 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
fc0bb9fa 820 uint64_t tcf = trace_clock_freq();
d0b96690
DG
821 struct timespec rts = { 0, 0 };
822 int ret;
823
824 monotonic[0] = trace_clock_read64();
389fbf04 825 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
8c645bb0
MD
826 if (ret < 0) {
827 return ret;
828 }
d0b96690 829 monotonic[1] = trace_clock_read64();
8c645bb0
MD
830 measure_delta = monotonic[1] - monotonic[0];
831 if (measure_delta > sample->measure_delta) {
832 /*
833 * Discard value if it took longer to read than the best
834 * sample so far.
835 */
836 return 0;
837 }
dc113ec7 838 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
6e1b0543
MD
839 realtime = (uint64_t) rts.tv_sec * tcf;
840 if (tcf == NSEC_PER_SEC) {
841 realtime += rts.tv_nsec;
842 } else {
843 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
fc0bb9fa 844 }
c2636b57 845 sample->offset = (int64_t) realtime - monotonic_avg;
8c645bb0
MD
846 sample->measure_delta = measure_delta;
847 return 0;
d0b96690
DG
848}
849
8c645bb0
MD
850/*
851 * Approximation of NTP time of day to clock monotonic correlation,
852 * taken at start of trace. Keep the measurement that took the less time
853 * to complete, thus removing imprecision caused by preemption.
c2636b57 854 * May return a negative offset.
8c645bb0
MD
855 */
856static
c2636b57 857int64_t measure_clock_offset(void)
8c645bb0
MD
858{
859 int i;
860 struct offset_sample offset_best_sample = {
861 .offset = 0,
862 .measure_delta = UINT64_MAX,
863 };
864
865 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
866 if (measure_single_clock_offset(&offset_best_sample)) {
867 return 0;
868 }
869 }
870 return offset_best_sample.offset;
871}
d0b96690
DG
872
873/*
874 * Should be called with session registry mutex held.
875 */
876int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf
MD
877 struct ust_app *app,
878 uint32_t major,
879 uint32_t minor)
d0b96690
DG
880{
881 unsigned char *uuid_c;
882 char uuid_s[UUID_STR_LEN],
883 clock_uuid_s[UUID_STR_LEN];
884 int ret = 0;
885 char hostname[HOST_NAME_MAX];
886
7972aab2 887 assert(session);
7972aab2 888
d0b96690
DG
889 uuid_c = session->uuid;
890
891 snprintf(uuid_s, sizeof(uuid_s),
892 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
893 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
894 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
895 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
896 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
897
d7ba1388
MD
898 /* For crash ABI */
899 ret = lttng_metadata_printf(session,
900 "/* CTF %u.%u */\n\n",
901 CTF_SPEC_MAJOR,
902 CTF_SPEC_MINOR);
903 if (ret) {
904 goto end;
905 }
906
d0b96690
DG
907 ret = lttng_metadata_printf(session,
908 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
909 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
910 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
911 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
912 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
913 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
914 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
915 "\n"
916 "trace {\n"
917 " major = %u;\n"
918 " minor = %u;\n"
919 " uuid = \"%s\";\n"
920 " byte_order = %s;\n"
921 " packet.header := struct {\n"
922 " uint32_t magic;\n"
923 " uint8_t uuid[16];\n"
924 " uint32_t stream_id;\n"
0793bcc6 925 " uint64_t stream_instance_id;\n"
d0b96690
DG
926 " };\n"
927 "};\n\n",
928 session->uint8_t_alignment,
929 session->uint16_t_alignment,
930 session->uint32_t_alignment,
931 session->uint64_t_alignment,
932 session->bits_per_long,
933 session->long_alignment,
934 CTF_SPEC_MAJOR,
935 CTF_SPEC_MINOR,
936 uuid_s,
937 session->byte_order == BIG_ENDIAN ? "be" : "le"
938 );
939 if (ret)
940 goto end;
941
942 /* ignore error, just use empty string if error. */
943 hostname[0] = '\0';
944 ret = gethostname(hostname, sizeof(hostname));
945 if (ret && errno == ENAMETOOLONG)
946 hostname[HOST_NAME_MAX - 1] = '\0';
947 ret = lttng_metadata_printf(session,
948 "env {\n"
949 " hostname = \"%s\";\n"
950 " domain = \"ust\";\n"
951 " tracer_name = \"lttng-ust\";\n"
952 " tracer_major = %u;\n"
af6142cf 953 " tracer_minor = %u;\n",
d0b96690 954 hostname,
af6142cf
MD
955 major,
956 minor
d0b96690
DG
957 );
958 if (ret)
959 goto end;
960
961 /*
962 * If per-application registry, we can output extra information
963 * about the application.
964 */
965 if (app) {
966 ret = lttng_metadata_printf(session,
af6142cf 967 " tracer_patchlevel = %u;\n"
d0b96690 968 " vpid = %d;\n"
d88aee68 969 " procname = \"%s\";\n",
af6142cf 970 app->version.patchlevel,
d0b96690
DG
971 (int) app->pid,
972 app->name
973 );
974 if (ret)
975 goto end;
976 }
977
978 ret = lttng_metadata_printf(session,
979 "};\n\n"
980 );
981 if (ret)
982 goto end;
983
984
985 ret = lttng_metadata_printf(session,
986 "clock {\n"
fc0bb9fa
MD
987 " name = \"%s\";\n",
988 trace_clock_name()
d0b96690
DG
989 );
990 if (ret)
991 goto end;
992
993 if (!trace_clock_uuid(clock_uuid_s)) {
994 ret = lttng_metadata_printf(session,
995 " uuid = \"%s\";\n",
996 clock_uuid_s
997 );
998 if (ret)
999 goto end;
1000 }
1001
1002 ret = lttng_metadata_printf(session,
fc0bb9fa 1003 " description = \"%s\";\n"
d0b96690
DG
1004 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1005 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
c2636b57 1006 " offset = %" PRId64 ";\n"
d0b96690 1007 "};\n\n",
fc0bb9fa 1008 trace_clock_description(),
d0b96690
DG
1009 trace_clock_freq(),
1010 measure_clock_offset()
1011 );
1012 if (ret)
1013 goto end;
1014
1015 ret = lttng_metadata_printf(session,
1016 "typealias integer {\n"
1017 " size = 27; align = 1; signed = false;\n"
fc0bb9fa 1018 " map = clock.%s.value;\n"
d0b96690
DG
1019 "} := uint27_clock_monotonic_t;\n"
1020 "\n"
1021 "typealias integer {\n"
1022 " size = 32; align = %u; signed = false;\n"
fc0bb9fa 1023 " map = clock.%s.value;\n"
d0b96690
DG
1024 "} := uint32_clock_monotonic_t;\n"
1025 "\n"
1026 "typealias integer {\n"
1027 " size = 64; align = %u; signed = false;\n"
fc0bb9fa 1028 " map = clock.%s.value;\n"
d0b96690 1029 "} := uint64_clock_monotonic_t;\n\n",
fc0bb9fa 1030 trace_clock_name(),
d0b96690 1031 session->uint32_t_alignment,
fc0bb9fa
MD
1032 trace_clock_name(),
1033 session->uint64_t_alignment,
1034 trace_clock_name()
d0b96690
DG
1035 );
1036 if (ret)
1037 goto end;
1038
1039 ret = _lttng_stream_packet_context_declare(session);
1040 if (ret)
1041 goto end;
1042
1043 ret = _lttng_event_header_declare(session);
1044 if (ret)
1045 goto end;
1046
1047end:
1048 return ret;
1049}
This page took 0.082294 seconds and 4 git commands to generate.