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