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