Commit | Line | Data |
---|---|---|
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> | |
c9e313bc SM |
16 | #include <common/common.hpp> |
17 | #include <common/time.hpp> | |
bacc39bb | 18 | #include <vector> |
d0b96690 | 19 | |
c9e313bc SM |
20 | #include "ust-registry.hpp" |
21 | #include "ust-clock.hpp" | |
22 | #include "ust-app.hpp" | |
d0b96690 | 23 | |
8c645bb0 MD |
24 | #define NR_CLOCK_OFFSET_SAMPLES 10 |
25 | ||
f1494934 | 26 | namespace { |
8c645bb0 | 27 | struct offset_sample { |
f1494934 JG |
28 | /* correlation offset */ |
29 | int64_t offset; | |
30 | /* lower is better */ | |
31 | uint64_t measure_delta; | |
8c645bb0 | 32 | }; |
f1494934 | 33 | } /* namespace */ |
8c645bb0 | 34 | |
da860cab MD |
35 | static |
36 | int _lttng_field_statedump(struct ust_registry_session *session, | |
b623cb6a | 37 | const struct lttng_ust_ctl_field *fields, size_t nr_fields, |
da860cab MD |
38 | size_t *iter_field, size_t nesting); |
39 | ||
d0b96690 DG |
40 | static inline |
41 | int get_count_order(unsigned int count) | |
42 | { | |
43 | int order; | |
44 | ||
78ddfe55 | 45 | order = lttng_fls(count) - 1; |
46b4dda6 | 46 | if (count & (count - 1)) { |
d0b96690 | 47 | order++; |
46b4dda6 | 48 | } |
a0377dfe | 49 | LTTNG_ASSERT(order >= 0); |
d0b96690 DG |
50 | return order; |
51 | } | |
52 | ||
53 | /* | |
54 | * Returns offset where to write in metadata array, or negative error value on error. | |
55 | */ | |
56 | static | |
57 | ssize_t metadata_reserve(struct ust_registry_session *session, size_t len) | |
58 | { | |
59 | size_t new_len = session->metadata_len + len; | |
60 | size_t new_alloc_len = new_len; | |
61 | size_t old_alloc_len = session->metadata_alloc_len; | |
62 | ssize_t ret; | |
63 | ||
64 | if (new_alloc_len > (UINT32_MAX >> 1)) | |
65 | return -EINVAL; | |
66 | if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) | |
67 | return -EINVAL; | |
68 | ||
69 | if (new_alloc_len > old_alloc_len) { | |
70 | char *newptr; | |
71 | ||
72 | new_alloc_len = | |
8957eb48 | 73 | std::max<size_t>(1U << get_count_order(new_alloc_len), old_alloc_len << 1); |
7966af57 | 74 | newptr = (char *) realloc(session->metadata, new_alloc_len); |
d0b96690 DG |
75 | if (!newptr) |
76 | return -ENOMEM; | |
77 | session->metadata = newptr; | |
78 | /* We zero directly the memory from start of allocation. */ | |
79 | memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len); | |
80 | session->metadata_alloc_len = new_alloc_len; | |
81 | } | |
82 | ret = session->metadata_len; | |
83 | session->metadata_len += len; | |
84 | return ret; | |
85 | } | |
86 | ||
d7ba1388 MD |
87 | static |
88 | int metadata_file_append(struct ust_registry_session *session, | |
89 | const char *str, size_t len) | |
90 | { | |
91 | ssize_t written; | |
92 | ||
93 | if (session->metadata_fd < 0) { | |
94 | return 0; | |
95 | } | |
96 | /* Write to metadata file */ | |
97 | written = lttng_write(session->metadata_fd, str, len); | |
98 | if (written != len) { | |
99 | return -1; | |
100 | } | |
101 | return 0; | |
102 | } | |
103 | ||
d0b96690 DG |
104 | /* |
105 | * We have exclusive access to our metadata buffer (protected by the | |
106 | * ust_lock), so we can do racy operations such as looking for | |
107 | * remaining space left in packet and write, since mutual exclusion | |
108 | * protects us from concurrent writes. | |
109 | */ | |
d22ad5f8 | 110 | static ATTR_FORMAT_PRINTF(2, 3) |
d0b96690 DG |
111 | int lttng_metadata_printf(struct ust_registry_session *session, |
112 | const char *fmt, ...) | |
113 | { | |
114 | char *str = NULL; | |
115 | size_t len; | |
116 | va_list ap; | |
117 | ssize_t offset; | |
118 | int ret; | |
119 | ||
120 | va_start(ap, fmt); | |
121 | ret = vasprintf(&str, fmt, ap); | |
122 | va_end(ap); | |
123 | if (ret < 0) | |
124 | return -ENOMEM; | |
125 | ||
126 | len = strlen(str); | |
127 | offset = metadata_reserve(session, len); | |
128 | if (offset < 0) { | |
129 | ret = offset; | |
130 | goto end; | |
131 | } | |
132 | memcpy(&session->metadata[offset], str, len); | |
d7ba1388 MD |
133 | ret = metadata_file_append(session, str, len); |
134 | if (ret) { | |
135 | PERROR("Error appending to metadata file"); | |
136 | goto end; | |
137 | } | |
d0b96690 DG |
138 | DBG3("Append to metadata: \"%s\"", str); |
139 | ret = 0; | |
140 | ||
141 | end: | |
142 | free(str); | |
143 | return ret; | |
144 | } | |
145 | ||
da860cab MD |
146 | static |
147 | int print_tabs(struct ust_registry_session *session, size_t nesting) | |
148 | { | |
149 | size_t i; | |
150 | ||
151 | for (i = 0; i < nesting; i++) { | |
152 | int ret; | |
153 | ||
154 | ret = lttng_metadata_printf(session, " "); | |
155 | if (ret) { | |
156 | return ret; | |
157 | } | |
158 | } | |
159 | return 0; | |
160 | } | |
161 | ||
a1f68b22 MD |
162 | static |
163 | void sanitize_ctf_identifier(char *out, const char *in) | |
164 | { | |
165 | size_t i; | |
166 | ||
fc4b93fa | 167 | for (i = 0; i < LTTNG_UST_ABI_SYM_NAME_LEN; i++) { |
a1f68b22 MD |
168 | switch (in[i]) { |
169 | case '.': | |
170 | case '$': | |
171 | case ':': | |
172 | out[i] = '_'; | |
173 | break; | |
174 | default: | |
175 | out[i] = in[i]; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
8de88061 JR |
180 | static |
181 | int print_escaped_ctf_string(struct ust_registry_session *session, const char *string) | |
182 | { | |
35c829f8 | 183 | int ret = 0; |
8de88061 JR |
184 | size_t i; |
185 | char cur; | |
186 | ||
187 | i = 0; | |
188 | cur = string[i]; | |
189 | while (cur != '\0') { | |
190 | switch (cur) { | |
191 | case '\n': | |
192 | ret = lttng_metadata_printf(session, "%s", "\\n"); | |
193 | break; | |
194 | case '\\': | |
195 | case '"': | |
196 | ret = lttng_metadata_printf(session, "%c", '\\'); | |
197 | if (ret) { | |
198 | goto error; | |
199 | } | |
200 | /* We still print the current char */ | |
201 | /* Fallthrough */ | |
202 | default: | |
203 | ret = lttng_metadata_printf(session, "%c", cur); | |
204 | break; | |
205 | } | |
206 | ||
207 | if (ret) { | |
208 | goto error; | |
209 | } | |
210 | ||
211 | cur = string[++i]; | |
212 | } | |
213 | error: | |
214 | return ret; | |
215 | } | |
216 | ||
10b56aef MD |
217 | /* Called with session registry mutex held. */ |
218 | static | |
219 | int ust_metadata_enum_statedump(struct ust_registry_session *session, | |
220 | const char *enum_name, | |
221 | uint64_t enum_id, | |
b623cb6a | 222 | const struct lttng_ust_ctl_integer_type *container_type, |
da860cab | 223 | const char *field_name, size_t *iter_field, size_t nesting) |
10b56aef MD |
224 | { |
225 | struct ust_registry_enum *reg_enum; | |
b623cb6a | 226 | const struct lttng_ust_ctl_enum_entry *entries; |
10b56aef MD |
227 | size_t nr_entries; |
228 | int ret = 0; | |
229 | size_t i; | |
fc4b93fa | 230 | char identifier[LTTNG_UST_ABI_SYM_NAME_LEN]; |
10b56aef MD |
231 | |
232 | rcu_read_lock(); | |
233 | reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id); | |
234 | rcu_read_unlock(); | |
235 | /* reg_enum can still be used because session registry mutex is held. */ | |
236 | if (!reg_enum) { | |
237 | ret = -ENOENT; | |
238 | goto end; | |
239 | } | |
240 | entries = reg_enum->entries; | |
241 | nr_entries = reg_enum->nr_entries; | |
242 | ||
da860cab MD |
243 | ret = print_tabs(session, nesting); |
244 | if (ret) { | |
245 | goto end; | |
246 | } | |
10b56aef | 247 | ret = lttng_metadata_printf(session, |
da860cab | 248 | "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n", |
10b56aef MD |
249 | container_type->size, |
250 | container_type->alignment, | |
251 | container_type->signedness, | |
b623cb6a | 252 | (container_type->encoding == lttng_ust_ctl_encode_none) |
10b56aef | 253 | ? "none" |
b623cb6a | 254 | : (container_type->encoding == lttng_ust_ctl_encode_UTF8) |
10b56aef MD |
255 | ? "UTF8" |
256 | : "ASCII", | |
257 | container_type->base); | |
258 | if (ret) { | |
259 | goto end; | |
260 | } | |
1ddb0e8a | 261 | nesting++; |
10b56aef MD |
262 | /* Dump all entries */ |
263 | for (i = 0; i < nr_entries; i++) { | |
b623cb6a | 264 | const struct lttng_ust_ctl_enum_entry *entry = &entries[i]; |
10b56aef MD |
265 | int j, len; |
266 | ||
a1f68b22 MD |
267 | ret = print_tabs(session, nesting); |
268 | if (ret) { | |
269 | goto end; | |
270 | } | |
10b56aef | 271 | ret = lttng_metadata_printf(session, |
a1f68b22 | 272 | "\""); |
10b56aef MD |
273 | if (ret) { |
274 | goto end; | |
275 | } | |
276 | len = strlen(entry->string); | |
277 | /* Escape the character '"' */ | |
278 | for (j = 0; j < len; j++) { | |
279 | char c = entry->string[j]; | |
280 | ||
281 | switch (c) { | |
282 | case '"': | |
283 | ret = lttng_metadata_printf(session, | |
284 | "\\\""); | |
285 | break; | |
286 | case '\\': | |
287 | ret = lttng_metadata_printf(session, | |
288 | "\\\\"); | |
289 | break; | |
290 | default: | |
291 | ret = lttng_metadata_printf(session, | |
292 | "%c", c); | |
293 | break; | |
294 | } | |
295 | if (ret) { | |
296 | goto end; | |
297 | } | |
298 | } | |
e85e3723 | 299 | ret = lttng_metadata_printf(session, "\""); |
10b56aef MD |
300 | if (ret) { |
301 | goto end; | |
302 | } | |
3b016e58 | 303 | |
9d27cec7 | 304 | if (entry->u.extra.options & |
b623cb6a | 305 | LTTNG_UST_CTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) { |
e85e3723 PP |
306 | ret = lttng_metadata_printf(session, ",\n"); |
307 | if (ret) { | |
308 | goto end; | |
309 | } | |
10b56aef MD |
310 | } else { |
311 | ret = lttng_metadata_printf(session, | |
e85e3723 PP |
312 | " = "); |
313 | if (ret) { | |
314 | goto end; | |
315 | } | |
3b016e58 | 316 | |
e85e3723 | 317 | if (entry->start.signedness) { |
3b016e58 | 318 | ret = lttng_metadata_printf(session, |
d22ad5f8 | 319 | "%" PRId64, entry->start.value); |
3b016e58 MD |
320 | } else { |
321 | ret = lttng_metadata_printf(session, | |
d22ad5f8 | 322 | "%" PRIu64, entry->start.value); |
e85e3723 PP |
323 | } |
324 | if (ret) { | |
325 | goto end; | |
326 | } | |
327 | ||
328 | if (entry->start.signedness == entry->end.signedness && | |
329 | entry->start.value == | |
330 | entry->end.value) { | |
331 | ret = lttng_metadata_printf(session, ",\n"); | |
332 | } else { | |
333 | if (entry->end.signedness) { | |
334 | ret = lttng_metadata_printf(session, | |
d22ad5f8 SM |
335 | " ... %" PRId64 ",\n", |
336 | entry->end.value); | |
e85e3723 PP |
337 | } else { |
338 | ret = lttng_metadata_printf(session, | |
d22ad5f8 | 339 | " ... %" PRIu64 ",\n", |
e85e3723 PP |
340 | entry->end.value); |
341 | } | |
342 | } | |
343 | if (ret) { | |
344 | goto end; | |
3b016e58 | 345 | } |
10b56aef MD |
346 | } |
347 | } | |
1ddb0e8a | 348 | nesting--; |
a1f68b22 MD |
349 | sanitize_ctf_identifier(identifier, field_name); |
350 | ret = print_tabs(session, nesting); | |
351 | if (ret) { | |
352 | goto end; | |
353 | } | |
354 | ret = lttng_metadata_printf(session, "} _%s;\n", | |
355 | identifier); | |
da860cab MD |
356 | end: |
357 | (*iter_field)++; | |
358 | return ret; | |
359 | } | |
360 | ||
da860cab MD |
361 | static |
362 | int _lttng_variant_statedump(struct ust_registry_session *session, | |
0d32d1a9 MD |
363 | uint32_t nr_choices, const char *tag_name, |
364 | uint32_t alignment, | |
b623cb6a | 365 | const struct lttng_ust_ctl_field *fields, size_t nr_fields, |
da860cab MD |
366 | size_t *iter_field, size_t nesting) |
367 | { | |
b623cb6a | 368 | const struct lttng_ust_ctl_field *variant = &fields[*iter_field]; |
0d32d1a9 | 369 | uint32_t i; |
da860cab | 370 | int ret; |
fc4b93fa | 371 | char identifier[LTTNG_UST_ABI_SYM_NAME_LEN]; |
da860cab | 372 | |
da860cab | 373 | (*iter_field)++; |
0d32d1a9 MD |
374 | sanitize_ctf_identifier(identifier, tag_name); |
375 | if (alignment) { | |
376 | ret = print_tabs(session, nesting); | |
377 | if (ret) { | |
378 | goto end; | |
379 | } | |
380 | ret = lttng_metadata_printf(session, | |
381 | "struct { } align(%u) _%s_padding;\n", | |
382 | alignment * CHAR_BIT, | |
383 | variant->name); | |
384 | if (ret) { | |
385 | goto end; | |
386 | } | |
387 | } | |
a1f68b22 MD |
388 | ret = print_tabs(session, nesting); |
389 | if (ret) { | |
390 | goto end; | |
391 | } | |
da860cab | 392 | ret = lttng_metadata_printf(session, |
a1f68b22 MD |
393 | "variant <_%s> {\n", |
394 | identifier); | |
da860cab MD |
395 | if (ret) { |
396 | goto end; | |
397 | } | |
398 | ||
399 | for (i = 0; i < nr_choices; i++) { | |
400 | if (*iter_field >= nr_fields) { | |
401 | ret = -EOVERFLOW; | |
402 | goto end; | |
403 | } | |
404 | ret = _lttng_field_statedump(session, | |
405 | fields, nr_fields, | |
406 | iter_field, nesting + 1); | |
dc6403f3 JG |
407 | if (ret) { |
408 | goto end; | |
409 | } | |
da860cab | 410 | } |
a1f68b22 MD |
411 | sanitize_ctf_identifier(identifier, variant->name); |
412 | ret = print_tabs(session, nesting); | |
850c5d4a JR |
413 | if (ret) { |
414 | goto end; | |
415 | } | |
da860cab | 416 | ret = lttng_metadata_printf(session, |
a1f68b22 MD |
417 | "} _%s;\n", |
418 | identifier); | |
da860cab MD |
419 | if (ret) { |
420 | goto end; | |
421 | } | |
10b56aef MD |
422 | end: |
423 | return ret; | |
424 | } | |
425 | ||
d0b96690 DG |
426 | static |
427 | int _lttng_field_statedump(struct ust_registry_session *session, | |
b623cb6a | 428 | const struct lttng_ust_ctl_field *fields, size_t nr_fields, |
da860cab | 429 | size_t *iter_field, size_t nesting) |
d0b96690 DG |
430 | { |
431 | int ret = 0; | |
432 | const char *bo_be = " byte_order = be;"; | |
433 | const char *bo_le = " byte_order = le;"; | |
434 | const char *bo_native = ""; | |
435 | const char *bo_reverse; | |
b623cb6a | 436 | const struct lttng_ust_ctl_field *field; |
d0b96690 | 437 | |
da860cab MD |
438 | if (*iter_field >= nr_fields) { |
439 | ret = -EOVERFLOW; | |
440 | goto end; | |
441 | } | |
442 | field = &fields[*iter_field]; | |
443 | ||
444 | if (session->byte_order == BIG_ENDIAN) { | |
d0b96690 | 445 | bo_reverse = bo_le; |
da860cab | 446 | } else { |
d0b96690 | 447 | bo_reverse = bo_be; |
da860cab | 448 | } |
d0b96690 DG |
449 | |
450 | switch (field->type.atype) { | |
b623cb6a | 451 | case lttng_ust_ctl_atype_integer: |
da860cab MD |
452 | ret = print_tabs(session, nesting); |
453 | if (ret) { | |
454 | goto end; | |
455 | } | |
d0b96690 | 456 | ret = lttng_metadata_printf(session, |
da860cab | 457 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
0d32d1a9 MD |
458 | field->type.u.integer.size, |
459 | field->type.u.integer.alignment, | |
460 | field->type.u.integer.signedness, | |
b623cb6a | 461 | (field->type.u.integer.encoding == lttng_ust_ctl_encode_none) |
d0b96690 | 462 | ? "none" |
b623cb6a | 463 | : (field->type.u.integer.encoding == lttng_ust_ctl_encode_UTF8) |
d0b96690 DG |
464 | ? "UTF8" |
465 | : "ASCII", | |
0d32d1a9 MD |
466 | field->type.u.integer.base, |
467 | field->type.u.integer.reverse_byte_order ? bo_reverse : bo_native, | |
d0b96690 | 468 | field->name); |
da860cab | 469 | (*iter_field)++; |
d0b96690 | 470 | break; |
b623cb6a | 471 | case lttng_ust_ctl_atype_enum: |
10b56aef | 472 | ret = ust_metadata_enum_statedump(session, |
0d32d1a9 MD |
473 | field->type.u.legacy.basic.enumeration.name, |
474 | field->type.u.legacy.basic.enumeration.id, | |
475 | &field->type.u.legacy.basic.enumeration.container_type, | |
da860cab | 476 | field->name, iter_field, nesting); |
10b56aef | 477 | break; |
b623cb6a | 478 | case lttng_ust_ctl_atype_float: |
da860cab MD |
479 | ret = print_tabs(session, nesting); |
480 | if (ret) { | |
481 | goto end; | |
482 | } | |
d0b96690 | 483 | ret = lttng_metadata_printf(session, |
da860cab | 484 | "floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
0d32d1a9 MD |
485 | field->type.u._float.exp_dig, |
486 | field->type.u._float.mant_dig, | |
487 | field->type.u._float.alignment, | |
488 | field->type.u._float.reverse_byte_order ? bo_reverse : bo_native, | |
d0b96690 | 489 | field->name); |
da860cab | 490 | (*iter_field)++; |
d0b96690 | 491 | break; |
b623cb6a | 492 | case lttng_ust_ctl_atype_array: |
d0b96690 | 493 | { |
b623cb6a | 494 | const struct lttng_ust_ctl_basic_type *elem_type; |
d0b96690 | 495 | |
da860cab MD |
496 | ret = print_tabs(session, nesting); |
497 | if (ret) { | |
498 | goto end; | |
499 | } | |
0d32d1a9 MD |
500 | elem_type = &field->type.u.legacy.array.elem_type; |
501 | /* Only integers are currently supported in arrays. */ | |
b623cb6a | 502 | if (elem_type->atype != lttng_ust_ctl_atype_integer) { |
0d32d1a9 MD |
503 | ret = -EINVAL; |
504 | goto end; | |
505 | } | |
d0b96690 | 506 | ret = lttng_metadata_printf(session, |
da860cab | 507 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
d0b96690 DG |
508 | elem_type->u.basic.integer.size, |
509 | elem_type->u.basic.integer.alignment, | |
510 | elem_type->u.basic.integer.signedness, | |
b623cb6a | 511 | (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none) |
d0b96690 | 512 | ? "none" |
b623cb6a | 513 | : (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8) |
d0b96690 DG |
514 | ? "UTF8" |
515 | : "ASCII", | |
516 | elem_type->u.basic.integer.base, | |
517 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
0d32d1a9 MD |
518 | field->name, field->type.u.legacy.array.length); |
519 | (*iter_field)++; | |
520 | break; | |
521 | } | |
b623cb6a | 522 | case lttng_ust_ctl_atype_array_nestable: |
0d32d1a9 MD |
523 | { |
524 | uint32_t array_length; | |
b623cb6a MJ |
525 | const struct lttng_ust_ctl_field *array_nestable; |
526 | const struct lttng_ust_ctl_type *elem_type; | |
0d32d1a9 MD |
527 | |
528 | array_length = field->type.u.array_nestable.length; | |
529 | (*iter_field)++; | |
530 | ||
531 | if (*iter_field >= nr_fields) { | |
532 | ret = -EOVERFLOW; | |
533 | goto end; | |
534 | } | |
535 | array_nestable = &fields[*iter_field]; | |
536 | elem_type = &array_nestable->type; | |
537 | ||
538 | /* Only integers are currently supported in arrays. */ | |
b623cb6a | 539 | if (elem_type->atype != lttng_ust_ctl_atype_integer) { |
0d32d1a9 MD |
540 | ret = -EINVAL; |
541 | goto end; | |
542 | } | |
543 | ||
544 | if (field->type.u.array_nestable.alignment) { | |
545 | ret = print_tabs(session, nesting); | |
546 | if (ret) { | |
547 | goto end; | |
548 | } | |
549 | ret = lttng_metadata_printf(session, | |
550 | "struct { } align(%u) _%s_padding;\n", | |
551 | field->type.u.array_nestable.alignment * CHAR_BIT, | |
552 | field->name); | |
553 | if (ret) { | |
554 | goto end; | |
555 | } | |
556 | } | |
557 | ||
558 | ret = print_tabs(session, nesting); | |
559 | if (ret) { | |
560 | goto end; | |
561 | } | |
562 | ret = lttng_metadata_printf(session, | |
563 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", | |
564 | elem_type->u.integer.size, | |
565 | elem_type->u.integer.alignment, | |
566 | elem_type->u.integer.signedness, | |
b623cb6a | 567 | (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none) |
0d32d1a9 | 568 | ? "none" |
b623cb6a | 569 | : (elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8) |
0d32d1a9 MD |
570 | ? "UTF8" |
571 | : "ASCII", | |
572 | elem_type->u.integer.base, | |
573 | elem_type->u.integer.reverse_byte_order ? bo_reverse : bo_native, | |
574 | field->name, array_length); | |
da860cab | 575 | (*iter_field)++; |
d0b96690 DG |
576 | break; |
577 | } | |
b623cb6a | 578 | case lttng_ust_ctl_atype_sequence: |
d0b96690 | 579 | { |
b623cb6a MJ |
580 | const struct lttng_ust_ctl_basic_type *elem_type; |
581 | const struct lttng_ust_ctl_basic_type *length_type; | |
d0b96690 | 582 | |
0d32d1a9 MD |
583 | elem_type = &field->type.u.legacy.sequence.elem_type; |
584 | length_type = &field->type.u.legacy.sequence.length_type; | |
da860cab MD |
585 | ret = print_tabs(session, nesting); |
586 | if (ret) { | |
587 | goto end; | |
588 | } | |
0d32d1a9 MD |
589 | |
590 | /* Only integers are currently supported in sequences. */ | |
b623cb6a | 591 | if (elem_type->atype != lttng_ust_ctl_atype_integer) { |
0d32d1a9 MD |
592 | ret = -EINVAL; |
593 | goto end; | |
594 | } | |
595 | ||
d0b96690 | 596 | ret = lttng_metadata_printf(session, |
da860cab | 597 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
d0b96690 DG |
598 | length_type->u.basic.integer.size, |
599 | (unsigned int) length_type->u.basic.integer.alignment, | |
600 | length_type->u.basic.integer.signedness, | |
b623cb6a | 601 | (length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none) |
d0b96690 | 602 | ? "none" |
b623cb6a | 603 | : ((length_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8) |
d0b96690 DG |
604 | ? "UTF8" |
605 | : "ASCII"), | |
606 | length_type->u.basic.integer.base, | |
607 | length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
608 | field->name); | |
da860cab MD |
609 | if (ret) { |
610 | goto end; | |
611 | } | |
d0b96690 | 612 | |
da860cab MD |
613 | ret = print_tabs(session, nesting); |
614 | if (ret) { | |
615 | goto end; | |
616 | } | |
d0b96690 | 617 | ret = lttng_metadata_printf(session, |
da860cab | 618 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
d0b96690 DG |
619 | elem_type->u.basic.integer.size, |
620 | (unsigned int) elem_type->u.basic.integer.alignment, | |
621 | elem_type->u.basic.integer.signedness, | |
b623cb6a | 622 | (elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_none) |
d0b96690 | 623 | ? "none" |
b623cb6a | 624 | : ((elem_type->u.basic.integer.encoding == lttng_ust_ctl_encode_UTF8) |
d0b96690 DG |
625 | ? "UTF8" |
626 | : "ASCII"), | |
627 | elem_type->u.basic.integer.base, | |
628 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, | |
629 | field->name, | |
630 | field->name); | |
da860cab | 631 | (*iter_field)++; |
d0b96690 DG |
632 | break; |
633 | } | |
b623cb6a | 634 | case lttng_ust_ctl_atype_sequence_nestable: |
0d32d1a9 | 635 | { |
b623cb6a MJ |
636 | const struct lttng_ust_ctl_field *sequence_nestable; |
637 | const struct lttng_ust_ctl_type *elem_type; | |
0d32d1a9 MD |
638 | |
639 | (*iter_field)++; | |
640 | if (*iter_field >= nr_fields) { | |
641 | ret = -EOVERFLOW; | |
642 | goto end; | |
643 | } | |
644 | sequence_nestable = &fields[*iter_field]; | |
645 | elem_type = &sequence_nestable->type; | |
646 | ||
647 | /* Only integers are currently supported in sequences. */ | |
b623cb6a | 648 | if (elem_type->atype != lttng_ust_ctl_atype_integer) { |
0d32d1a9 MD |
649 | ret = -EINVAL; |
650 | goto end; | |
651 | } | |
652 | ||
653 | if (field->type.u.sequence_nestable.alignment) { | |
654 | ret = print_tabs(session, nesting); | |
655 | if (ret) { | |
656 | goto end; | |
657 | } | |
658 | ret = lttng_metadata_printf(session, | |
659 | "struct { } align(%u) _%s_padding;\n", | |
660 | field->type.u.sequence_nestable.alignment * CHAR_BIT, | |
661 | field->name); | |
662 | if (ret) { | |
663 | goto end; | |
664 | } | |
665 | } | |
d0b96690 | 666 | |
0d32d1a9 MD |
667 | ret = print_tabs(session, nesting); |
668 | if (ret) { | |
669 | goto end; | |
670 | } | |
671 | ret = lttng_metadata_printf(session, | |
672 | "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ _%s ];\n", | |
673 | elem_type->u.integer.size, | |
674 | (unsigned int) elem_type->u.integer.alignment, | |
675 | elem_type->u.integer.signedness, | |
b623cb6a | 676 | (elem_type->u.integer.encoding == lttng_ust_ctl_encode_none) |
0d32d1a9 | 677 | ? "none" |
b623cb6a | 678 | : ((elem_type->u.integer.encoding == lttng_ust_ctl_encode_UTF8) |
0d32d1a9 MD |
679 | ? "UTF8" |
680 | : "ASCII"), | |
681 | elem_type->u.integer.base, | |
682 | elem_type->u.integer.reverse_byte_order ? bo_reverse : bo_native, | |
683 | field->name, | |
684 | field->type.u.sequence_nestable.length_name); | |
685 | (*iter_field)++; | |
686 | break; | |
687 | } | |
b623cb6a | 688 | case lttng_ust_ctl_atype_string: |
d0b96690 | 689 | /* Default encoding is UTF8 */ |
da860cab MD |
690 | ret = print_tabs(session, nesting); |
691 | if (ret) { | |
692 | goto end; | |
693 | } | |
d0b96690 | 694 | ret = lttng_metadata_printf(session, |
da860cab | 695 | "string%s _%s;\n", |
b623cb6a | 696 | field->type.u.string.encoding == lttng_ust_ctl_encode_ASCII ? |
d0b96690 DG |
697 | " { encoding = ASCII; }" : "", |
698 | field->name); | |
da860cab MD |
699 | (*iter_field)++; |
700 | break; | |
b623cb6a | 701 | case lttng_ust_ctl_atype_variant: |
0d32d1a9 MD |
702 | ret = _lttng_variant_statedump(session, |
703 | field->type.u.legacy.variant.nr_choices, | |
704 | field->type.u.legacy.variant.tag_name, | |
705 | 0, | |
706 | fields, nr_fields, iter_field, nesting); | |
707 | if (ret) { | |
708 | goto end; | |
709 | } | |
710 | break; | |
b623cb6a | 711 | case lttng_ust_ctl_atype_variant_nestable: |
0d32d1a9 MD |
712 | ret = _lttng_variant_statedump(session, |
713 | field->type.u.variant_nestable.nr_choices, | |
714 | field->type.u.variant_nestable.tag_name, | |
715 | field->type.u.variant_nestable.alignment, | |
716 | fields, nr_fields, iter_field, nesting); | |
da860cab MD |
717 | if (ret) { |
718 | goto end; | |
719 | } | |
720 | break; | |
b623cb6a | 721 | case lttng_ust_ctl_atype_struct: |
0d32d1a9 MD |
722 | if (field->type.u.legacy._struct.nr_fields != 0) { |
723 | /* Currently only 0-length structures are supported. */ | |
724 | ret = -EINVAL; | |
725 | goto end; | |
726 | } | |
da860cab MD |
727 | ret = print_tabs(session, nesting); |
728 | if (ret) { | |
729 | goto end; | |
730 | } | |
731 | ret = lttng_metadata_printf(session, | |
732 | "struct {} _%s;\n", | |
733 | field->name); | |
734 | (*iter_field)++; | |
d0b96690 | 735 | break; |
b623cb6a | 736 | case lttng_ust_ctl_atype_struct_nestable: |
0d32d1a9 MD |
737 | if (field->type.u.struct_nestable.nr_fields != 0) { |
738 | /* Currently only 0-length structures are supported. */ | |
739 | ret = -EINVAL; | |
740 | goto end; | |
741 | } | |
742 | ret = print_tabs(session, nesting); | |
743 | if (ret) { | |
744 | goto end; | |
745 | } | |
746 | if (field->type.u.struct_nestable.alignment) { | |
747 | ret = lttng_metadata_printf(session, | |
748 | "struct {} align(%u) _%s;\n", | |
749 | field->type.u.struct_nestable.alignment * CHAR_BIT, | |
750 | field->name); | |
751 | if (ret) { | |
752 | goto end; | |
753 | } | |
754 | } else { | |
755 | ret = lttng_metadata_printf(session, | |
756 | "struct {} _%s;\n", | |
757 | field->name); | |
758 | } | |
759 | (*iter_field)++; | |
760 | break; | |
b623cb6a | 761 | case lttng_ust_ctl_atype_enum_nestable: |
0d32d1a9 | 762 | { |
b623cb6a MJ |
763 | const struct lttng_ust_ctl_field *container_field; |
764 | const struct lttng_ust_ctl_type *container_type; | |
0d32d1a9 MD |
765 | |
766 | (*iter_field)++; | |
767 | if (*iter_field >= nr_fields) { | |
768 | ret = -EOVERFLOW; | |
769 | goto end; | |
770 | } | |
771 | container_field = &fields[*iter_field]; | |
772 | container_type = &container_field->type; | |
773 | ||
774 | /* Only integers are supported as container types. */ | |
b623cb6a | 775 | if (container_type->atype != lttng_ust_ctl_atype_integer) { |
0d32d1a9 MD |
776 | ret = -EINVAL; |
777 | goto end; | |
778 | } | |
779 | ret = ust_metadata_enum_statedump(session, | |
780 | field->type.u.enum_nestable.name, | |
781 | field->type.u.enum_nestable.id, | |
782 | &container_type->u.integer, | |
783 | field->name, iter_field, nesting); | |
784 | break; | |
785 | } | |
d0b96690 | 786 | default: |
da860cab | 787 | ret = -EINVAL; |
d0b96690 | 788 | } |
da860cab | 789 | end: |
d0b96690 DG |
790 | return ret; |
791 | } | |
792 | ||
793 | static | |
794 | int _lttng_context_metadata_statedump(struct ust_registry_session *session, | |
795 | size_t nr_ctx_fields, | |
b623cb6a | 796 | struct lttng_ust_ctl_field *ctx) |
d0b96690 DG |
797 | { |
798 | int ret = 0; | |
da860cab | 799 | size_t i = 0; |
d0b96690 DG |
800 | |
801 | if (!ctx) | |
802 | return 0; | |
da860cab MD |
803 | for (;;) { |
804 | if (i >= nr_ctx_fields) { | |
805 | break; | |
806 | } | |
807 | ret = _lttng_field_statedump(session, ctx, | |
808 | nr_ctx_fields, &i, 2); | |
809 | if (ret) { | |
810 | break; | |
811 | } | |
d0b96690 DG |
812 | } |
813 | return ret; | |
814 | } | |
815 | ||
816 | static | |
817 | int _lttng_fields_metadata_statedump(struct ust_registry_session *session, | |
818 | struct ust_registry_event *event) | |
819 | { | |
820 | int ret = 0; | |
da860cab | 821 | size_t i = 0; |
d0b96690 | 822 | |
da860cab MD |
823 | for (;;) { |
824 | if (i >= event->nr_fields) { | |
825 | break; | |
826 | } | |
827 | ret = _lttng_field_statedump(session, event->fields, | |
828 | event->nr_fields, &i, 2); | |
829 | if (ret) { | |
830 | break; | |
831 | } | |
d0b96690 DG |
832 | } |
833 | return ret; | |
834 | } | |
835 | ||
836 | /* | |
837 | * Should be called with session registry mutex held. | |
838 | */ | |
839 | int ust_metadata_event_statedump(struct ust_registry_session *session, | |
840 | struct ust_registry_channel *chan, | |
841 | struct ust_registry_event *event) | |
842 | { | |
843 | int ret = 0; | |
844 | ||
845 | /* Don't dump metadata events */ | |
846 | if (chan->chan_id == -1U) | |
847 | return 0; | |
848 | ||
bacc39bb SM |
849 | /* |
850 | * We don't want to output an event's metadata before its parent | |
851 | * stream's metadata. If the stream's metadata hasn't been output yet, | |
852 | * skip this event. Its metadata will be output when we output the | |
853 | * stream's metadata. | |
854 | */ | |
855 | if (!chan->metadata_dumped || event->metadata_dumped) { | |
856 | return 0; | |
857 | } | |
858 | ||
d0b96690 DG |
859 | ret = lttng_metadata_printf(session, |
860 | "event {\n" | |
861 | " name = \"%s\";\n" | |
862 | " id = %u;\n" | |
863 | " stream_id = %u;\n", | |
864 | event->name, | |
865 | event->id, | |
866 | chan->chan_id); | |
0d32d1a9 | 867 | if (ret) { |
d0b96690 | 868 | goto end; |
0d32d1a9 | 869 | } |
d0b96690 DG |
870 | |
871 | ret = lttng_metadata_printf(session, | |
872 | " loglevel = %d;\n", | |
2106efa0 | 873 | event->loglevel_value); |
0d32d1a9 | 874 | if (ret) { |
d0b96690 | 875 | goto end; |
0d32d1a9 | 876 | } |
d0b96690 DG |
877 | |
878 | if (event->model_emf_uri) { | |
879 | ret = lttng_metadata_printf(session, | |
880 | " model.emf.uri = \"%s\";\n", | |
881 | event->model_emf_uri); | |
0d32d1a9 | 882 | if (ret) { |
d0b96690 | 883 | goto end; |
0d32d1a9 | 884 | } |
d0b96690 DG |
885 | } |
886 | ||
d0b96690 DG |
887 | ret = lttng_metadata_printf(session, |
888 | " fields := struct {\n" | |
889 | ); | |
0d32d1a9 | 890 | if (ret) { |
d0b96690 | 891 | goto end; |
0d32d1a9 | 892 | } |
d0b96690 DG |
893 | |
894 | ret = _lttng_fields_metadata_statedump(session, event); | |
0d32d1a9 | 895 | if (ret) { |
d0b96690 | 896 | goto end; |
0d32d1a9 | 897 | } |
d0b96690 DG |
898 | |
899 | ret = lttng_metadata_printf(session, | |
900 | " };\n" | |
901 | "};\n\n"); | |
0d32d1a9 | 902 | if (ret) { |
d0b96690 | 903 | goto end; |
0d32d1a9 | 904 | } |
7972aab2 | 905 | event->metadata_dumped = 1; |
d0b96690 DG |
906 | |
907 | end: | |
908 | return ret; | |
909 | } | |
910 | ||
911 | /* | |
912 | * Should be called with session registry mutex held. | |
60f1b42d JG |
913 | * |
914 | * RCU read lock must be held by the caller. | |
d0b96690 DG |
915 | */ |
916 | int ust_metadata_channel_statedump(struct ust_registry_session *session, | |
917 | struct ust_registry_channel *chan) | |
918 | { | |
e4589ae2 | 919 | int ret; |
d0b96690 | 920 | |
60f1b42d JG |
921 | ASSERT_RCU_READ_LOCKED(); |
922 | ||
d0b96690 DG |
923 | /* Don't dump metadata events */ |
924 | if (chan->chan_id == -1U) | |
925 | return 0; | |
926 | ||
927 | if (!chan->header_type) | |
928 | return -EINVAL; | |
929 | ||
930 | ret = lttng_metadata_printf(session, | |
931 | "stream {\n" | |
932 | " id = %u;\n" | |
933 | " event.header := %s;\n" | |
934 | " packet.context := struct packet_context;\n", | |
935 | chan->chan_id, | |
b623cb6a | 936 | chan->header_type == LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT ? |
d0b96690 DG |
937 | "struct event_header_compact" : |
938 | "struct event_header_large"); | |
0d32d1a9 | 939 | if (ret) { |
e4589ae2 | 940 | return ret; |
0d32d1a9 | 941 | } |
d0b96690 DG |
942 | |
943 | if (chan->ctx_fields) { | |
944 | ret = lttng_metadata_printf(session, | |
945 | " event.context := struct {\n"); | |
0d32d1a9 | 946 | if (ret) { |
e4589ae2 | 947 | return ret; |
0d32d1a9 | 948 | } |
d0b96690 DG |
949 | } |
950 | ret = _lttng_context_metadata_statedump(session, | |
951 | chan->nr_ctx_fields, | |
952 | chan->ctx_fields); | |
0d32d1a9 | 953 | if (ret) { |
e4589ae2 | 954 | return ret; |
0d32d1a9 | 955 | } |
d0b96690 DG |
956 | if (chan->ctx_fields) { |
957 | ret = lttng_metadata_printf(session, | |
958 | " };\n"); | |
0d32d1a9 | 959 | if (ret) { |
e4589ae2 | 960 | return ret; |
0d32d1a9 | 961 | } |
d0b96690 DG |
962 | } |
963 | ||
964 | ret = lttng_metadata_printf(session, | |
965 | "};\n\n"); | |
e4589ae2 SM |
966 | if (ret) { |
967 | return ret; | |
968 | } | |
969 | ||
7972aab2 DG |
970 | /* Flag success of metadata dump. */ |
971 | chan->metadata_dumped = 1; | |
d0b96690 | 972 | |
bacc39bb SM |
973 | /* |
974 | * Output the metadata of any existing event. | |
975 | * | |
976 | * Sort the events by id. This is not necessary, but it's nice to have | |
977 | * a more predictable order in the metadata file. | |
978 | */ | |
979 | std::vector<ust_registry_event *> events; | |
980 | { | |
981 | cds_lfht_iter event_iter; | |
982 | ust_registry_event *event; | |
983 | cds_lfht_for_each_entry(chan->events->ht, &event_iter, event, | |
984 | node.node) { | |
985 | events.push_back(event); | |
986 | } | |
987 | } | |
988 | ||
989 | std::sort(events.begin(), events.end(), | |
990 | [] (ust_registry_event *a, ust_registry_event *b) { | |
991 | return a->id < b->id; | |
992 | }); | |
993 | ||
994 | for (ust_registry_event *event : events) { | |
995 | ust_metadata_event_statedump(session, chan, event); | |
996 | } | |
997 | ||
e4589ae2 | 998 | return 0; |
d0b96690 DG |
999 | } |
1000 | ||
1001 | static | |
1002 | int _lttng_stream_packet_context_declare(struct ust_registry_session *session) | |
1003 | { | |
1004 | return lttng_metadata_printf(session, | |
1005 | "struct packet_context {\n" | |
1006 | " uint64_clock_monotonic_t timestamp_begin;\n" | |
1007 | " uint64_clock_monotonic_t timestamp_end;\n" | |
1008 | " uint64_t content_size;\n" | |
1009 | " uint64_t packet_size;\n" | |
0793bcc6 | 1010 | " uint64_t packet_seq_num;\n" |
d0b96690 DG |
1011 | " unsigned long events_discarded;\n" |
1012 | " uint32_t cpu_id;\n" | |
1013 | "};\n\n" | |
1014 | ); | |
1015 | } | |
1016 | ||
1017 | /* | |
1018 | * Compact header: | |
1019 | * id: range: 0 - 30. | |
1020 | * id 31 is reserved to indicate an extended header. | |
1021 | * | |
1022 | * Large header: | |
1023 | * id: range: 0 - 65534. | |
1024 | * id 65535 is reserved to indicate an extended header. | |
1025 | */ | |
1026 | static | |
1027 | int _lttng_event_header_declare(struct ust_registry_session *session) | |
1028 | { | |
1029 | return lttng_metadata_printf(session, | |
1030 | "struct event_header_compact {\n" | |
1031 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
1032 | " variant <id> {\n" | |
1033 | " struct {\n" | |
1034 | " uint27_clock_monotonic_t timestamp;\n" | |
1035 | " } compact;\n" | |
1036 | " struct {\n" | |
1037 | " uint32_t id;\n" | |
1038 | " uint64_clock_monotonic_t timestamp;\n" | |
1039 | " } extended;\n" | |
1040 | " } v;\n" | |
1041 | "} align(%u);\n" | |
1042 | "\n" | |
1043 | "struct event_header_large {\n" | |
1044 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
1045 | " variant <id> {\n" | |
1046 | " struct {\n" | |
1047 | " uint32_clock_monotonic_t timestamp;\n" | |
1048 | " } compact;\n" | |
1049 | " struct {\n" | |
1050 | " uint32_t id;\n" | |
1051 | " uint64_clock_monotonic_t timestamp;\n" | |
1052 | " } extended;\n" | |
1053 | " } v;\n" | |
1054 | "} align(%u);\n\n", | |
1055 | session->uint32_t_alignment, | |
1056 | session->uint16_t_alignment | |
1057 | ); | |
1058 | } | |
1059 | ||
dc113ec7 MD |
1060 | /* |
1061 | * The offset between monotonic and realtime clock can be negative if | |
1062 | * the system sets the REALTIME clock to 0 after boot. | |
dc113ec7 | 1063 | */ |
d0b96690 | 1064 | static |
8c645bb0 | 1065 | int measure_single_clock_offset(struct offset_sample *sample) |
d0b96690 | 1066 | { |
dc113ec7 | 1067 | uint64_t monotonic_avg, monotonic[2], measure_delta, realtime; |
fc0bb9fa | 1068 | uint64_t tcf = trace_clock_freq(); |
d0b96690 DG |
1069 | struct timespec rts = { 0, 0 }; |
1070 | int ret; | |
1071 | ||
1072 | monotonic[0] = trace_clock_read64(); | |
389fbf04 | 1073 | ret = lttng_clock_gettime(CLOCK_REALTIME, &rts); |
8c645bb0 MD |
1074 | if (ret < 0) { |
1075 | return ret; | |
1076 | } | |
d0b96690 | 1077 | monotonic[1] = trace_clock_read64(); |
8c645bb0 MD |
1078 | measure_delta = monotonic[1] - monotonic[0]; |
1079 | if (measure_delta > sample->measure_delta) { | |
1080 | /* | |
1081 | * Discard value if it took longer to read than the best | |
1082 | * sample so far. | |
1083 | */ | |
1084 | return 0; | |
1085 | } | |
dc113ec7 | 1086 | monotonic_avg = (monotonic[0] + monotonic[1]) >> 1; |
6e1b0543 MD |
1087 | realtime = (uint64_t) rts.tv_sec * tcf; |
1088 | if (tcf == NSEC_PER_SEC) { | |
1089 | realtime += rts.tv_nsec; | |
1090 | } else { | |
1091 | realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; | |
fc0bb9fa | 1092 | } |
c2636b57 | 1093 | sample->offset = (int64_t) realtime - monotonic_avg; |
8c645bb0 MD |
1094 | sample->measure_delta = measure_delta; |
1095 | return 0; | |
d0b96690 DG |
1096 | } |
1097 | ||
8c645bb0 MD |
1098 | /* |
1099 | * Approximation of NTP time of day to clock monotonic correlation, | |
1100 | * taken at start of trace. Keep the measurement that took the less time | |
1101 | * to complete, thus removing imprecision caused by preemption. | |
c2636b57 | 1102 | * May return a negative offset. |
8c645bb0 MD |
1103 | */ |
1104 | static | |
c2636b57 | 1105 | int64_t measure_clock_offset(void) |
8c645bb0 MD |
1106 | { |
1107 | int i; | |
1108 | struct offset_sample offset_best_sample = { | |
1109 | .offset = 0, | |
1110 | .measure_delta = UINT64_MAX, | |
1111 | }; | |
1112 | ||
1113 | for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { | |
1114 | if (measure_single_clock_offset(&offset_best_sample)) { | |
1115 | return 0; | |
1116 | } | |
1117 | } | |
1118 | return offset_best_sample.offset; | |
1119 | } | |
d0b96690 | 1120 | |
8de88061 JR |
1121 | static |
1122 | int print_metadata_session_information(struct ust_registry_session *registry) | |
1123 | { | |
1124 | int ret; | |
1125 | struct ltt_session *session = NULL; | |
1126 | char creation_datetime[ISO8601_STR_LEN]; | |
1127 | ||
1128 | rcu_read_lock(); | |
1129 | session = session_find_by_id(registry->tracing_id); | |
1130 | if (!session) { | |
1131 | ret = -1; | |
1132 | goto error; | |
1133 | } | |
1134 | ||
1135 | /* Print the trace name */ | |
1136 | ret = lttng_metadata_printf(registry, " trace_name = \""); | |
1137 | if (ret) { | |
1138 | goto error; | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * This is necessary since the creation time is present in the session | |
1143 | * name when it is generated. | |
1144 | */ | |
1145 | if (session->has_auto_generated_name) { | |
1146 | ret = print_escaped_ctf_string(registry, DEFAULT_SESSION_NAME); | |
1147 | } else { | |
1148 | ret = print_escaped_ctf_string(registry, session->name); | |
1149 | } | |
1150 | if (ret) { | |
1151 | goto error; | |
1152 | } | |
1153 | ||
1154 | ret = lttng_metadata_printf(registry, "\";\n"); | |
1155 | if (ret) { | |
1156 | goto error; | |
1157 | } | |
1158 | ||
1159 | /* Prepare creation time */ | |
1160 | ret = time_to_iso8601_str(session->creation_time, creation_datetime, | |
1161 | sizeof(creation_datetime)); | |
1162 | if (ret) { | |
1163 | goto error; | |
1164 | } | |
1165 | ||
1166 | /* Output the reste of the information */ | |
1167 | ret = lttng_metadata_printf(registry, | |
1168 | " trace_creation_datetime = \"%s\";\n" | |
1169 | " hostname = \"%s\";\n", | |
1170 | creation_datetime, session->hostname); | |
1171 | if (ret) { | |
1172 | goto error; | |
1173 | } | |
1174 | ||
1175 | error: | |
1176 | if (session) { | |
1177 | session_put(session); | |
1178 | } | |
1179 | rcu_read_unlock(); | |
1180 | return ret; | |
1181 | } | |
1182 | ||
1183 | static | |
1184 | int print_metadata_app_information(struct ust_registry_session *registry, | |
1185 | struct ust_app *app) | |
1186 | { | |
1187 | int ret; | |
1188 | char datetime[ISO8601_STR_LEN]; | |
1189 | ||
1190 | if (!app) { | |
1191 | ret = 0; | |
1192 | goto end; | |
1193 | } | |
1194 | ||
1195 | ret = time_to_iso8601_str( | |
1196 | app->registration_time, datetime, sizeof(datetime)); | |
1197 | if (ret) { | |
1198 | goto end; | |
1199 | } | |
1200 | ||
1201 | ret = lttng_metadata_printf(registry, | |
1202 | " tracer_patchlevel = %u;\n" | |
1203 | " vpid = %d;\n" | |
1204 | " procname = \"%s\";\n" | |
1205 | " vpid_datetime = \"%s\";\n", | |
1206 | app->version.patchlevel, (int) app->pid, app->name, | |
1207 | datetime); | |
1208 | ||
1209 | end: | |
1210 | return ret; | |
1211 | } | |
1212 | ||
d0b96690 DG |
1213 | /* |
1214 | * Should be called with session registry mutex held. | |
1215 | */ | |
1216 | int ust_metadata_session_statedump(struct ust_registry_session *session, | |
af6142cf MD |
1217 | struct ust_app *app, |
1218 | uint32_t major, | |
1219 | uint32_t minor) | |
d0b96690 | 1220 | { |
c70636a7 MJ |
1221 | char uuid_s[LTTNG_UUID_STR_LEN], |
1222 | clock_uuid_s[LTTNG_UUID_STR_LEN]; | |
d0b96690 | 1223 | int ret = 0; |
d0b96690 | 1224 | |
a0377dfe | 1225 | LTTNG_ASSERT(session); |
7972aab2 | 1226 | |
c8e0c5f5 | 1227 | lttng_uuid_to_str(session->uuid, uuid_s); |
d0b96690 | 1228 | |
d7ba1388 MD |
1229 | /* For crash ABI */ |
1230 | ret = lttng_metadata_printf(session, | |
1231 | "/* CTF %u.%u */\n\n", | |
1232 | CTF_SPEC_MAJOR, | |
1233 | CTF_SPEC_MINOR); | |
1234 | if (ret) { | |
1235 | goto end; | |
1236 | } | |
1237 | ||
d0b96690 DG |
1238 | ret = lttng_metadata_printf(session, |
1239 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" | |
1240 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" | |
1241 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" | |
1242 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" | |
1243 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" | |
1244 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
1245 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
1246 | "\n" | |
1247 | "trace {\n" | |
1248 | " major = %u;\n" | |
1249 | " minor = %u;\n" | |
1250 | " uuid = \"%s\";\n" | |
1251 | " byte_order = %s;\n" | |
1252 | " packet.header := struct {\n" | |
1253 | " uint32_t magic;\n" | |
1254 | " uint8_t uuid[16];\n" | |
1255 | " uint32_t stream_id;\n" | |
0793bcc6 | 1256 | " uint64_t stream_instance_id;\n" |
d0b96690 DG |
1257 | " };\n" |
1258 | "};\n\n", | |
1259 | session->uint8_t_alignment, | |
1260 | session->uint16_t_alignment, | |
1261 | session->uint32_t_alignment, | |
1262 | session->uint64_t_alignment, | |
1263 | session->bits_per_long, | |
1264 | session->long_alignment, | |
1265 | CTF_SPEC_MAJOR, | |
1266 | CTF_SPEC_MINOR, | |
1267 | uuid_s, | |
1268 | session->byte_order == BIG_ENDIAN ? "be" : "le" | |
1269 | ); | |
0d32d1a9 | 1270 | if (ret) { |
d0b96690 | 1271 | goto end; |
0d32d1a9 | 1272 | } |
d0b96690 | 1273 | |
d0b96690 DG |
1274 | ret = lttng_metadata_printf(session, |
1275 | "env {\n" | |
d0b96690 DG |
1276 | " domain = \"ust\";\n" |
1277 | " tracer_name = \"lttng-ust\";\n" | |
1278 | " tracer_major = %u;\n" | |
8de88061 JR |
1279 | " tracer_minor = %u;\n" |
1280 | " tracer_buffering_scheme = \"%s\";\n" | |
1281 | " tracer_buffering_id = %u;\n" | |
1282 | " architecture_bit_width = %u;\n", | |
af6142cf | 1283 | major, |
8de88061 JR |
1284 | minor, |
1285 | app ? "pid" : "uid", | |
1286 | app ? (int) app->pid : (int) session->tracing_uid, | |
1287 | session->bits_per_long); | |
1288 | if (ret) { | |
1289 | goto end; | |
1290 | } | |
1291 | ||
1292 | ret = print_metadata_session_information(session); | |
1293 | if (ret) { | |
d0b96690 | 1294 | goto end; |
8de88061 | 1295 | } |
d0b96690 DG |
1296 | |
1297 | /* | |
1298 | * If per-application registry, we can output extra information | |
1299 | * about the application. | |
1300 | */ | |
8de88061 JR |
1301 | ret = print_metadata_app_information(session, app); |
1302 | if (ret) { | |
1303 | goto end; | |
d0b96690 DG |
1304 | } |
1305 | ||
1306 | ret = lttng_metadata_printf(session, | |
1307 | "};\n\n" | |
1308 | ); | |
0d32d1a9 | 1309 | if (ret) { |
d0b96690 | 1310 | goto end; |
0d32d1a9 | 1311 | } |
d0b96690 DG |
1312 | |
1313 | ret = lttng_metadata_printf(session, | |
1314 | "clock {\n" | |
fc0bb9fa MD |
1315 | " name = \"%s\";\n", |
1316 | trace_clock_name() | |
d0b96690 | 1317 | ); |
0d32d1a9 | 1318 | if (ret) { |
d0b96690 | 1319 | goto end; |
0d32d1a9 | 1320 | } |
d0b96690 DG |
1321 | |
1322 | if (!trace_clock_uuid(clock_uuid_s)) { | |
1323 | ret = lttng_metadata_printf(session, | |
1324 | " uuid = \"%s\";\n", | |
1325 | clock_uuid_s | |
1326 | ); | |
0d32d1a9 | 1327 | if (ret) { |
d0b96690 | 1328 | goto end; |
0d32d1a9 | 1329 | } |
d0b96690 DG |
1330 | } |
1331 | ||
1332 | ret = lttng_metadata_printf(session, | |
fc0bb9fa | 1333 | " description = \"%s\";\n" |
d0b96690 DG |
1334 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
1335 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
c2636b57 | 1336 | " offset = %" PRId64 ";\n" |
d0b96690 | 1337 | "};\n\n", |
fc0bb9fa | 1338 | trace_clock_description(), |
d0b96690 DG |
1339 | trace_clock_freq(), |
1340 | measure_clock_offset() | |
1341 | ); | |
0d32d1a9 | 1342 | if (ret) { |
d0b96690 | 1343 | goto end; |
0d32d1a9 | 1344 | } |
d0b96690 DG |
1345 | |
1346 | ret = lttng_metadata_printf(session, | |
1347 | "typealias integer {\n" | |
1348 | " size = 27; align = 1; signed = false;\n" | |
fc0bb9fa | 1349 | " map = clock.%s.value;\n" |
d0b96690 DG |
1350 | "} := uint27_clock_monotonic_t;\n" |
1351 | "\n" | |
1352 | "typealias integer {\n" | |
1353 | " size = 32; align = %u; signed = false;\n" | |
fc0bb9fa | 1354 | " map = clock.%s.value;\n" |
d0b96690 DG |
1355 | "} := uint32_clock_monotonic_t;\n" |
1356 | "\n" | |
1357 | "typealias integer {\n" | |
1358 | " size = 64; align = %u; signed = false;\n" | |
fc0bb9fa | 1359 | " map = clock.%s.value;\n" |
d0b96690 | 1360 | "} := uint64_clock_monotonic_t;\n\n", |
fc0bb9fa | 1361 | trace_clock_name(), |
d0b96690 | 1362 | session->uint32_t_alignment, |
fc0bb9fa MD |
1363 | trace_clock_name(), |
1364 | session->uint64_t_alignment, | |
1365 | trace_clock_name() | |
d0b96690 | 1366 | ); |
0d32d1a9 | 1367 | if (ret) { |
d0b96690 | 1368 | goto end; |
0d32d1a9 | 1369 | } |
d0b96690 DG |
1370 | |
1371 | ret = _lttng_stream_packet_context_declare(session); | |
0d32d1a9 | 1372 | if (ret) { |
d0b96690 | 1373 | goto end; |
0d32d1a9 | 1374 | } |
d0b96690 DG |
1375 | |
1376 | ret = _lttng_event_header_declare(session); | |
0d32d1a9 | 1377 | if (ret) { |
d0b96690 | 1378 | goto end; |
0d32d1a9 | 1379 | } |
d0b96690 DG |
1380 | |
1381 | end: | |
1382 | return ret; | |
1383 | } |