| 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 _GNU_SOURCE |
| 23 | #define _LGPL_SOURCE |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <stdio.h> |
| 28 | #include <limits.h> |
| 29 | #include <unistd.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <common/common.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 NSEC_PER_SEC 1000000000ULL |
| 42 | #define NR_CLOCK_OFFSET_SAMPLES 10 |
| 43 | |
| 44 | struct offset_sample { |
| 45 | uint64_t offset; /* correlation offset */ |
| 46 | uint64_t measure_delta; /* lower is better */ |
| 47 | }; |
| 48 | |
| 49 | static inline |
| 50 | int fls(unsigned int x) |
| 51 | { |
| 52 | int r = 32; |
| 53 | |
| 54 | if (!x) |
| 55 | return 0; |
| 56 | if (!(x & 0xFFFF0000U)) { |
| 57 | x <<= 16; |
| 58 | r -= 16; |
| 59 | } |
| 60 | if (!(x & 0xFF000000U)) { |
| 61 | x <<= 8; |
| 62 | r -= 8; |
| 63 | } |
| 64 | if (!(x & 0xF0000000U)) { |
| 65 | x <<= 4; |
| 66 | r -= 4; |
| 67 | } |
| 68 | if (!(x & 0xC0000000U)) { |
| 69 | x <<= 2; |
| 70 | r -= 2; |
| 71 | } |
| 72 | if (!(x & 0x80000000U)) { |
| 73 | x <<= 1; |
| 74 | r -= 1; |
| 75 | } |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | int get_count_order(unsigned int count) |
| 81 | { |
| 82 | int order; |
| 83 | |
| 84 | order = fls(count) - 1; |
| 85 | if (count & (count - 1)) |
| 86 | order++; |
| 87 | return order; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Returns offset where to write in metadata array, or negative error value on error. |
| 92 | */ |
| 93 | static |
| 94 | ssize_t metadata_reserve(struct ust_registry_session *session, size_t len) |
| 95 | { |
| 96 | size_t new_len = session->metadata_len + len; |
| 97 | size_t new_alloc_len = new_len; |
| 98 | size_t old_alloc_len = session->metadata_alloc_len; |
| 99 | ssize_t ret; |
| 100 | |
| 101 | if (new_alloc_len > (UINT32_MAX >> 1)) |
| 102 | return -EINVAL; |
| 103 | if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | if (new_alloc_len > old_alloc_len) { |
| 107 | char *newptr; |
| 108 | |
| 109 | new_alloc_len = |
| 110 | max_t(size_t, 1U << get_count_order(new_alloc_len), old_alloc_len << 1); |
| 111 | newptr = realloc(session->metadata, new_alloc_len); |
| 112 | if (!newptr) |
| 113 | return -ENOMEM; |
| 114 | session->metadata = newptr; |
| 115 | /* We zero directly the memory from start of allocation. */ |
| 116 | memset(&session->metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len); |
| 117 | session->metadata_alloc_len = new_alloc_len; |
| 118 | } |
| 119 | ret = session->metadata_len; |
| 120 | session->metadata_len += len; |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static |
| 125 | int metadata_file_append(struct ust_registry_session *session, |
| 126 | const char *str, size_t len) |
| 127 | { |
| 128 | ssize_t written; |
| 129 | |
| 130 | if (session->metadata_fd < 0) { |
| 131 | return 0; |
| 132 | } |
| 133 | /* Write to metadata file */ |
| 134 | written = lttng_write(session->metadata_fd, str, len); |
| 135 | if (written != len) { |
| 136 | return -1; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * We have exclusive access to our metadata buffer (protected by the |
| 143 | * ust_lock), so we can do racy operations such as looking for |
| 144 | * remaining space left in packet and write, since mutual exclusion |
| 145 | * protects us from concurrent writes. |
| 146 | */ |
| 147 | static |
| 148 | int lttng_metadata_printf(struct ust_registry_session *session, |
| 149 | const char *fmt, ...) |
| 150 | { |
| 151 | char *str = NULL; |
| 152 | size_t len; |
| 153 | va_list ap; |
| 154 | ssize_t offset; |
| 155 | int ret; |
| 156 | |
| 157 | va_start(ap, fmt); |
| 158 | ret = vasprintf(&str, fmt, ap); |
| 159 | va_end(ap); |
| 160 | if (ret < 0) |
| 161 | return -ENOMEM; |
| 162 | |
| 163 | len = strlen(str); |
| 164 | offset = metadata_reserve(session, len); |
| 165 | if (offset < 0) { |
| 166 | ret = offset; |
| 167 | goto end; |
| 168 | } |
| 169 | memcpy(&session->metadata[offset], str, len); |
| 170 | ret = metadata_file_append(session, str, len); |
| 171 | if (ret) { |
| 172 | PERROR("Error appending to metadata file"); |
| 173 | goto end; |
| 174 | } |
| 175 | DBG3("Append to metadata: \"%s\"", str); |
| 176 | ret = 0; |
| 177 | |
| 178 | end: |
| 179 | free(str); |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | int _lttng_field_statedump(struct ust_registry_session *session, |
| 185 | const struct ustctl_field *field) |
| 186 | { |
| 187 | int ret = 0; |
| 188 | const char *bo_be = " byte_order = be;"; |
| 189 | const char *bo_le = " byte_order = le;"; |
| 190 | const char *bo_native = ""; |
| 191 | const char *bo_reverse; |
| 192 | |
| 193 | if (session->byte_order == BIG_ENDIAN) |
| 194 | bo_reverse = bo_le; |
| 195 | else |
| 196 | bo_reverse = bo_be; |
| 197 | |
| 198 | switch (field->type.atype) { |
| 199 | case ustctl_atype_integer: |
| 200 | ret = lttng_metadata_printf(session, |
| 201 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n", |
| 202 | field->type.u.basic.integer.size, |
| 203 | field->type.u.basic.integer.alignment, |
| 204 | field->type.u.basic.integer.signedness, |
| 205 | (field->type.u.basic.integer.encoding == ustctl_encode_none) |
| 206 | ? "none" |
| 207 | : (field->type.u.basic.integer.encoding == ustctl_encode_UTF8) |
| 208 | ? "UTF8" |
| 209 | : "ASCII", |
| 210 | field->type.u.basic.integer.base, |
| 211 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, |
| 212 | field->name); |
| 213 | break; |
| 214 | case ustctl_atype_float: |
| 215 | ret = lttng_metadata_printf(session, |
| 216 | " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n", |
| 217 | field->type.u.basic._float.exp_dig, |
| 218 | field->type.u.basic._float.mant_dig, |
| 219 | field->type.u.basic._float.alignment, |
| 220 | field->type.u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, |
| 221 | field->name); |
| 222 | break; |
| 223 | case ustctl_atype_enum: |
| 224 | return -EINVAL; |
| 225 | case ustctl_atype_array: |
| 226 | { |
| 227 | const struct ustctl_basic_type *elem_type; |
| 228 | |
| 229 | elem_type = &field->type.u.array.elem_type; |
| 230 | ret = lttng_metadata_printf(session, |
| 231 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n", |
| 232 | elem_type->u.basic.integer.size, |
| 233 | elem_type->u.basic.integer.alignment, |
| 234 | elem_type->u.basic.integer.signedness, |
| 235 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) |
| 236 | ? "none" |
| 237 | : (elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) |
| 238 | ? "UTF8" |
| 239 | : "ASCII", |
| 240 | elem_type->u.basic.integer.base, |
| 241 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, |
| 242 | field->name, field->type.u.array.length); |
| 243 | break; |
| 244 | } |
| 245 | case ustctl_atype_sequence: |
| 246 | { |
| 247 | const struct ustctl_basic_type *elem_type; |
| 248 | const struct ustctl_basic_type *length_type; |
| 249 | |
| 250 | elem_type = &field->type.u.sequence.elem_type; |
| 251 | length_type = &field->type.u.sequence.length_type; |
| 252 | ret = lttng_metadata_printf(session, |
| 253 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n", |
| 254 | length_type->u.basic.integer.size, |
| 255 | (unsigned int) length_type->u.basic.integer.alignment, |
| 256 | length_type->u.basic.integer.signedness, |
| 257 | (length_type->u.basic.integer.encoding == ustctl_encode_none) |
| 258 | ? "none" |
| 259 | : ((length_type->u.basic.integer.encoding == ustctl_encode_UTF8) |
| 260 | ? "UTF8" |
| 261 | : "ASCII"), |
| 262 | length_type->u.basic.integer.base, |
| 263 | length_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, |
| 264 | field->name); |
| 265 | if (ret) |
| 266 | return ret; |
| 267 | |
| 268 | ret = lttng_metadata_printf(session, |
| 269 | " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n", |
| 270 | elem_type->u.basic.integer.size, |
| 271 | (unsigned int) elem_type->u.basic.integer.alignment, |
| 272 | elem_type->u.basic.integer.signedness, |
| 273 | (elem_type->u.basic.integer.encoding == ustctl_encode_none) |
| 274 | ? "none" |
| 275 | : ((elem_type->u.basic.integer.encoding == ustctl_encode_UTF8) |
| 276 | ? "UTF8" |
| 277 | : "ASCII"), |
| 278 | elem_type->u.basic.integer.base, |
| 279 | elem_type->u.basic.integer.reverse_byte_order ? bo_reverse : bo_native, |
| 280 | field->name, |
| 281 | field->name); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | case ustctl_atype_string: |
| 286 | /* Default encoding is UTF8 */ |
| 287 | ret = lttng_metadata_printf(session, |
| 288 | " string%s _%s;\n", |
| 289 | field->type.u.basic.string.encoding == ustctl_encode_ASCII ? |
| 290 | " { encoding = ASCII; }" : "", |
| 291 | field->name); |
| 292 | break; |
| 293 | default: |
| 294 | return -EINVAL; |
| 295 | } |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static |
| 300 | int _lttng_context_metadata_statedump(struct ust_registry_session *session, |
| 301 | size_t nr_ctx_fields, |
| 302 | struct ustctl_field *ctx) |
| 303 | { |
| 304 | int ret = 0; |
| 305 | int i; |
| 306 | |
| 307 | if (!ctx) |
| 308 | return 0; |
| 309 | for (i = 0; i < nr_ctx_fields; i++) { |
| 310 | const struct ustctl_field *field = &ctx[i]; |
| 311 | |
| 312 | ret = _lttng_field_statedump(session, field); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | } |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static |
| 320 | int _lttng_fields_metadata_statedump(struct ust_registry_session *session, |
| 321 | struct ust_registry_event *event) |
| 322 | { |
| 323 | int ret = 0; |
| 324 | int i; |
| 325 | |
| 326 | for (i = 0; i < event->nr_fields; i++) { |
| 327 | const struct ustctl_field *field = &event->fields[i]; |
| 328 | |
| 329 | ret = _lttng_field_statedump(session, field); |
| 330 | if (ret) |
| 331 | return ret; |
| 332 | } |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Should be called with session registry mutex held. |
| 338 | */ |
| 339 | int ust_metadata_event_statedump(struct ust_registry_session *session, |
| 340 | struct ust_registry_channel *chan, |
| 341 | struct ust_registry_event *event) |
| 342 | { |
| 343 | int ret = 0; |
| 344 | |
| 345 | /* Don't dump metadata events */ |
| 346 | if (chan->chan_id == -1U) |
| 347 | return 0; |
| 348 | |
| 349 | ret = lttng_metadata_printf(session, |
| 350 | "event {\n" |
| 351 | " name = \"%s\";\n" |
| 352 | " id = %u;\n" |
| 353 | " stream_id = %u;\n", |
| 354 | event->name, |
| 355 | event->id, |
| 356 | chan->chan_id); |
| 357 | if (ret) |
| 358 | goto end; |
| 359 | |
| 360 | ret = lttng_metadata_printf(session, |
| 361 | " loglevel = %d;\n", |
| 362 | event->loglevel); |
| 363 | if (ret) |
| 364 | goto end; |
| 365 | |
| 366 | if (event->model_emf_uri) { |
| 367 | ret = lttng_metadata_printf(session, |
| 368 | " model.emf.uri = \"%s\";\n", |
| 369 | event->model_emf_uri); |
| 370 | if (ret) |
| 371 | goto end; |
| 372 | } |
| 373 | |
| 374 | #if 0 /* context for events not supported */ |
| 375 | if (event->ctx) { |
| 376 | ret = lttng_metadata_printf(session, |
| 377 | " context := struct {\n"); |
| 378 | if (ret) |
| 379 | goto end; |
| 380 | } |
| 381 | ret = _lttng_context_metadata_statedump(session, event->ctx); |
| 382 | if (ret) |
| 383 | goto end; |
| 384 | if (event->ctx) { |
| 385 | ret = lttng_metadata_printf(session, |
| 386 | " };\n"); |
| 387 | if (ret) |
| 388 | goto end; |
| 389 | } |
| 390 | #endif |
| 391 | ret = lttng_metadata_printf(session, |
| 392 | " fields := struct {\n" |
| 393 | ); |
| 394 | if (ret) |
| 395 | goto end; |
| 396 | |
| 397 | ret = _lttng_fields_metadata_statedump(session, event); |
| 398 | if (ret) |
| 399 | goto end; |
| 400 | |
| 401 | ret = lttng_metadata_printf(session, |
| 402 | " };\n" |
| 403 | "};\n\n"); |
| 404 | if (ret) |
| 405 | goto end; |
| 406 | event->metadata_dumped = 1; |
| 407 | |
| 408 | end: |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Should be called with session registry mutex held. |
| 414 | */ |
| 415 | int ust_metadata_channel_statedump(struct ust_registry_session *session, |
| 416 | struct ust_registry_channel *chan) |
| 417 | { |
| 418 | int ret = 0; |
| 419 | |
| 420 | /* Don't dump metadata events */ |
| 421 | if (chan->chan_id == -1U) |
| 422 | return 0; |
| 423 | |
| 424 | if (!chan->header_type) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | ret = lttng_metadata_printf(session, |
| 428 | "stream {\n" |
| 429 | " id = %u;\n" |
| 430 | " event.header := %s;\n" |
| 431 | " packet.context := struct packet_context;\n", |
| 432 | chan->chan_id, |
| 433 | chan->header_type == USTCTL_CHANNEL_HEADER_COMPACT ? |
| 434 | "struct event_header_compact" : |
| 435 | "struct event_header_large"); |
| 436 | if (ret) |
| 437 | goto end; |
| 438 | |
| 439 | if (chan->ctx_fields) { |
| 440 | ret = lttng_metadata_printf(session, |
| 441 | " event.context := struct {\n"); |
| 442 | if (ret) |
| 443 | goto end; |
| 444 | } |
| 445 | ret = _lttng_context_metadata_statedump(session, |
| 446 | chan->nr_ctx_fields, |
| 447 | chan->ctx_fields); |
| 448 | if (ret) |
| 449 | goto end; |
| 450 | if (chan->ctx_fields) { |
| 451 | ret = lttng_metadata_printf(session, |
| 452 | " };\n"); |
| 453 | if (ret) |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | ret = lttng_metadata_printf(session, |
| 458 | "};\n\n"); |
| 459 | /* Flag success of metadata dump. */ |
| 460 | chan->metadata_dumped = 1; |
| 461 | |
| 462 | end: |
| 463 | return ret; |
| 464 | } |
| 465 | |
| 466 | static |
| 467 | int _lttng_stream_packet_context_declare(struct ust_registry_session *session) |
| 468 | { |
| 469 | return lttng_metadata_printf(session, |
| 470 | "struct packet_context {\n" |
| 471 | " uint64_clock_monotonic_t timestamp_begin;\n" |
| 472 | " uint64_clock_monotonic_t timestamp_end;\n" |
| 473 | " uint64_t content_size;\n" |
| 474 | " uint64_t packet_size;\n" |
| 475 | " unsigned long events_discarded;\n" |
| 476 | " uint32_t cpu_id;\n" |
| 477 | "};\n\n" |
| 478 | ); |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Compact header: |
| 483 | * id: range: 0 - 30. |
| 484 | * id 31 is reserved to indicate an extended header. |
| 485 | * |
| 486 | * Large header: |
| 487 | * id: range: 0 - 65534. |
| 488 | * id 65535 is reserved to indicate an extended header. |
| 489 | */ |
| 490 | static |
| 491 | int _lttng_event_header_declare(struct ust_registry_session *session) |
| 492 | { |
| 493 | return lttng_metadata_printf(session, |
| 494 | "struct event_header_compact {\n" |
| 495 | " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" |
| 496 | " variant <id> {\n" |
| 497 | " struct {\n" |
| 498 | " uint27_clock_monotonic_t timestamp;\n" |
| 499 | " } compact;\n" |
| 500 | " struct {\n" |
| 501 | " uint32_t id;\n" |
| 502 | " uint64_clock_monotonic_t timestamp;\n" |
| 503 | " } extended;\n" |
| 504 | " } v;\n" |
| 505 | "} align(%u);\n" |
| 506 | "\n" |
| 507 | "struct event_header_large {\n" |
| 508 | " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" |
| 509 | " variant <id> {\n" |
| 510 | " struct {\n" |
| 511 | " uint32_clock_monotonic_t timestamp;\n" |
| 512 | " } compact;\n" |
| 513 | " struct {\n" |
| 514 | " uint32_t id;\n" |
| 515 | " uint64_clock_monotonic_t timestamp;\n" |
| 516 | " } extended;\n" |
| 517 | " } v;\n" |
| 518 | "} align(%u);\n\n", |
| 519 | session->uint32_t_alignment, |
| 520 | session->uint16_t_alignment |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | static |
| 525 | int measure_single_clock_offset(struct offset_sample *sample) |
| 526 | { |
| 527 | uint64_t offset, monotonic[2], measure_delta, realtime; |
| 528 | uint64_t tcf = trace_clock_freq(); |
| 529 | struct timespec rts = { 0, 0 }; |
| 530 | int ret; |
| 531 | |
| 532 | monotonic[0] = trace_clock_read64(); |
| 533 | ret = clock_gettime(CLOCK_REALTIME, &rts); |
| 534 | if (ret < 0) { |
| 535 | return ret; |
| 536 | } |
| 537 | monotonic[1] = trace_clock_read64(); |
| 538 | measure_delta = monotonic[1] - monotonic[0]; |
| 539 | if (measure_delta > sample->measure_delta) { |
| 540 | /* |
| 541 | * Discard value if it took longer to read than the best |
| 542 | * sample so far. |
| 543 | */ |
| 544 | return 0; |
| 545 | } |
| 546 | offset = (monotonic[0] + monotonic[1]) >> 1; |
| 547 | realtime = (uint64_t) rts.tv_sec * tcf; |
| 548 | if (tcf == NSEC_PER_SEC) { |
| 549 | realtime += rts.tv_nsec; |
| 550 | } else { |
| 551 | realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; |
| 552 | } |
| 553 | offset = realtime - offset; |
| 554 | sample->offset = offset; |
| 555 | sample->measure_delta = measure_delta; |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Approximation of NTP time of day to clock monotonic correlation, |
| 561 | * taken at start of trace. Keep the measurement that took the less time |
| 562 | * to complete, thus removing imprecision caused by preemption. |
| 563 | */ |
| 564 | static |
| 565 | uint64_t measure_clock_offset(void) |
| 566 | { |
| 567 | int i; |
| 568 | struct offset_sample offset_best_sample = { |
| 569 | .offset = 0, |
| 570 | .measure_delta = UINT64_MAX, |
| 571 | }; |
| 572 | |
| 573 | for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { |
| 574 | if (measure_single_clock_offset(&offset_best_sample)) { |
| 575 | return 0; |
| 576 | } |
| 577 | } |
| 578 | return offset_best_sample.offset; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Should be called with session registry mutex held. |
| 583 | */ |
| 584 | int ust_metadata_session_statedump(struct ust_registry_session *session, |
| 585 | struct ust_app *app, |
| 586 | uint32_t major, |
| 587 | uint32_t minor) |
| 588 | { |
| 589 | unsigned char *uuid_c; |
| 590 | char uuid_s[UUID_STR_LEN], |
| 591 | clock_uuid_s[UUID_STR_LEN]; |
| 592 | int ret = 0; |
| 593 | char hostname[HOST_NAME_MAX]; |
| 594 | |
| 595 | assert(session); |
| 596 | |
| 597 | uuid_c = session->uuid; |
| 598 | |
| 599 | snprintf(uuid_s, sizeof(uuid_s), |
| 600 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 601 | uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3], |
| 602 | uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7], |
| 603 | uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], |
| 604 | uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); |
| 605 | |
| 606 | /* For crash ABI */ |
| 607 | ret = lttng_metadata_printf(session, |
| 608 | "/* CTF %u.%u */\n\n", |
| 609 | CTF_SPEC_MAJOR, |
| 610 | CTF_SPEC_MINOR); |
| 611 | if (ret) { |
| 612 | goto end; |
| 613 | } |
| 614 | |
| 615 | ret = lttng_metadata_printf(session, |
| 616 | "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" |
| 617 | "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" |
| 618 | "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" |
| 619 | "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" |
| 620 | "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" |
| 621 | "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" |
| 622 | "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" |
| 623 | "\n" |
| 624 | "trace {\n" |
| 625 | " major = %u;\n" |
| 626 | " minor = %u;\n" |
| 627 | " uuid = \"%s\";\n" |
| 628 | " byte_order = %s;\n" |
| 629 | " packet.header := struct {\n" |
| 630 | " uint32_t magic;\n" |
| 631 | " uint8_t uuid[16];\n" |
| 632 | " uint32_t stream_id;\n" |
| 633 | " };\n" |
| 634 | "};\n\n", |
| 635 | session->uint8_t_alignment, |
| 636 | session->uint16_t_alignment, |
| 637 | session->uint32_t_alignment, |
| 638 | session->uint64_t_alignment, |
| 639 | session->bits_per_long, |
| 640 | session->long_alignment, |
| 641 | CTF_SPEC_MAJOR, |
| 642 | CTF_SPEC_MINOR, |
| 643 | uuid_s, |
| 644 | session->byte_order == BIG_ENDIAN ? "be" : "le" |
| 645 | ); |
| 646 | if (ret) |
| 647 | goto end; |
| 648 | |
| 649 | /* ignore error, just use empty string if error. */ |
| 650 | hostname[0] = '\0'; |
| 651 | ret = gethostname(hostname, sizeof(hostname)); |
| 652 | if (ret && errno == ENAMETOOLONG) |
| 653 | hostname[HOST_NAME_MAX - 1] = '\0'; |
| 654 | ret = lttng_metadata_printf(session, |
| 655 | "env {\n" |
| 656 | " hostname = \"%s\";\n" |
| 657 | " domain = \"ust\";\n" |
| 658 | " tracer_name = \"lttng-ust\";\n" |
| 659 | " tracer_major = %u;\n" |
| 660 | " tracer_minor = %u;\n", |
| 661 | hostname, |
| 662 | major, |
| 663 | minor |
| 664 | ); |
| 665 | if (ret) |
| 666 | goto end; |
| 667 | |
| 668 | /* |
| 669 | * If per-application registry, we can output extra information |
| 670 | * about the application. |
| 671 | */ |
| 672 | if (app) { |
| 673 | ret = lttng_metadata_printf(session, |
| 674 | " tracer_patchlevel = %u;\n" |
| 675 | " vpid = %d;\n" |
| 676 | " procname = \"%s\";\n", |
| 677 | app->version.patchlevel, |
| 678 | (int) app->pid, |
| 679 | app->name |
| 680 | ); |
| 681 | if (ret) |
| 682 | goto end; |
| 683 | } |
| 684 | |
| 685 | ret = lttng_metadata_printf(session, |
| 686 | "};\n\n" |
| 687 | ); |
| 688 | if (ret) |
| 689 | goto end; |
| 690 | |
| 691 | |
| 692 | ret = lttng_metadata_printf(session, |
| 693 | "clock {\n" |
| 694 | " name = \"%s\";\n", |
| 695 | trace_clock_name() |
| 696 | ); |
| 697 | if (ret) |
| 698 | goto end; |
| 699 | |
| 700 | if (!trace_clock_uuid(clock_uuid_s)) { |
| 701 | ret = lttng_metadata_printf(session, |
| 702 | " uuid = \"%s\";\n", |
| 703 | clock_uuid_s |
| 704 | ); |
| 705 | if (ret) |
| 706 | goto end; |
| 707 | } |
| 708 | |
| 709 | ret = lttng_metadata_printf(session, |
| 710 | " description = \"%s\";\n" |
| 711 | " freq = %" PRIu64 "; /* Frequency, in Hz */\n" |
| 712 | " /* clock value offset from Epoch is: offset * (1/freq) */\n" |
| 713 | " offset = %" PRIu64 ";\n" |
| 714 | "};\n\n", |
| 715 | trace_clock_description(), |
| 716 | trace_clock_freq(), |
| 717 | measure_clock_offset() |
| 718 | ); |
| 719 | if (ret) |
| 720 | goto end; |
| 721 | |
| 722 | ret = lttng_metadata_printf(session, |
| 723 | "typealias integer {\n" |
| 724 | " size = 27; align = 1; signed = false;\n" |
| 725 | " map = clock.%s.value;\n" |
| 726 | "} := uint27_clock_monotonic_t;\n" |
| 727 | "\n" |
| 728 | "typealias integer {\n" |
| 729 | " size = 32; align = %u; signed = false;\n" |
| 730 | " map = clock.%s.value;\n" |
| 731 | "} := uint32_clock_monotonic_t;\n" |
| 732 | "\n" |
| 733 | "typealias integer {\n" |
| 734 | " size = 64; align = %u; signed = false;\n" |
| 735 | " map = clock.%s.value;\n" |
| 736 | "} := uint64_clock_monotonic_t;\n\n", |
| 737 | trace_clock_name(), |
| 738 | session->uint32_t_alignment, |
| 739 | trace_clock_name(), |
| 740 | session->uint64_t_alignment, |
| 741 | trace_clock_name() |
| 742 | ); |
| 743 | if (ret) |
| 744 | goto end; |
| 745 | |
| 746 | ret = _lttng_stream_packet_context_declare(session); |
| 747 | if (ret) |
| 748 | goto end; |
| 749 | |
| 750 | ret = _lttng_event_header_declare(session); |
| 751 | if (ret) |
| 752 | goto end; |
| 753 | |
| 754 | end: |
| 755 | return ret; |
| 756 | } |