| 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 | #include <stdio.h> |
| 16 | #include <stdbool.h> |
| 17 | #include <stdint.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <assert.h> |
| 20 | #include <babeltrace/compat/uuid-internal.h> |
| 21 | #include <babeltrace/compat/memstream-internal.h> |
| 22 | #include <babeltrace/ctf-ir/trace.h> |
| 23 | #include <glib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "ast.h" |
| 27 | #include "decoder.h" |
| 28 | #include "scanner.h" |
| 29 | |
| 30 | #define PRINT_ERR_STREAM (mdec ? mdec->err_stream : stderr) |
| 31 | #define PRINT_PREFIX "metadata-decoder" |
| 32 | #include "../print.h" |
| 33 | |
| 34 | #define TSDL_MAGIC 0x75d11d57 |
| 35 | |
| 36 | struct ctf_metadata_decoder { |
| 37 | struct ctf_visitor_generate_ir *visitor; |
| 38 | FILE *err_stream; |
| 39 | uint8_t uuid[16]; |
| 40 | bool is_uuid_set; |
| 41 | int bo; |
| 42 | }; |
| 43 | |
| 44 | struct packet_header { |
| 45 | uint32_t magic; |
| 46 | uint8_t uuid[16]; |
| 47 | uint32_t checksum; |
| 48 | uint32_t content_size; |
| 49 | uint32_t packet_size; |
| 50 | uint8_t compression_scheme; |
| 51 | uint8_t encryption_scheme; |
| 52 | uint8_t checksum_scheme; |
| 53 | uint8_t major; |
| 54 | uint8_t minor; |
| 55 | } __attribute__((__packed__)); |
| 56 | |
| 57 | BT_HIDDEN |
| 58 | bool ctf_metadata_decoder_is_packetized(FILE *fp, int *byte_order) |
| 59 | { |
| 60 | uint32_t magic; |
| 61 | size_t len; |
| 62 | int ret = 0; |
| 63 | |
| 64 | len = fread(&magic, sizeof(magic), 1, fp); |
| 65 | if (len != 1) { |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | if (byte_order) { |
| 70 | if (magic == TSDL_MAGIC) { |
| 71 | ret = 1; |
| 72 | *byte_order = BYTE_ORDER; |
| 73 | } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) { |
| 74 | ret = 1; |
| 75 | *byte_order = BYTE_ORDER == BIG_ENDIAN ? |
| 76 | LITTLE_ENDIAN : BIG_ENDIAN; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | end: |
| 81 | rewind(fp); |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | bool is_version_valid(unsigned int major, unsigned int minor) |
| 88 | { |
| 89 | return major == 1 && minor == 8; |
| 90 | } |
| 91 | |
| 92 | static |
| 93 | int decode_packet(struct ctf_metadata_decoder *mdec, FILE *in_fp, FILE *out_fp, |
| 94 | int byte_order) |
| 95 | { |
| 96 | struct packet_header header; |
| 97 | size_t readlen, writelen, toread; |
| 98 | uint8_t buf[512 + 1]; /* + 1 for debug-mode \0 */ |
| 99 | int ret = 0; |
| 100 | |
| 101 | readlen = fread(&header, sizeof(header), 1, in_fp); |
| 102 | if (feof(in_fp) != 0) { |
| 103 | goto end; |
| 104 | } |
| 105 | if (readlen < 1) { |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | if (byte_order != BYTE_ORDER) { |
| 110 | header.magic = GUINT32_SWAP_LE_BE(header.magic); |
| 111 | header.checksum = GUINT32_SWAP_LE_BE(header.checksum); |
| 112 | header.content_size = GUINT32_SWAP_LE_BE(header.content_size); |
| 113 | header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size); |
| 114 | } |
| 115 | |
| 116 | if (header.compression_scheme) { |
| 117 | PERR("Metadata packet compression not supported yet\n"); |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | if (header.encryption_scheme) { |
| 122 | PERR("Metadata packet encryption not supported yet\n"); |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | if (header.checksum || header.checksum_scheme) { |
| 127 | PERR("Metadata packet checksum verification not supported yet\n"); |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | if (!is_version_valid(header.major, header.minor)) { |
| 132 | PERR("Invalid metadata version: %u.%u\n", header.major, |
| 133 | header.minor); |
| 134 | goto error; |
| 135 | } |
| 136 | |
| 137 | /* Set expected trace UUID if not set; otherwise validate it */ |
| 138 | if (mdec) { |
| 139 | if (!mdec->is_uuid_set) { |
| 140 | memcpy(mdec->uuid, header.uuid, sizeof(header.uuid)); |
| 141 | mdec->is_uuid_set = true; |
| 142 | } else if (bt_uuid_compare(header.uuid, mdec->uuid)) { |
| 143 | PERR("Metadata UUID mismatch between packets of the same stream\n"); |
| 144 | goto error; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if ((header.content_size / CHAR_BIT) < sizeof(header)) { |
| 149 | PERR("Bad metadata packet content size: %u\n", |
| 150 | header.content_size); |
| 151 | goto error; |
| 152 | } |
| 153 | |
| 154 | toread = header.content_size / CHAR_BIT - sizeof(header); |
| 155 | |
| 156 | for (;;) { |
| 157 | readlen = fread(buf, sizeof(uint8_t), |
| 158 | MIN(sizeof(buf) - 1, toread), in_fp); |
| 159 | if (ferror(in_fp)) { |
| 160 | PERR("Cannot read metadata packet buffer (at position %ld)\n", |
| 161 | ftell(in_fp)); |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | writelen = fwrite(buf, sizeof(uint8_t), readlen, out_fp); |
| 166 | if (writelen < readlen || ferror(out_fp)) { |
| 167 | PERR("Cannot write decoded metadata text to buffer\n"); |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | toread -= readlen; |
| 172 | if (toread == 0) { |
| 173 | int fseek_ret; |
| 174 | |
| 175 | /* Read leftover padding */ |
| 176 | toread = (header.packet_size - header.content_size) / |
| 177 | CHAR_BIT; |
| 178 | fseek_ret = fseek(in_fp, toread, SEEK_CUR); |
| 179 | if (fseek_ret < 0) { |
| 180 | PWARN("Missing padding at the end of the metadata file\n"); |
| 181 | } |
| 182 | |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | goto end; |
| 188 | |
| 189 | error: |
| 190 | ret = -1; |
| 191 | |
| 192 | end: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static |
| 197 | int ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 198 | struct ctf_metadata_decoder *mdec, FILE *fp, |
| 199 | char **buf, int byte_order) |
| 200 | { |
| 201 | FILE *out_fp; |
| 202 | size_t size; |
| 203 | int ret = 0; |
| 204 | int tret; |
| 205 | size_t packet_index = 0; |
| 206 | |
| 207 | out_fp = bt_open_memstream(buf, &size); |
| 208 | if (out_fp == NULL) { |
| 209 | PERR("Cannot open memory stream: %s\n", strerror(errno)); |
| 210 | goto error; |
| 211 | } |
| 212 | |
| 213 | for (;;) { |
| 214 | if (feof(fp) != 0) { |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | tret = decode_packet(mdec, fp, out_fp, byte_order); |
| 219 | if (tret) { |
| 220 | PERR("Cannot decode packet #%zu\n", packet_index); |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | packet_index++; |
| 225 | } |
| 226 | |
| 227 | /* Make sure the whole string ends with a null character */ |
| 228 | tret = fputc('\0', out_fp); |
| 229 | if (tret == EOF) { |
| 230 | PERR("Cannot append '\\0' to the decoded metadata buffer\n"); |
| 231 | goto error; |
| 232 | } |
| 233 | |
| 234 | /* Close stream, which also flushes the buffer */ |
| 235 | ret = bt_close_memstream(buf, &size, out_fp); |
| 236 | if (ret < 0) { |
| 237 | PERR("Cannot close memory stream: %s\n", strerror(errno)); |
| 238 | goto error; |
| 239 | } |
| 240 | |
| 241 | goto end; |
| 242 | |
| 243 | error: |
| 244 | ret = -1; |
| 245 | |
| 246 | if (out_fp) { |
| 247 | bt_close_memstream(buf, &size, out_fp); |
| 248 | } |
| 249 | |
| 250 | if (*buf) { |
| 251 | free(*buf); |
| 252 | *buf = NULL; |
| 253 | } |
| 254 | |
| 255 | end: |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | BT_HIDDEN |
| 260 | int ctf_metadata_decoder_packetized_file_stream_to_buf( |
| 261 | FILE *fp, char **buf, int byte_order) |
| 262 | { |
| 263 | return ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 264 | NULL, fp, buf, byte_order); |
| 265 | } |
| 266 | |
| 267 | BT_HIDDEN |
| 268 | struct ctf_metadata_decoder *ctf_metadata_decoder_create(FILE *err, |
| 269 | uint64_t clock_class_offset_ns, const char *name) |
| 270 | { |
| 271 | struct ctf_metadata_decoder *mdec = |
| 272 | g_new0(struct ctf_metadata_decoder, 1); |
| 273 | |
| 274 | if (!mdec) { |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | mdec->err_stream = err; |
| 279 | mdec->visitor = ctf_visitor_generate_ir_create(err, |
| 280 | clock_class_offset_ns, name); |
| 281 | if (!mdec->visitor) { |
| 282 | ctf_metadata_decoder_destroy(mdec); |
| 283 | mdec = NULL; |
| 284 | goto end; |
| 285 | } |
| 286 | |
| 287 | end: |
| 288 | return mdec; |
| 289 | } |
| 290 | |
| 291 | BT_HIDDEN |
| 292 | void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *mdec) |
| 293 | { |
| 294 | if (!mdec) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | ctf_visitor_generate_ir_destroy(mdec->visitor); |
| 299 | g_free(mdec); |
| 300 | } |
| 301 | |
| 302 | BT_HIDDEN |
| 303 | enum ctf_metadata_decoder_status ctf_metadata_decoder_decode( |
| 304 | struct ctf_metadata_decoder *mdec, FILE *fp) |
| 305 | { |
| 306 | enum ctf_metadata_decoder_status status = |
| 307 | CTF_METADATA_DECODER_STATUS_OK; |
| 308 | int ret; |
| 309 | struct ctf_scanner *scanner = NULL; |
| 310 | char *buf = NULL; |
| 311 | bool close_fp = false; |
| 312 | |
| 313 | assert(mdec); |
| 314 | |
| 315 | if (ctf_metadata_decoder_is_packetized(fp, &mdec->bo)) { |
| 316 | PDBG("Metadata stream is packetized\n"); |
| 317 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec( |
| 318 | mdec, fp, &buf, mdec->bo); |
| 319 | if (ret) { |
| 320 | // log: details |
| 321 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 322 | goto end; |
| 323 | } |
| 324 | |
| 325 | if (strlen(buf) == 0) { |
| 326 | /* An empty metadata packet is OK. */ |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | /* Convert the real file pointer to a memory file pointer */ |
| 331 | fp = bt_fmemopen(buf, strlen(buf), "rb"); |
| 332 | close_fp = true; |
| 333 | if (!fp) { |
| 334 | PERR("Cannot memory-open metadata buffer: %s\n", |
| 335 | strerror(errno)); |
| 336 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 337 | goto end; |
| 338 | } |
| 339 | } else { |
| 340 | unsigned int major, minor; |
| 341 | ssize_t nr_items; |
| 342 | const long init_pos = ftell(fp); |
| 343 | |
| 344 | PDBG("Metadata stream is plain text\n"); |
| 345 | |
| 346 | /* Check text-only metadata header and version */ |
| 347 | nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor); |
| 348 | if (nr_items < 2) { |
| 349 | PWARN("Ill-shapen or missing \"/* CTF major.minor\" header in plain text metadata file stream\n"); |
| 350 | } |
| 351 | |
| 352 | PDBG("Metadata version: %u.%u\n", major, minor); |
| 353 | |
| 354 | if (!is_version_valid(major, minor)) { |
| 355 | PERR("Invalid metadata version: %u.%u\n", major, minor); |
| 356 | status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION; |
| 357 | goto end; |
| 358 | } |
| 359 | |
| 360 | if (fseek(fp, init_pos, SEEK_SET)) { |
| 361 | PERR("Cannot seek metadata file stream to initial position: %s\n", |
| 362 | strerror(errno)); |
| 363 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 364 | goto end; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /* Allocate a scanner and append the metadata text content */ |
| 369 | scanner = ctf_scanner_alloc(); |
| 370 | if (!scanner) { |
| 371 | PERR("Cannot allocate a metadata lexical scanner\n"); |
| 372 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 373 | goto end; |
| 374 | } |
| 375 | |
| 376 | assert(fp); |
| 377 | ret = ctf_scanner_append_ast(scanner, fp); |
| 378 | if (ret) { |
| 379 | PERR("Cannot create the metadata AST\n"); |
| 380 | status = CTF_METADATA_DECODER_STATUS_INCOMPLETE; |
| 381 | goto end; |
| 382 | } |
| 383 | |
| 384 | ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root); |
| 385 | if (ret) { |
| 386 | PERR("Metadata semantic validation failed\n"); |
| 387 | status = CTF_METADATA_DECODER_STATUS_ERROR; |
| 388 | goto end; |
| 389 | } |
| 390 | |
| 391 | ret = ctf_visitor_generate_ir_visit_node(mdec->visitor, |
| 392 | &scanner->ast->root); |
| 393 | switch (ret) { |
| 394 | case 0: |
| 395 | /* Success */ |
| 396 | break; |
| 397 | case -EINCOMPLETE: |
| 398 | PDBG("While visiting AST: incomplete data\n"); |
| 399 | status = CTF_METADATA_DECODER_STATUS_INCOMPLETE; |
| 400 | goto end; |
| 401 | default: |
| 402 | PERR("Cannot visit AST node to create CTF IR objects\n"); |
| 403 | status = CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR; |
| 404 | goto end; |
| 405 | } |
| 406 | |
| 407 | end: |
| 408 | if (scanner) { |
| 409 | ctf_scanner_free(scanner); |
| 410 | } |
| 411 | |
| 412 | if (fp && close_fp) { |
| 413 | if (fclose(fp)) { |
| 414 | PERR("Cannot close metadata file stream\n"); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (buf) { |
| 419 | free(buf); |
| 420 | } |
| 421 | |
| 422 | return status; |
| 423 | } |
| 424 | |
| 425 | BT_HIDDEN |
| 426 | struct bt_ctf_trace *ctf_metadata_decoder_get_trace( |
| 427 | struct ctf_metadata_decoder *mdec) |
| 428 | { |
| 429 | return ctf_visitor_generate_ir_get_trace(mdec->visitor); |
| 430 | } |