Fix: sessiond: ust session is inactive during ust_app_global_update
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.c
CommitLineData
d0b96690 1/*
d0b96690
DG
2 * Copyright (C) 2010-2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
d0b96690 5 *
d0b96690
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
d0b96690
DG
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>
395d6b02 17#include <common/time.h>
d0b96690
DG
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
8c645bb0
MD
27#define NR_CLOCK_OFFSET_SAMPLES 10
28
29struct offset_sample {
c2636b57 30 int64_t offset; /* correlation offset */
8c645bb0
MD
31 uint64_t measure_delta; /* lower is better */
32};
33
da860cab
MD
34static
35int _lttng_field_statedump(struct ust_registry_session *session,
b623cb6a 36 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
da860cab
MD
37 size_t *iter_field, size_t nesting);
38
d0b96690
DG
39static inline
40int get_count_order(unsigned int count)
41{
42 int order;
43
78ddfe55 44 order = lttng_fls(count) - 1;
46b4dda6 45 if (count & (count - 1)) {
d0b96690 46 order++;
46b4dda6
JG
47 }
48 assert(order >= 0);
d0b96690
DG
49 return order;
50}
51
52/*
53 * Returns offset where to write in metadata array, or negative error value on error.
54 */
55static
56ssize_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
d7ba1388
MD
86static
87int 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
d0b96690
DG
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 */
109static
110int 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);
d7ba1388
MD
132 ret = metadata_file_append(session, str, len);
133 if (ret) {
134 PERROR("Error appending to metadata file");
135 goto end;
136 }
d0b96690
DG
137 DBG3("Append to metadata: \"%s\"", str);
138 ret = 0;
139
140end:
141 free(str);
142 return ret;
143}
144
da860cab
MD
145static
146int 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
a1f68b22
MD
161static
162void sanitize_ctf_identifier(char *out, const char *in)
163{
164 size_t i;
165
fc4b93fa 166 for (i = 0; i < LTTNG_UST_ABI_SYM_NAME_LEN; i++) {
a1f68b22
MD
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
8de88061
JR
179static
180int print_escaped_ctf_string(struct ust_registry_session *session, const char *string)
181{
35c829f8 182 int ret = 0;
8de88061
JR
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 }
212error:
213 return ret;
214}
215
10b56aef
MD
216/* Called with session registry mutex held. */
217static
218int ust_metadata_enum_statedump(struct ust_registry_session *session,
219 const char *enum_name,
220 uint64_t enum_id,
b623cb6a 221 const struct lttng_ust_ctl_integer_type *container_type,
da860cab 222 const char *field_name, size_t *iter_field, size_t nesting)
10b56aef
MD
223{
224 struct ust_registry_enum *reg_enum;
b623cb6a 225 const struct lttng_ust_ctl_enum_entry *entries;
10b56aef
MD
226 size_t nr_entries;
227 int ret = 0;
228 size_t i;
fc4b93fa 229 char identifier[LTTNG_UST_ABI_SYM_NAME_LEN];
10b56aef
MD
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
da860cab
MD
242 ret = print_tabs(session, nesting);
243 if (ret) {
244 goto end;
245 }
10b56aef 246 ret = lttng_metadata_printf(session,
da860cab 247 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
10b56aef
MD
248 container_type->size,
249 container_type->alignment,
250 container_type->signedness,
b623cb6a 251 (container_type->encoding == lttng_ust_ctl_encode_none)
10b56aef 252 ? "none"
b623cb6a 253 : (container_type->encoding == lttng_ust_ctl_encode_UTF8)
10b56aef
MD
254 ? "UTF8"
255 : "ASCII",
256 container_type->base);
257 if (ret) {
258 goto end;
259 }
1ddb0e8a 260 nesting++;
10b56aef
MD
261 /* Dump all entries */
262 for (i = 0; i < nr_entries; i++) {
b623cb6a 263 const struct lttng_ust_ctl_enum_entry *entry = &entries[i];
10b56aef
MD
264 int j, len;
265
a1f68b22
MD
266 ret = print_tabs(session, nesting);
267 if (ret) {
268 goto end;
269 }
10b56aef 270 ret = lttng_metadata_printf(session,
a1f68b22 271 "\"");
10b56aef
MD
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 }
e85e3723 298 ret = lttng_metadata_printf(session, "\"");
10b56aef
MD
299 if (ret) {
300 goto end;
301 }
3b016e58 302
9d27cec7 303 if (entry->u.extra.options &
b623cb6a 304 LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
e85e3723
PP
305 ret = lttng_metadata_printf(session, ",\n");
306 if (ret) {
307 goto end;
308 }
10b56aef
MD
309 } else {
310 ret = lttng_metadata_printf(session,
e85e3723
PP
311 " = ");
312 if (ret) {
313 goto end;
314 }
3b016e58 315
e85e3723 316 if (entry->start.signedness) {
3b016e58 317 ret = lttng_metadata_printf(session,
e85e3723 318 "%lld", (long long) entry->start.value);
3b016e58
MD
319 } else {
320 ret = lttng_metadata_printf(session,
e85e3723
PP
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;
3b016e58 344 }
10b56aef
MD
345 }
346 }
1ddb0e8a 347 nesting--;
a1f68b22
MD
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);
da860cab
MD
355end:
356 (*iter_field)++;
357 return ret;
358}
359
da860cab
MD
360static
361int _lttng_variant_statedump(struct ust_registry_session *session,
0d32d1a9
MD
362 uint32_t nr_choices, const char *tag_name,
363 uint32_t alignment,
b623cb6a 364 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
da860cab
MD
365 size_t *iter_field, size_t nesting)
366{
b623cb6a 367 const struct lttng_ust_ctl_field *variant = &fields[*iter_field];
0d32d1a9 368 uint32_t i;
da860cab 369 int ret;
fc4b93fa 370 char identifier[LTTNG_UST_ABI_SYM_NAME_LEN];
da860cab 371
b623cb6a 372 if (variant->type.atype != lttng_ust_ctl_atype_variant) {
da860cab
MD
373 ret = -EINVAL;
374 goto end;
375 }
da860cab 376 (*iter_field)++;
0d32d1a9
MD
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 }
a1f68b22
MD
391 ret = print_tabs(session, nesting);
392 if (ret) {
393 goto end;
394 }
da860cab 395 ret = lttng_metadata_printf(session,
a1f68b22
MD
396 "variant <_%s> {\n",
397 identifier);
da860cab
MD
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);
dc6403f3
JG
410 if (ret) {
411 goto end;
412 }
da860cab 413 }
a1f68b22
MD
414 sanitize_ctf_identifier(identifier, variant->name);
415 ret = print_tabs(session, nesting);
850c5d4a
JR
416 if (ret) {
417 goto end;
418 }
da860cab 419 ret = lttng_metadata_printf(session,
a1f68b22
MD
420 "} _%s;\n",
421 identifier);
da860cab
MD
422 if (ret) {
423 goto end;
424 }
10b56aef
MD
425end:
426 return ret;
427}
428
d0b96690
DG
429static
430int _lttng_field_statedump(struct ust_registry_session *session,
b623cb6a 431 const struct lttng_ust_ctl_field *fields, size_t nr_fields,
da860cab 432 size_t *iter_field, size_t nesting)
d0b96690
DG
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;
b623cb6a 439 const struct lttng_ust_ctl_field *field;
d0b96690 440
da860cab
MD
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) {
d0b96690 448 bo_reverse = bo_le;
da860cab 449 } else {
d0b96690 450 bo_reverse = bo_be;
da860cab 451 }
d0b96690
DG
452
453 switch (field->type.atype) {
b623cb6a 454 case lttng_ust_ctl_atype_integer:
da860cab
MD
455 ret = print_tabs(session, nesting);
456 if (ret) {
457 goto end;
458 }
d0b96690 459 ret = lttng_metadata_printf(session,
da860cab 460 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
0d32d1a9
MD
461 field->type.u.integer.size,
462 field->type.u.integer.alignment,
463 field->type.u.integer.signedness,
b623cb6a 464 (field->type.u.integer.encoding == lttng_ust_ctl_encode_none)
d0b96690 465 ? "none"
b623cb6a 466 : (field->type.u.integer.encoding == lttng_ust_ctl_encode_UTF8)
d0b96690
DG
467 ? "UTF8"
468 : "ASCII",
0d32d1a9
MD
469 field->type.u.integer.base,
470 field->type.u.integer.reverse_byte_order ? bo_reverse : bo_native,
d0b96690 471 field->name);
da860cab 472 (*iter_field)++;
d0b96690 473 break;
b623cb6a 474 case lttng_ust_ctl_atype_enum:
10b56aef 475 ret = ust_metadata_enum_statedump(session,
0d32d1a9
MD
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,
da860cab 479 field->name, iter_field, nesting);
10b56aef 480 break;
b623cb6a 481 case lttng_ust_ctl_atype_float:
da860cab
MD
482 ret = print_tabs(session, nesting);
483 if (ret) {
484 goto end;
485 }
d0b96690 486 ret = lttng_metadata_printf(session,
da860cab 487 "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
0d32d1a9
MD
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,
d0b96690 492 field->name);
da860cab 493 (*iter_field)++;
d0b96690 494 break;
b623cb6a 495 case lttng_ust_ctl_atype_array:
d0b96690 496 {
b623cb6a 497 const struct lttng_ust_ctl_basic_type *elem_type;
d0b96690 498
da860cab
MD
499 ret = print_tabs(session, nesting);
500 if (ret) {
501 goto end;
502 }
0d32d1a9
MD
503 elem_type = &field->type.u.legacy.array.elem_type;
504 /* Only integers are currently supported in arrays. */
b623cb6a 505 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
0d32d1a9
MD
506 ret = -EINVAL;
507 goto end;
508 }
d0b96690 509 ret = lttng_metadata_printf(session,
da860cab 510 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
d0b96690
DG
511 elem_type->u.basic.integer.size,
512 elem_type->u.basic.integer.alignment,
513 elem_type->u.basic.integer.signedness,
b623cb6a 514 (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
d0b96690 515 ? "none"
b623cb6a 516 : (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
d0b96690
DG
517 ? "UTF8"
518 : "ASCII",
519 elem_type->u.basic.integer.base,
520 elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native,
0d32d1a9
MD
521 field->name, field->type.u.legacy.array.length);
522 (*iter_field)++;
523 break;
524 }
b623cb6a 525 case lttng_ust_ctl_atype_array_nestable:
0d32d1a9
MD
526 {
527 uint32_t array_length;
b623cb6a
MJ
528 const struct lttng_ust_ctl_field *array_nestable;
529 const struct lttng_ust_ctl_type *elem_type;
0d32d1a9
MD
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. */
b623cb6a 542 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
0d32d1a9
MD
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,
b623cb6a 570 (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none)
0d32d1a9 571 ? "none"
b623cb6a 572 : (elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8)
0d32d1a9
MD
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);
da860cab 578 (*iter_field)++;
d0b96690
DG
579 break;
580 }
b623cb6a 581 case lttng_ust_ctl_atype_sequence:
d0b96690 582 {
b623cb6a
MJ
583 const struct lttng_ust_ctl_basic_type *elem_type;
584 const struct lttng_ust_ctl_basic_type *length_type;
d0b96690 585
0d32d1a9
MD
586 elem_type = &field->type.u.legacy.sequence.elem_type;
587 length_type = &field->type.u.legacy.sequence.length_type;
da860cab
MD
588 ret = print_tabs(session, nesting);
589 if (ret) {
590 goto end;
591 }
0d32d1a9
MD
592
593 /* Only integers are currently supported in sequences. */
b623cb6a 594 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
0d32d1a9
MD
595 ret = -EINVAL;
596 goto end;
597 }
598
d0b96690 599 ret = lttng_metadata_printf(session,
da860cab 600 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
d0b96690
DG
601 length_type->u.basic.integer.size,
602 (unsigned int) length_type->u.basic.integer.alignment,
603 length_type->u.basic.integer.signedness,
b623cb6a 604 (length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
d0b96690 605 ? "none"
b623cb6a 606 : ((length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
d0b96690
DG
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);
da860cab
MD
612 if (ret) {
613 goto end;
614 }
d0b96690 615
da860cab
MD
616 ret = print_tabs(session, nesting);
617 if (ret) {
618 goto end;
619 }
d0b96690 620 ret = lttng_metadata_printf(session,
da860cab 621 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
d0b96690
DG
622 elem_type->u.basic.integer.size,
623 (unsigned int) elem_type->u.basic.integer.alignment,
624 elem_type->u.basic.integer.signedness,
b623cb6a 625 (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none)
d0b96690 626 ? "none"
b623cb6a 627 : ((elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8)
d0b96690
DG
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);
da860cab 634 (*iter_field)++;
d0b96690
DG
635 break;
636 }
b623cb6a 637 case lttng_ust_ctl_atype_sequence_nestable:
0d32d1a9 638 {
b623cb6a
MJ
639 const struct lttng_ust_ctl_field *sequence_nestable;
640 const struct lttng_ust_ctl_type *elem_type;
0d32d1a9
MD
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. */
b623cb6a 651 if (elem_type->atype != lttng_ust_ctl_atype_integer) {
0d32d1a9
MD
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 }
d0b96690 669
0d32d1a9
MD
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,
b623cb6a 679 (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none)
0d32d1a9 680 ? "none"
b623cb6a 681 : ((elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8)
0d32d1a9
MD
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 }
b623cb6a 691 case lttng_ust_ctl_atype_string:
d0b96690 692 /* Default encoding is UTF8 */
da860cab
MD
693 ret = print_tabs(session, nesting);
694 if (ret) {
695 goto end;
696 }
d0b96690 697 ret = lttng_metadata_printf(session,
da860cab 698 "string%s _%s;\n",
b623cb6a 699 field->type.u.string.encoding == lttng_ust_ctl_encode_ASCII ?
d0b96690
DG
700 " { encoding = ASCII; }" : "",
701 field->name);
da860cab
MD
702 (*iter_field)++;
703 break;
b623cb6a 704 case lttng_ust_ctl_atype_variant:
0d32d1a9
MD
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;
b623cb6a 714 case lttng_ust_ctl_atype_variant_nestable:
0d32d1a9
MD
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);
da860cab
MD
720 if (ret) {
721 goto end;
722 }
723 break;
b623cb6a 724 case lttng_ust_ctl_atype_struct:
0d32d1a9
MD
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 }
da860cab
MD
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)++;
d0b96690 738 break;
b623cb6a 739 case lttng_ust_ctl_atype_struct_nestable:
0d32d1a9
MD
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;
b623cb6a 764 case lttng_ust_ctl_atype_enum_nestable:
0d32d1a9 765 {
b623cb6a
MJ
766 const struct lttng_ust_ctl_field *container_field;
767 const struct lttng_ust_ctl_type *container_type;
0d32d1a9
MD
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. */
b623cb6a 778 if (container_type->atype != lttng_ust_ctl_atype_integer) {
0d32d1a9
MD
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 }
d0b96690 789 default:
da860cab 790 ret = -EINVAL;
d0b96690 791 }
da860cab 792end:
d0b96690
DG
793 return ret;
794}
795
796static
797int _lttng_context_metadata_statedump(struct ust_registry_session *session,
798 size_t nr_ctx_fields,
b623cb6a 799 struct lttng_ust_ctl_field *ctx)
d0b96690
DG
800{
801 int ret = 0;
da860cab 802 size_t i = 0;
d0b96690
DG
803
804 if (!ctx)
805 return 0;
da860cab
MD
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 }
d0b96690
DG
815 }
816 return ret;
817}
818
819static
820int _lttng_fields_metadata_statedump(struct ust_registry_session *session,
821 struct ust_registry_event *event)
822{
823 int ret = 0;
da860cab 824 size_t i = 0;
d0b96690 825
da860cab
MD
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 }
d0b96690
DG
835 }
836 return ret;
837}
838
839/*
840 * Should be called with session registry mutex held.
841 */
842int 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);
0d32d1a9 860 if (ret) {
d0b96690 861 goto end;
0d32d1a9 862 }
d0b96690
DG
863
864 ret = lttng_metadata_printf(session,
865 " loglevel = %d;\n",
2106efa0 866 event->loglevel_value);
0d32d1a9 867 if (ret) {
d0b96690 868 goto end;
0d32d1a9 869 }
d0b96690
DG
870
871 if (event->model_emf_uri) {
872 ret = lttng_metadata_printf(session,
873 " model.emf.uri = \"%s\";\n",
874 event->model_emf_uri);
0d32d1a9 875 if (ret) {
d0b96690 876 goto end;
0d32d1a9 877 }
d0b96690
DG
878 }
879
d0b96690
DG
880 ret = lttng_metadata_printf(session,
881 " fields := struct {\n"
882 );
0d32d1a9 883 if (ret) {
d0b96690 884 goto end;
0d32d1a9 885 }
d0b96690
DG
886
887 ret = _lttng_fields_metadata_statedump(session, event);
0d32d1a9 888 if (ret) {
d0b96690 889 goto end;
0d32d1a9 890 }
d0b96690
DG
891
892 ret = lttng_metadata_printf(session,
893 " };\n"
894 "};\n\n");
0d32d1a9 895 if (ret) {
d0b96690 896 goto end;
0d32d1a9 897 }
7972aab2 898 event->metadata_dumped = 1;
d0b96690
DG
899
900end:
901 return ret;
902}
903
904/*
905 * Should be called with session registry mutex held.
906 */
907int 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,
b623cb6a 925 chan->header_type == LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT ?
d0b96690
DG
926 "struct event_header_compact" :
927 "struct event_header_large");
0d32d1a9 928 if (ret) {
d0b96690 929 goto end;
0d32d1a9 930 }
d0b96690
DG
931
932 if (chan->ctx_fields) {
933 ret = lttng_metadata_printf(session,
934 " event.context := struct {\n");
0d32d1a9 935 if (ret) {
d0b96690 936 goto end;
0d32d1a9 937 }
d0b96690
DG
938 }
939 ret = _lttng_context_metadata_statedump(session,
940 chan->nr_ctx_fields,
941 chan->ctx_fields);
0d32d1a9 942 if (ret) {
d0b96690 943 goto end;
0d32d1a9 944 }
d0b96690
DG
945 if (chan->ctx_fields) {
946 ret = lttng_metadata_printf(session,
947 " };\n");
0d32d1a9 948 if (ret) {
d0b96690 949 goto end;
0d32d1a9 950 }
d0b96690
DG
951 }
952
953 ret = lttng_metadata_printf(session,
954 "};\n\n");
7972aab2
DG
955 /* Flag success of metadata dump. */
956 chan->metadata_dumped = 1;
d0b96690
DG
957
958end:
959 return ret;
960}
961
962static
963int _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"
0793bcc6 971 " uint64_t packet_seq_num;\n"
d0b96690
DG
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 */
987static
988int _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
dc113ec7
MD
1021/*
1022 * The offset between monotonic and realtime clock can be negative if
1023 * the system sets the REALTIME clock to 0 after boot.
dc113ec7 1024 */
d0b96690 1025static
8c645bb0 1026int measure_single_clock_offset(struct offset_sample *sample)
d0b96690 1027{
dc113ec7 1028 uint64_t monotonic_avg, monotonic[2], measure_delta, realtime;
fc0bb9fa 1029 uint64_t tcf = trace_clock_freq();
d0b96690
DG
1030 struct timespec rts = { 0, 0 };
1031 int ret;
1032
1033 monotonic[0] = trace_clock_read64();
389fbf04 1034 ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
8c645bb0
MD
1035 if (ret < 0) {
1036 return ret;
1037 }
d0b96690 1038 monotonic[1] = trace_clock_read64();
8c645bb0
MD
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 }
dc113ec7 1047 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
6e1b0543
MD
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;
fc0bb9fa 1053 }
c2636b57 1054 sample->offset = (int64_t) realtime - monotonic_avg;
8c645bb0
MD
1055 sample->measure_delta = measure_delta;
1056 return 0;
d0b96690
DG
1057}
1058
8c645bb0
MD
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.
c2636b57 1063 * May return a negative offset.
8c645bb0
MD
1064 */
1065static
c2636b57 1066int64_t measure_clock_offset(void)
8c645bb0
MD
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}
d0b96690 1081
8de88061
JR
1082static
1083int 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
1136error:
1137 if (session) {
1138 session_put(session);
1139 }
1140 rcu_read_unlock();
1141 return ret;
1142}
1143
1144static
1145int 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
1170end:
1171 return ret;
1172}
1173
d0b96690
DG
1174/*
1175 * Should be called with session registry mutex held.
1176 */
1177int ust_metadata_session_statedump(struct ust_registry_session *session,
af6142cf
MD
1178 struct ust_app *app,
1179 uint32_t major,
1180 uint32_t minor)
d0b96690 1181{
c70636a7
MJ
1182 char uuid_s[LTTNG_UUID_STR_LEN],
1183 clock_uuid_s[LTTNG_UUID_STR_LEN];
d0b96690 1184 int ret = 0;
d0b96690 1185
7972aab2 1186 assert(session);
7972aab2 1187
c8e0c5f5 1188 lttng_uuid_to_str(session->uuid, uuid_s);
d0b96690 1189
d7ba1388
MD
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
d0b96690
DG
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"
0793bcc6 1217 " uint64_t stream_instance_id;\n"
d0b96690
DG
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 );
0d32d1a9 1231 if (ret) {
d0b96690 1232 goto end;
0d32d1a9 1233 }
d0b96690 1234
d0b96690
DG
1235 ret = lttng_metadata_printf(session,
1236 "env {\n"
d0b96690
DG
1237 " domain = \"ust\";\n"
1238 " tracer_name = \"lttng-ust\";\n"
1239 " tracer_major = %u;\n"
8de88061
JR
1240 " tracer_minor = %u;\n"
1241 " tracer_buffering_scheme = \"%s\";\n"
1242 " tracer_buffering_id = %u;\n"
1243 " architecture_bit_width = %u;\n",
af6142cf 1244 major,
8de88061
JR
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) {
d0b96690 1255 goto end;
8de88061 1256 }
d0b96690
DG
1257
1258 /*
1259 * If per-application registry, we can output extra information
1260 * about the application.
1261 */
8de88061
JR
1262 ret = print_metadata_app_information(session, app);
1263 if (ret) {
1264 goto end;
d0b96690
DG
1265 }
1266
1267 ret = lttng_metadata_printf(session,
1268 "};\n\n"
1269 );
0d32d1a9 1270 if (ret) {
d0b96690 1271 goto end;
0d32d1a9 1272 }
d0b96690
DG
1273
1274 ret = lttng_metadata_printf(session,
1275 "clock {\n"
fc0bb9fa
MD
1276 " name = \"%s\";\n",
1277 trace_clock_name()
d0b96690 1278 );
0d32d1a9 1279 if (ret) {
d0b96690 1280 goto end;
0d32d1a9 1281 }
d0b96690
DG
1282
1283 if (!trace_clock_uuid(clock_uuid_s)) {
1284 ret = lttng_metadata_printf(session,
1285 " uuid = \"%s\";\n",
1286 clock_uuid_s
1287 );
0d32d1a9 1288 if (ret) {
d0b96690 1289 goto end;
0d32d1a9 1290 }
d0b96690
DG
1291 }
1292
1293 ret = lttng_metadata_printf(session,
fc0bb9fa 1294 " description = \"%s\";\n"
d0b96690
DG
1295 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1296 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
c2636b57 1297 " offset = %" PRId64 ";\n"
d0b96690 1298 "};\n\n",
fc0bb9fa 1299 trace_clock_description(),
d0b96690
DG
1300 trace_clock_freq(),
1301 measure_clock_offset()
1302 );
0d32d1a9 1303 if (ret) {
d0b96690 1304 goto end;
0d32d1a9 1305 }
d0b96690
DG
1306
1307 ret = lttng_metadata_printf(session,
1308 "typealias integer {\n"
1309 " size = 27; align = 1; signed = false;\n"
fc0bb9fa 1310 " map = clock.%s.value;\n"
d0b96690
DG
1311 "} := uint27_clock_monotonic_t;\n"
1312 "\n"
1313 "typealias integer {\n"
1314 " size = 32; align = %u; signed = false;\n"
fc0bb9fa 1315 " map = clock.%s.value;\n"
d0b96690
DG
1316 "} := uint32_clock_monotonic_t;\n"
1317 "\n"
1318 "typealias integer {\n"
1319 " size = 64; align = %u; signed = false;\n"
fc0bb9fa 1320 " map = clock.%s.value;\n"
d0b96690 1321 "} := uint64_clock_monotonic_t;\n\n",
fc0bb9fa 1322 trace_clock_name(),
d0b96690 1323 session->uint32_t_alignment,
fc0bb9fa
MD
1324 trace_clock_name(),
1325 session->uint64_t_alignment,
1326 trace_clock_name()
d0b96690 1327 );
0d32d1a9 1328 if (ret) {
d0b96690 1329 goto end;
0d32d1a9 1330 }
d0b96690
DG
1331
1332 ret = _lttng_stream_packet_context_declare(session);
0d32d1a9 1333 if (ret) {
d0b96690 1334 goto end;
0d32d1a9 1335 }
d0b96690
DG
1336
1337 ret = _lttng_event_header_declare(session);
0d32d1a9 1338 if (ret) {
d0b96690 1339 goto end;
0d32d1a9 1340 }
d0b96690
DG
1341
1342end:
1343 return ret;
1344}
This page took 0.124327 seconds and 4 git commands to generate.