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