| 1 | /* |
| 2 | * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | */ |
| 14 | |
| 15 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-DECODER" |
| 16 | #include "logging.h" |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <assert.h> |
| 24 | #include <babeltrace/compat/uuid-internal.h> |
| 25 | #include <babeltrace/compat/memstream-internal.h> |
| 26 | #include <babeltrace/ctf-ir/trace.h> |
| 27 | #include <glib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include "ast.h" |
| 31 | #include "decoder.h" |
| 32 | #include "scanner.h" |
| 33 | |
| 34 | #define TSDL_MAGIC 0x75d11d57 |
| 35 | |
| 36 | BT_HIDDEN |
| 37 | int yydebug; |
| 38 | |
| 39 | struct ctf_metadata_decoder { |
| 40 | struct ctf_visitor_generate_ir *visitor; |
| 41 | uint8_t uuid[16]; |
| 42 | bool is_uuid_set; |
| 43 | int bo; |
| 44 | }; |
| 45 | |
| 46 | struct packet_header { |
| 47 | uint32_t magic; |
| 48 | uint8_t uuid[16]; |
| 49 | uint32_t checksum; |
| 50 | uint32_t content_size; |
| 51 | uint32_t packet_size; |
| 52 | uint8_t compression_scheme; |
| 53 | uint8_t encryption_scheme; |
| 54 | uint8_t checksum_scheme; |
| 55 | uint8_t major; |
| 56 | uint8_t minor; |
| 57 | } __attribute__((__packed__)); |
| 58 | |
| 59 | BT_HIDDEN |
| 60 | bool ctf_metadata_decoder_is_packetized(FILE *fp, int *byte_order) |
| 61 | { |
| 62 | uint32_t magic; |
| 63 | size_t len; |
| 64 | int ret = 0; |
| 65 | |
| 66 | len = fread(&magic, sizeof(magic), 1, fp); |
| 67 | if (len != 1) { |
| 68 | BT_LOGD_STR("Cannot reade first metadata packet header: assuming the stream is not packetized."); |
| 69 | goto end; |
| 70 | } |
| 71 | |
| 72 | if (byte_order) { |
| 73 | if (magic == TSDL_MAGIC) { |
| 74 | ret = 1; |
| 75 | *byte_order = BYTE_ORDER; |
| 76 | } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) { |
| 77 | ret = 1; |
| 78 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 79 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | end: |
| 84 | rewind(fp); |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | bool is_version_valid(unsigned int major, unsigned int minor) |
| 91 | { |
| 92 | return major == 1 && minor == 8; |
| 93 | } |
| 94 | |
| 95 | static |
| 96 | int decode_packet(struct ctf_metadata_decoder *mdec, FILE *in_fp, FILE *out_fp, |
| 97 | int byte_order) |
| 98 | { |
| 99 | struct packet_header header; |
| 100 | size_t readlen, writelen, toread; |
| 101 | uint8_t buf[512 + 1]; /* + 1 for debug-mode \0 */ |
| 102 | int ret = 0; |
| 103 | const long offset = ftell(in_fp); |
| 104 | |
| 105 | BT_LOGV("Decoding metadata packet: mdec-addr=%p, offset=%ld", |
| 106 | mdec, offset); |
| 107 | readlen = fread(&header, sizeof(header), 1, in_fp); |
| 108 | if (feof(in_fp) != 0) { |
| 109 | BT_LOGV("Reached end of file: offset=%ld", ftell(in_fp)); |
| 110 | goto end; |
| 111 | } |
| 112 | if (readlen < 1) { |
| 113 | BT_LOGV("Cannot decode metadata packet: offset=%ld", offset); |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | if (byte_order != BYTE_ORDER) { |
| 118 | header.magic = GUINT32_SWAP_LE_BE(header.magic); |
| 119 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); |
| 120 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); |
| 121 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); |
| 122 | } |
| 123 | |
| 124 | if (header.compression_scheme) { |
| 125 | BT_LOGE("Metadata packet compression is not supported as of this version: " |
| 126 | "compression-scheme=%u, offset=%ld", |
| 127 | (unsigned int) header.compression_scheme, offset); |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | if (header.encryption_scheme) { |
| 132 | BT_LOGE("Metadata packet encryption is not supported as of this version: " |
| 133 | "encryption-scheme=%u, offset=%ld", |
| 134 | (unsigned int) header.encryption_scheme, offset); |
| 135 | goto error; |
| 136 | } |
| 137 | |
| 138 | if (header.checksum || header.checksum_scheme) { |
| 139 | BT_LOGE("Metadata packet checksum verification is not supported as of this version: " |
| 140 | "checksum-scheme=%u, checksum=%x, offset=%ld", |
| 141 | (unsigned int) header.checksum_scheme, header.checksum, |
| 142 | offset); |
| 143 | goto error; |
| 144 | } |
| 145 | |
| 146 | if (!is_version_valid(header.major, header.minor)) { |
| 147 | BT_LOGE("Invalid metadata packet version: " |
| 148 | "version=%u.%u, offset=%ld", |
| 149 | header.major, header.minor, offset); |
| 150 | goto error; |
| 151 | } |
| 152 | |
| 153 | /* Set expected trace UUID if not set; otherwise validate it */ |
| 154 | if (mdec) { |
| 155 | if (!mdec->is_uuid_set) { |
| 156 | memcpy(mdec->uuid, header.uuid, sizeof(header.uuid)); |
| 157 | mdec->is_uuid_set = true; |
| 158 | } else if (bt_uuid_compare(header.uuid, mdec->uuid)) { |
| 159 | BT_LOGE("Metadata UUID mismatch between packets of the same stream: " |
| 160 | "packet-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " |
| 161 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " |
| 162 | "offset=%ld", |
| 163 | (unsigned int) header.uuid[0], |
| 164 | (unsigned int) header.uuid[1], |
| 165 | (unsigned int) header.uuid[2], |
| 166 | (unsigned int) header.uuid[3], |
| 167 | (unsigned int) header.uuid[4], |
| 168 | (unsigned int) header.uuid[5], |
| 169 | (unsigned int) header.uuid[6], |
| 170 | (unsigned int) header.uuid[7], |
| 171 | (unsigned int) header.uuid[8], |
| 172 | (unsigned int) header.uuid[9], |
| 173 | (unsigned int) header.uuid[10], |
| 174 | (unsigned int) header.uuid[11], |
| 175 | (unsigned int) header.uuid[12], |
| 176 | (unsigned int) header.uuid[13], |
| 177 | (unsigned int) header.uuid[14], |
| 178 | (unsigned int) header.uuid[15], |
| 179 | (unsigned int) mdec->uuid[0], |
| 180 | (unsigned int) mdec->uuid[1], |
| 181 | (unsigned int) mdec->uuid[2], |
| 182 | (unsigned int) mdec->uuid[3], |
| 183 | (unsigned int) mdec->uuid[4], |
| 184 | (unsigned int) mdec->uuid[5], |
| 185 | (unsigned int) mdec->uuid[6], |
| 186 | (unsigned int) mdec->uuid[7], |
| 187 | (unsigned int) mdec->uuid[8], |
| 188 | (unsigned int) mdec->uuid[9], |
| 189 | (unsigned int) mdec->uuid[10], |
| 190 | (unsigned int) mdec->uuid[11], |
| 191 | (unsigned int) mdec->uuid[12], |
| 192 | (unsigned int) mdec->uuid[13], |
| 193 | (unsigned int) mdec->uuid[14], |
| 194 | (unsigned int) mdec->uuid[15], |
| 195 | offset); |
| 196 | goto error; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if ((header.content_size / CHAR_BIT) < sizeof(header)) { |
| 201 | BT_LOGE("Bad metadata packet content size: content-size=%u, " |
| 202 | "offset=%ld", header.content_size, offset); |
| 203 | goto error; |
| 204 | } |
| 205 | |
| 206 | toread = header.content_size / CHAR_BIT - sizeof(header); |
| 207 | |
| 208 | for (;;) { |
| 209 | readlen = fread(buf, sizeof(uint8_t), |
| 210 | MIN(sizeof(buf) - 1, toread), in_fp); |
| 211 | if (ferror(in_fp)) { |
| 212 | BT_LOGE("Cannot read metadata packet buffer: " |
| 213 | "offset=%ld, read-size=%u", |
| 214 | ftell(in_fp), (unsigned int) readlen); |
| 215 | goto error; |
| 216 | } |
| 217 | |
| 218 | writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp); |
| 219 | if (writelen < readlen || ferror(out_fp)) { |
| 220 | BT_LOGE("Cannot write decoded metadata text to buffer: " |
| 221 | "read-offset=%ld, write-size=%u", |
| 222 | ftell(in_fp), (unsigned int) readlen); |
| 223 | goto error; |
| 224 | } |
| 225 | |
| 226 | toread -= readlen; |
| 227 | if (toread == 0) { |
| 228 | int fseek_ret; |
| 229 | |
| 230 | /* Read leftover padding */ |
| 231 | toread = (header.packet_size - header.content_size) / |
| 232 | CHAR_BIT; |
| 233 | fseek_ret = fseek(in_fp, toread, SEEK_CUR); |
| 234 | if (fseek_ret < 0) { |
| 235 | BT_LOGW_STR("Missing padding at the end of the metadata stream."); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | goto end; |
| 242 | |
| 243 | error: |
| 244 | ret = -1; |
| 245 | |
| 246 | end: |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | static |
| 251 | int ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 252 | struct ctf_metadata_decoder *mdec, FILE *fp, |
| 253 | char **buf, int byte_order) |
| 254 | { |
| 255 | FILE *out_fp; |
| 256 | size_t size; |
| 257 | int ret = 0; |
| 258 | int tret; |
| 259 | size_t packet_index = 0; |
| 260 | |
| 261 | out_fp = bt_open_memstream(buf, &size); |
| 262 | if (out_fp == NULL) { |
| 263 | BT_LOGE("Cannot open memory stream: %s: mdec-addr=%p", |
| 264 | strerror(errno), mdec); |
| 265 | goto error; |
| 266 | } |
| 267 | |
| 268 | for (;;) { |
| 269 | if (feof(fp) != 0) { |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | tret = decode_packet(mdec, fp, out_fp, byte_order); |
| 274 | if (tret) { |
| 275 | BT_LOGE("Cannot decode packet: index=%zu, mdec-addr=%p", |
| 276 | packet_index, mdec); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | packet_index++; |
| 281 | } |
| 282 | |
| 283 | /* Make sure the whole string ends with a null character */ |
| 284 | tret = fputc('\0', out_fp); |
| 285 | if (tret == EOF) { |
| 286 | BT_LOGE("Cannot append '\\0' to the decoded metadata buffer: " |
| 287 | "mdec-addr=%p", mdec); |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | /* Close stream, which also flushes the buffer */ |
| 292 | ret = bt_close_memstream(buf, &size, out_fp); |
| 293 | if (ret < 0) { |
| 294 | BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p", |
| 295 | strerror(errno), mdec); |
| 296 | goto error; |
| 297 | } |
| 298 | |
| 299 | goto end; |
| 300 | |
| 301 | error: |
| 302 | ret = -1; |
| 303 | |
| 304 | if (out_fp) { |
| 305 | if (bt_close_memstream(buf, &size, out_fp)) { |
| 306 | BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p", |
| 307 | strerror(errno), mdec); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (*buf) { |
| 312 | free(*buf); |
| 313 | *buf = NULL; |
| 314 | } |
| 315 | |
| 316 | end: |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | BT_HIDDEN |
| 321 | int ctf_metadata_decoder_packetized_file_stream_to_buf( |
| 322 | FILE *fp, char **buf, int byte_order) |
| 323 | { |
| 324 | return ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 325 | NULL, fp, buf, byte_order); |
| 326 | } |
| 327 | |
| 328 | BT_HIDDEN |
| 329 | struct ctf_metadata_decoder *ctf_metadata_decoder_create( |
| 330 | int64_t clock_class_offset_ns, const char *name) |
| 331 | { |
| 332 | struct ctf_metadata_decoder *mdec = |
| 333 | g_new0(struct ctf_metadata_decoder, 1); |
| 334 | |
| 335 | BT_LOGD("Creating CTF metadata decoder: " |
| 336 | "clock-class-offset-ns=%" PRId64 ", name=\"%s\"", |
| 337 | clock_class_offset_ns, name); |
| 338 | |
| 339 | if (!mdec) { |
| 340 | BT_LOGE_STR("Failed to allocate one CTF metadata decoder."); |
| 341 | goto end; |
| 342 | } |
| 343 | |
| 344 | mdec->visitor = ctf_visitor_generate_ir_create(clock_class_offset_ns, |
| 345 | name); |
| 346 | if (!mdec->visitor) { |
| 347 | BT_LOGE("Failed to create a CTF IR metadata AST visitor: " |
| 348 | "mdec-addr=%p", mdec); |
| 349 | ctf_metadata_decoder_destroy(mdec); |
| 350 | mdec = NULL; |
| 351 | goto end; |
| 352 | } |
| 353 | |
| 354 | BT_LOGD("Created CTF metadata decoder: " |
| 355 | "clock-class-offset-ns=%" PRId64 ", name=\"%s\", addr=%p", |
| 356 | clock_class_offset_ns, name, mdec); |
| 357 | |
| 358 | end: |
| 359 | return mdec; |
| 360 | } |
| 361 | |
| 362 | BT_HIDDEN |
| 363 | void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *mdec) |
| 364 | { |
| 365 | if (!mdec) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | BT_LOGD("Destroying CTF metadata decoder: addr=%p", mdec); |
| 370 | ctf_visitor_generate_ir_destroy(mdec->visitor); |
| 371 | g_free(mdec); |
| 372 | } |
| 373 | |
| 374 | BT_HIDDEN |
| 375 | enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( |
| 376 | struct ctf_metadata_decoder *mdec, FILE *fp) |
| 377 | { |
| 378 | enum ctf_metadata_decoder_status status = |
| 379 | CTF_METADATA_DECODER_STATUS_OK; |
| 380 | int ret; |
| 381 | struct ctf_scanner *scanner = NULL; |
| 382 | char *buf = NULL; |
| 383 | bool close_fp = false; |
| 384 | |
| 385 | assert(mdec); |
| 386 | |
| 387 | if (ctf_metadata_decoder_is_packetized(fp, &mdec->bo)) { |
| 388 | BT_LOGD("Metadata stream is packetized: mdec-addr=%p", mdec); |
| 389 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 390 | mdec, fp, &buf, mdec->bo); |
| 391 | if (ret) { |
| 392 | BT_LOGE("Cannot decode packetized metadata packets to metadata text: " |
| 393 | "mdec-addr=%p, ret=%d", mdec, ret); |
| 394 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 395 | goto end; |
| 396 | } |
| 397 | |
| 398 | if (strlen(buf) == 0) { |
| 399 | /* An empty metadata packet is OK. */ |
| 400 | goto end; |
| 401 | } |
| 402 | |
| 403 | /* Convert the real file pointer to a memory file pointer */ |
| 404 | fp = bt_fmemopen(buf, strlen(buf), "rb"); |
| 405 | close_fp = true; |
| 406 | if (!fp) { |
| 407 | BT_LOGE("Cannot memory-open metadata buffer: %s: " |
| 408 | "mdec-addr=%p", strerror(errno), mdec); |
| 409 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 410 | goto end; |
| 411 | } |
| 412 | } else { |
| 413 | unsigned int major, minor; |
| 414 | ssize_t nr_items; |
| 415 | const long init_pos = ftell(fp); |
| 416 | |
| 417 | BT_LOGD("Metadata stream is plain text: mdec-addr=%p", mdec); |
| 418 | |
| 419 | /* Check text-only metadata header and version */ |
| 420 | nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor); |
| 421 | if (nr_items < 2) { |
| 422 | BT_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: " |
| 423 | "mdec-addr=%p", mdec); |
| 424 | } |
| 425 | |
| 426 | BT_LOGD("Found metadata stream version in signature: version=%u.%u", major, minor); |
| 427 | |
| 428 | if (!is_version_valid(major, minor)) { |
| 429 | BT_LOGE("Invalid metadata version found in plain text signature: " |
| 430 | "version=%u.%u, mdec-addr=%p", major, minor, |
| 431 | mdec); |
| 432 | status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION; |
| 433 | goto end; |
| 434 | } |
| 435 | |
| 436 | if (fseek(fp, init_pos, SEEK_SET)) { |
| 437 | BT_LOGE("Cannot seek metadata file stream to initial position: %s: " |
| 438 | "mdec-addr=%p", strerror(errno), mdec); |
| 439 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 440 | goto end; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | if (BT_LOG_ON_VERBOSE) { |
| 445 | yydebug = 1; |
| 446 | } |
| 447 | |
| 448 | /* Allocate a scanner and append the metadata text content */ |
| 449 | scanner = ctf_scanner_alloc(); |
| 450 | if (!scanner) { |
| 451 | BT_LOGE("Cannot allocate a metadata lexical scanner: " |
| 452 | "mdec-addr=%p", mdec); |
| 453 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 454 | goto end; |
| 455 | } |
| 456 | |
| 457 | assert(fp); |
| 458 | ret = ctf_scanner_append_ast(scanner, fp); |
| 459 | if (ret) { |
| 460 | BT_LOGE("Cannot create the metadata AST out of the metadata text: " |
| 461 | "mdec-addr=%p", mdec); |
| 462 | status = CTF_METADATA_DECODER_STATUS_INCOMPLETE; |
| 463 | goto end; |
| 464 | } |
| 465 | |
| 466 | ret = ctf_visitor_semantic_check(0, &scanner->ast->root); |
| 467 | if (ret) { |
| 468 | BT_LOGE("Validation of the metadata semantics failed: " |
| 469 | "mdec-addr=%p", mdec); |
| 470 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | ret = ctf_visitor_generate_ir_visit_node(mdec->visitor, |
| 475 | &scanner->ast->root); |
| 476 | switch (ret) { |
| 477 | case 0: |
| 478 | /* Success */ |
| 479 | break; |
| 480 | case -EINCOMPLETE: |
| 481 | BT_LOGD("While visiting metadata AST: incomplete data: " |
| 482 | "mdec-addr=%p", mdec); |
| 483 | status = CTF_METADATA_DECODER_STATUS_INCOMPLETE; |
| 484 | goto end; |
| 485 | default: |
| 486 | BT_LOGE("Failed to visit AST node to create CTF IR objects: " |
| 487 | "mdec-addr=%p, ret=%d", mdec, ret); |
| 488 | status = CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR; |
| 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | end: |
| 493 | if (scanner) { |
| 494 | ctf_scanner_free(scanner); |
| 495 | } |
| 496 | |
| 497 | yydebug = 0; |
| 498 | |
| 499 | if (fp && close_fp) { |
| 500 | if (fclose(fp)) { |
| 501 | BT_LOGE("Cannot close metadata file stream: " |
| 502 | "mdec-addr=%p", mdec); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if (buf) { |
| 507 | free(buf); |
| 508 | } |
| 509 | |
| 510 | return status; |
| 511 | } |
| 512 | |
| 513 | BT_HIDDEN |
| 514 | struct bt_ctf_trace *ctf_metadata_decoder_get_trace( |
| 515 | struct ctf_metadata_decoder *mdec) |
| 516 | { |
| 517 | return ctf_visitor_generate_ir_get_trace(mdec->visitor); |
| 518 | } |