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