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