Fix: Integer overflowed argument
[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>
31
32#include "ust-registry.h"
33#include "ust-clock.h"
34#include "ust-app.h"
35
36#ifndef max_t
37#define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
38#endif
39
6e1b0543 40#define NSEC_PER_SEC 1000000000ULL
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 }
1dbccf09 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 }
303 ret = lttng_metadata_printf(session,
304 "\" = ");
305 if (ret) {
306 goto end;
307 }
76e8b16e
MD
308
309 if (entry->start.signedness) {
10b56aef 310 ret = lttng_metadata_printf(session,
76e8b16e 311 "%lld", (long long) entry->start.value);
10b56aef
MD
312 } else {
313 ret = lttng_metadata_printf(session,
76e8b16e
MD
314 "%llu", entry->start.value);
315 }
316 if (ret) {
317 goto end;
318 }
319
320 if (entry->start.signedness == entry->end.signedness &&
321 entry->start.value == entry->end.value) {
322 ret = lttng_metadata_printf(session,
323 ",\n");
324 } else {
325 if (entry->end.signedness) {
326 ret = lttng_metadata_printf(session,
327 " ... %lld,\n", (long long) entry->end.value);
328 } else {
329 ret = lttng_metadata_printf(session,
330 " ... %llu,\n", entry->end.value);
331 }
10b56aef
MD
332 }
333 if (ret) {
334 goto end;
335 }
336 }
1dbccf09 337 nesting--;
a1f68b22
MD
338 sanitize_ctf_identifier(identifier, field_name);
339 ret = print_tabs(session, nesting);
340 if (ret) {
341 goto end;
342 }
343 ret = lttng_metadata_printf(session, "} _%s;\n",
344 identifier);
da860cab
MD
345end:
346 (*iter_field)++;
347 return ret;
348}
349
da860cab
MD
350static
351int _lttng_variant_statedump(struct ust_registry_session *session,
352 const struct ustctl_field *fields, size_t nr_fields,
353 size_t *iter_field, size_t nesting)
354{
355 const struct ustctl_field *variant = &fields[*iter_field];
356 uint32_t nr_choices, i;
357 int ret;
a1f68b22 358 char identifier[LTTNG_UST_SYM_NAME_LEN];
da860cab
MD
359
360 if (variant->type.atype != ustctl_atype_variant) {
361 ret = -EINVAL;
362 goto end;
363 }
364 nr_choices = variant->type.u.variant.nr_choices;
365 (*iter_field)++;
a1f68b22
MD
366 sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
367 ret = print_tabs(session, nesting);
368 if (ret) {
369 goto end;
370 }
da860cab 371 ret = lttng_metadata_printf(session,
a1f68b22
MD
372 "variant <_%s> {\n",
373 identifier);
da860cab
MD
374 if (ret) {
375 goto end;
376 }
377
378 for (i = 0; i < nr_choices; i++) {
379 if (*iter_field >= nr_fields) {
380 ret = -EOVERFLOW;
381 goto end;
382 }
383 ret = _lttng_field_statedump(session,
384 fields, nr_fields,
385 iter_field, nesting + 1);
dc6403f3
JG
386 if (ret) {
387 goto end;
388 }
da860cab 389 }
a1f68b22
MD
390 sanitize_ctf_identifier(identifier, variant->name);
391 ret = print_tabs(session, nesting);
da860cab 392 ret = lttng_metadata_printf(session,
a1f68b22
MD
393 "} _%s;\n",
394 identifier);
da860cab
MD
395 if (ret) {
396 goto end;
397 }
10b56aef
MD
398end:
399 return ret;
400}
401
d0b96690
DG
402static
403int _lttng_field_statedump(struct ust_registry_session *session,
da860cab
MD
404 const struct ustctl_field *fields, size_t nr_fields,
405 size_t *iter_field, size_t nesting)
d0b96690
DG
406{
407 int ret = 0;
408 const char *bo_be = " byte_order = be;";
409 const char *bo_le = " byte_order = le;";
410 const char *bo_native = "";
411 const char *bo_reverse;
da860cab 412 const struct ustctl_field *field;
d0b96690 413
da860cab
MD
414 if (*iter_field >= nr_fields) {
415 ret = -EOVERFLOW;
416 goto end;
417 }
418 field = &fields[*iter_field];
419
420 if (session->byte_order == BIG_ENDIAN) {
d0b96690 421 bo_reverse = bo_le;
da860cab 422 } else {
d0b96690 423 bo_reverse = bo_be;
da860cab 424 }
d0b96690
DG
425
426 switch (field->type.atype) {
427 case ustctl_atype_integer:
da860cab
MD
428 ret = print_tabs(session, nesting);
429 if (ret) {
430 goto end;
431 }
d0b96690 432 ret = lttng_metadata_printf(session,
da860cab 433 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
d0b96690
DG
434 field->type.u.basic.integer.size,
435 field->type.u.basic.integer.alignment,
436 field->type.u.basic.integer.signedness,
437 (field->type.u.basic.integer.encoding == ustctl_encode_none)
438 ? "none"
439 : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8)
440 ? "UTF8"
441 : "ASCII",
442 field->type.u.basic.integer.base,
443 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
444 field->name);
da860cab 445 (*iter_field)++;
d0b96690 446 break;
10b56aef
MD
447 case ustctl_atype_enum:
448 ret = ust_metadata_enum_statedump(session,
449 field->type.u.basic.enumeration.name,
450 field->type.u.basic.enumeration.id,
451 &field->type.u.basic.enumeration.container_type,
da860cab 452 field->name, iter_field, nesting);
10b56aef 453 break;
d0b96690 454 case ustctl_atype_float:
da860cab
MD
455 ret = print_tabs(session, nesting);
456 if (ret) {
457 goto end;
458 }
d0b96690 459 ret = lttng_metadata_printf(session,
da860cab 460 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
d0b96690
DG
461 field->type.u.basic._float.exp_dig,
462 field->type.u.basic._float.mant_dig,
463 field->type.u.basic._float.alignment,
464 field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
465 field->name);
da860cab 466 (*iter_field)++;
d0b96690 467 break;
d0b96690
DG
468 case ustctl_atype_array:
469 {
470 const struct ustctl_basic_type *elem_type;
471
da860cab
MD
472 ret = print_tabs(session, nesting);
473 if (ret) {
474 goto end;
475 }
d0b96690
DG
476 elem_type = &field->type.u.array.elem_type;
477 ret = lttng_metadata_printf(session,
da860cab 478 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
d0b96690
DG
479 elem_type->u.basic.integer.size,
480 elem_type->u.basic.integer.alignment,
481 elem_type->u.basic.integer.signedness,
482 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
483 ? "none"
484 : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
485 ? "UTF8"
486 : "ASCII",
487 elem_type->u.basic.integer.base,
488 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
489 field->name, field->type.u.array.length);
da860cab 490 (*iter_field)++;
d0b96690
DG
491 break;
492 }
493 case ustctl_atype_sequence:
494 {
495 const struct ustctl_basic_type *elem_type;
496 const struct ustctl_basic_type *length_type;
497
498 elem_type = &field->type.u.sequence.elem_type;
499 length_type = &field->type.u.sequence.length_type;
da860cab
MD
500 ret = print_tabs(session, nesting);
501 if (ret) {
502 goto end;
503 }
d0b96690 504 ret = lttng_metadata_printf(session,
da860cab 505 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
d0b96690
DG
506 length_type->u.basic.integer.size,
507 (unsigned int) length_type->u.basic.integer.alignment,
508 length_type->u.basic.integer.signedness,
509 (length_type->u.basic.integer.encoding == ustctl_encode_none)
510 ? "none"
511 : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8)
512 ? "UTF8"
513 : "ASCII"),
514 length_type->u.basic.integer.base,
515 length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
516 field->name);
da860cab
MD
517 if (ret) {
518 goto end;
519 }
d0b96690 520
da860cab
MD
521 ret = print_tabs(session, nesting);
522 if (ret) {
523 goto end;
524 }
d0b96690 525 ret = lttng_metadata_printf(session,
da860cab 526 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
d0b96690
DG
527 elem_type->u.basic.integer.size,
528 (unsigned int) elem_type->u.basic.integer.alignment,
529 elem_type->u.basic.integer.signedness,
530 (elem_type->u.basic.integer.encoding == ustctl_encode_none)
531 ? "none"
532 : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8)
533 ? "UTF8"
534 : "ASCII"),
535 elem_type->u.basic.integer.base,
536 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
537 field->name,
538 field->name);
da860cab 539 (*iter_field)++;
d0b96690
DG
540 break;
541 }
542
543 case ustctl_atype_string:
544 /* Default encoding is UTF8 */
da860cab
MD
545 ret = print_tabs(session, nesting);
546 if (ret) {
547 goto end;
548 }
d0b96690 549 ret = lttng_metadata_printf(session,
da860cab 550 "string%s _%s;\n",
d0b96690
DG
551 field->type.u.basic.string.encoding == ustctl_encode_ASCII ?
552 " { encoding = ASCII; }" : "",
553 field->name);
da860cab
MD
554 (*iter_field)++;
555 break;
556 case ustctl_atype_variant:
557 ret = _lttng_variant_statedump(session, fields, nr_fields, iter_field, nesting);
558 if (ret) {
559 goto end;
560 }
561 break;
562 case ustctl_atype_struct:
563 ret = print_tabs(session, nesting);
564 if (ret) {
565 goto end;
566 }
567 ret = lttng_metadata_printf(session,
568 "struct {} _%s;\n",
569 field->name);
570 (*iter_field)++;
d0b96690
DG
571 break;
572 default:
da860cab 573 ret = -EINVAL;
d0b96690 574 }
da860cab 575end:
d0b96690
DG
576 return ret;
577}
578
579static
580int _lttng_context_metadata_statedump(struct ust_registry_session *session,
581 size_t nr_ctx_fields,
582 struct ustctl_field *ctx)
583{
584 int ret = 0;
da860cab 585 size_t i = 0;
d0b96690
DG
586
587 if (!ctx)
588 return 0;
da860cab
MD
589 for (;;) {
590 if (i >= nr_ctx_fields) {
591 break;
592 }
593 ret = _lttng_field_statedump(session, ctx,
594 nr_ctx_fields, &i, 2);
595 if (ret) {
596 break;
597 }
d0b96690
DG
598 }
599 return ret;
600}
601
602static
603int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
604 struct ust_registry_event *event)
605{
606 int ret = 0;
da860cab 607 size_t i = 0;
d0b96690 608
da860cab
MD
609 for (;;) {
610 if (i >= event->nr_fields) {
611 break;
612 }
613 ret = _lttng_field_statedump(session, event->fields,
614 event->nr_fields, &i, 2);
615 if (ret) {
616 break;
617 }
d0b96690
DG
618 }
619 return ret;
620}
621
622/*
623 * Should be called with session registry mutex held.
624 */
625int ust_metadata_event_statedump(struct ust_registry_session *session,
626 struct ust_registry_channel *chan,
627 struct ust_registry_event *event)
628{
629 int ret = 0;
630
631 /* Don't dump metadata events */
632 if (chan->chan_id == -1U)
633 return 0;
634
635 ret = lttng_metadata_printf(session,
636 "event {\n"
637 " name = \"%s\";\n"
638 " id = %u;\n"
639 " stream_id = %u;\n",
640 event->name,
641 event->id,
642 chan->chan_id);
643 if (ret)
644 goto end;
645
646 ret = lttng_metadata_printf(session,
647 " loglevel = %d;\n",
2106efa0 648 event->loglevel_value);
d0b96690
DG
649 if (ret)
650 goto end;
651
652 if (event->model_emf_uri) {
653 ret = lttng_metadata_printf(session,
654 " model.emf.uri = \"%s\";\n",
655 event->model_emf_uri);
656 if (ret)
657 goto end;
658 }
659
d0b96690
DG
660 ret = lttng_metadata_printf(session,
661 " fields := struct {\n"
662 );
663 if (ret)
664 goto end;
665
666 ret = _lttng_fields_metadata_statedump(session, event);
667 if (ret)
668 goto end;
669
670 ret = lttng_metadata_printf(session,
671 " };\n"
672 "};\n\n");
673 if (ret)
674 goto end;
7972aab2 675 event->metadata_dumped = 1;
d0b96690
DG
676
677end:
678 return ret;
679}
680
681/*
682 * Should be called with session registry mutex held.
683 */
684int ust_metadata_channel_statedump(struct ust_registry_session *session,
685 struct ust_registry_channel *chan)
686{
687 int ret = 0;
688
689 /* Don't dump metadata events */
690 if (chan->chan_id == -1U)
691 return 0;
692
693 if (!chan->header_type)
694 return -EINVAL;
695
696 ret = lttng_metadata_printf(session,
697 "stream {\n"
698 " id = %u;\n"
699 " event.header := %s;\n"
700 " packet.context := struct packet_context;\n",
701 chan->chan_id,
702 chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ?
703 "struct event_header_compact" :
704 "struct event_header_large");
705 if (ret)
706 goto end;
707
708 if (chan->ctx_fields) {
709 ret = lttng_metadata_printf(session,
710 " event.context := struct {\n");
711 if (ret)
712 goto end;
713 }
714 ret = _lttng_context_metadata_statedump(session,
715 chan->nr_ctx_fields,
716 chan->ctx_fields);
717 if (ret)
718 goto end;
719 if (chan->ctx_fields) {
720 ret = lttng_metadata_printf(session,
721 " };\n");
722 if (ret)
723 goto end;
724 }
725
726 ret = lttng_metadata_printf(session,
727 "};\n\n");
7972aab2
DG
728 /* Flag success of metadata dump. */
729 chan->metadata_dumped = 1;
d0b96690
DG
730
731end:
732 return ret;
733}
734
735static
736int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
737{
738 return lttng_metadata_printf(session,
739 "struct packet_context {\n"
740 " uint64_clock_monotonic_t timestamp_begin;\n"
741 " uint64_clock_monotonic_t timestamp_end;\n"
742 " uint64_t content_size;\n"
743 " uint64_t packet_size;\n"
0793bcc6 744 " uint64_t packet_seq_num;\n"
d0b96690
DG
745 " unsigned long events_discarded;\n"
746 " uint32_t cpu_id;\n"
747 "};\n\n"
748 );
749}
750
751/*
752 * Compact header:
753 * id: range: 0 - 30.
754 * id 31 is reserved to indicate an extended header.
755 *
756 * Large header:
757 * id: range: 0 - 65534.
758 * id 65535 is reserved to indicate an extended header.
759 */
760static
761int _lttng_event_header_declare(struct ust_registry_session *session)
762{
763 return lttng_metadata_printf(session,
764 "struct event_header_compact {\n"
765 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
766 " variant <id> {\n"
767 " struct {\n"
768 " uint27_clock_monotonic_t timestamp;\n"
769 " } compact;\n"
770 " struct {\n"
771 " uint32_t id;\n"
772 " uint64_clock_monotonic_t timestamp;\n"
773 " } extended;\n"
774 " } v;\n"
775 "} align(%u);\n"
776 "\n"
777 "struct event_header_large {\n"
778 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
779 " variant <id> {\n"
780 " struct {\n"
781 " uint32_clock_monotonic_t timestamp;\n"
782 " } compact;\n"
783 " struct {\n"
784 " uint32_t id;\n"
785 " uint64_clock_monotonic_t timestamp;\n"
786 " } extended;\n"
787 " } v;\n"
788 "} align(%u);\n\n",
789 session->uint32_t_alignment,
790 session->uint16_t_alignment
791 );
792}
793
dc113ec7
MD
794/*
795 * The offset between monotonic and realtime clock can be negative if
796 * the system sets the REALTIME clock to 0 after boot.
dc113ec7 797 */
d0b96690 798static
8c645bb0 799int measure_single_clock_offset(struct offset_sample *sample)
d0b96690 800{
dc113ec7 801 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
fc0bb9fa 802 uint64_t tcf = trace_clock_freq();
d0b96690
DG
803 struct timespec rts = { 0, 0 };
804 int ret;
805
806 monotonic[0] = trace_clock_read64();
807 ret = clock_gettime(CLOCK_REALTIME, &rts);
8c645bb0
MD
808 if (ret < 0) {
809 return ret;
810 }
d0b96690 811 monotonic[1] = trace_clock_read64();
8c645bb0
MD
812 measure_delta = monotonic[1] - monotonic[0];
813 if (measure_delta > sample->measure_delta) {
814 /*
815 * Discard value if it took longer to read than the best
816 * sample so far.
817 */
818 return 0;
819 }
dc113ec7 820 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
6e1b0543
MD
821 realtime = (uint64_t) rts.tv_sec * tcf;
822 if (tcf == NSEC_PER_SEC) {
823 realtime += rts.tv_nsec;
824 } else {
825 realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC;
fc0bb9fa 826 }
c2636b57 827 sample->offset = (int64_t) realtime - monotonic_avg;
8c645bb0
MD
828 sample->measure_delta = measure_delta;
829 return 0;
d0b96690
DG
830}
831
8c645bb0
MD
832/*
833 * Approximation of NTP time of day to clock monotonic correlation,
834 * taken at start of trace. Keep the measurement that took the less time
835 * to complete, thus removing imprecision caused by preemption.
c2636b57 836 * May return a negative offset.
8c645bb0
MD
837 */
838static
c2636b57 839int64_t measure_clock_offset(void)
8c645bb0
MD
840{
841 int i;
842 struct offset_sample offset_best_sample = {
843 .offset = 0,
844 .measure_delta = UINT64_MAX,
845 };
846
847 for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) {
848 if (measure_single_clock_offset(&offset_best_sample)) {
849 return 0;
850 }
851 }
852 return offset_best_sample.offset;
853}
d0b96690
DG
854
855/*
856 * Should be called with session registry mutex held.
857 */
858int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf
MD
859 struct ust_app *app,
860 uint32_t major,
861 uint32_t minor)
d0b96690
DG
862{
863 unsigned char *uuid_c;
864 char uuid_s[UUID_STR_LEN],
865 clock_uuid_s[UUID_STR_LEN];
866 int ret = 0;
867 char hostname[HOST_NAME_MAX];
868
7972aab2 869 assert(session);
7972aab2 870
d0b96690
DG
871 uuid_c = session->uuid;
872
873 snprintf(uuid_s, sizeof(uuid_s),
874 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
875 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
876 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
877 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
878 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
879
d7ba1388
MD
880 /* For crash ABI */
881 ret = lttng_metadata_printf(session,
882 "/* CTF %u.%u */\n\n",
883 CTF_SPEC_MAJOR,
884 CTF_SPEC_MINOR);
885 if (ret) {
886 goto end;
887 }
888
d0b96690
DG
889 ret = lttng_metadata_printf(session,
890 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
891 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
892 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
893 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
894 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
895 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
896 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
897 "\n"
898 "trace {\n"
899 " major = %u;\n"
900 " minor = %u;\n"
901 " uuid = \"%s\";\n"
902 " byte_order = %s;\n"
903 " packet.header := struct {\n"
904 " uint32_t magic;\n"
905 " uint8_t uuid[16];\n"
906 " uint32_t stream_id;\n"
0793bcc6 907 " uint64_t stream_instance_id;\n"
d0b96690
DG
908 " };\n"
909 "};\n\n",
910 session->uint8_t_alignment,
911 session->uint16_t_alignment,
912 session->uint32_t_alignment,
913 session->uint64_t_alignment,
914 session->bits_per_long,
915 session->long_alignment,
916 CTF_SPEC_MAJOR,
917 CTF_SPEC_MINOR,
918 uuid_s,
919 session->byte_order == BIG_ENDIAN ? "be" : "le"
920 );
921 if (ret)
922 goto end;
923
924 /* ignore error, just use empty string if error. */
925 hostname[0] = '\0';
926 ret = gethostname(hostname, sizeof(hostname));
927 if (ret && errno == ENAMETOOLONG)
928 hostname[HOST_NAME_MAX - 1] = '\0';
929 ret = lttng_metadata_printf(session,
930 "env {\n"
931 " hostname = \"%s\";\n"
932 " domain = \"ust\";\n"
933 " tracer_name = \"lttng-ust\";\n"
934 " tracer_major = %u;\n"
af6142cf 935 " tracer_minor = %u;\n",
d0b96690 936 hostname,
af6142cf
MD
937 major,
938 minor
d0b96690
DG
939 );
940 if (ret)
941 goto end;
942
943 /*
944 * If per-application registry, we can output extra information
945 * about the application.
946 */
947 if (app) {
948 ret = lttng_metadata_printf(session,
af6142cf 949 " tracer_patchlevel = %u;\n"
d0b96690 950 " vpid = %d;\n"
d88aee68 951 " procname = \"%s\";\n",
af6142cf 952 app->version.patchlevel,
d0b96690
DG
953 (int) app->pid,
954 app->name
955 );
956 if (ret)
957 goto end;
958 }
959
960 ret = lttng_metadata_printf(session,
961 "};\n\n"
962 );
963 if (ret)
964 goto end;
965
966
967 ret = lttng_metadata_printf(session,
968 "clock {\n"
fc0bb9fa
MD
969 " name = \"%s\";\n",
970 trace_clock_name()
d0b96690
DG
971 );
972 if (ret)
973 goto end;
974
975 if (!trace_clock_uuid(clock_uuid_s)) {
976 ret = lttng_metadata_printf(session,
977 " uuid = \"%s\";\n",
978 clock_uuid_s
979 );
980 if (ret)
981 goto end;
982 }
983
984 ret = lttng_metadata_printf(session,
fc0bb9fa 985 " description = \"%s\";\n"
d0b96690
DG
986 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
987 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
c2636b57 988 " offset = %" PRId64 ";\n"
d0b96690 989 "};\n\n",
fc0bb9fa 990 trace_clock_description(),
d0b96690
DG
991 trace_clock_freq(),
992 measure_clock_offset()
993 );
994 if (ret)
995 goto end;
996
997 ret = lttng_metadata_printf(session,
998 "typealias integer {\n"
999 " size = 27; align = 1; signed = false;\n"
fc0bb9fa 1000 " map = clock.%s.value;\n"
d0b96690
DG
1001 "} := uint27_clock_monotonic_t;\n"
1002 "\n"
1003 "typealias integer {\n"
1004 " size = 32; align = %u; signed = false;\n"
fc0bb9fa 1005 " map = clock.%s.value;\n"
d0b96690
DG
1006 "} := uint32_clock_monotonic_t;\n"
1007 "\n"
1008 "typealias integer {\n"
1009 " size = 64; align = %u; signed = false;\n"
fc0bb9fa 1010 " map = clock.%s.value;\n"
d0b96690 1011 "} := uint64_clock_monotonic_t;\n\n",
fc0bb9fa 1012 trace_clock_name(),
d0b96690 1013 session->uint32_t_alignment,
fc0bb9fa
MD
1014 trace_clock_name(),
1015 session->uint64_t_alignment,
1016 trace_clock_name()
d0b96690
DG
1017 );
1018 if (ret)
1019 goto end;
1020
1021 ret = _lttng_stream_packet_context_declare(session);
1022 if (ret)
1023 goto end;
1024
1025 ret = _lttng_event_header_declare(session);
1026 if (ret)
1027 goto end;
1028
1029end:
1030 return ret;
1031}
This page took 0.084089 seconds and 4 git commands to generate.