2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
15 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-DECODER"
24 #include <babeltrace/compat/uuid-internal.h>
25 #include <babeltrace/compat/memstream-internal.h>
26 #include <babeltrace/ctf-ir/trace.h>
34 #define TSDL_MAGIC 0x75d11d57
39 struct ctf_metadata_decoder
{
40 struct ctf_visitor_generate_ir
*visitor
;
46 struct packet_header
{
50 uint32_t content_size
;
52 uint8_t compression_scheme
;
53 uint8_t encryption_scheme
;
54 uint8_t checksum_scheme
;
57 } __attribute__((__packed__
));
60 bool ctf_metadata_decoder_is_packetized(FILE *fp
, int *byte_order
)
66 len
= fread(&magic
, sizeof(magic
), 1, fp
);
68 BT_LOGD_STR("Cannot reade first metadata packet header: assuming the stream is not packetized.");
73 if (magic
== TSDL_MAGIC
) {
75 *byte_order
= BYTE_ORDER
;
76 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
78 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
79 LITTLE_ENDIAN
: BIG_ENDIAN
;
90 bool is_version_valid(unsigned int major
, unsigned int minor
)
92 return major
== 1 && minor
== 8;
96 int decode_packet(struct ctf_metadata_decoder
*mdec
, FILE *in_fp
, FILE *out_fp
,
99 struct packet_header header
;
100 size_t readlen
, writelen
, toread
;
101 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
103 const long offset
= ftell(in_fp
);
105 BT_LOGV("Decoding metadata packet: mdec-addr=%p, offset=%ld",
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
));
113 BT_LOGV("Cannot decode metadata packet: offset=%ld", offset
);
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
);
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
);
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
);
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
,
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
);
153 /* Set expected trace UUID if not set; otherwise validate it */
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\", "
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],
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
);
206 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
209 readlen
= fread(buf
, sizeof(uint8_t),
210 MIN(sizeof(buf
) - 1, toread
), in_fp
);
212 BT_LOGE("Cannot read metadata packet buffer: "
213 "offset=%ld, read-size=%u",
214 ftell(in_fp
), (unsigned int) readlen
);
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
);
230 /* Read leftover padding */
231 toread
= (header
.packet_size
- header
.content_size
) /
233 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
235 BT_LOGW_STR("Missing padding at the end of the metadata stream.");
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
)
259 size_t packet_index
= 0;
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
);
273 tret
= decode_packet(mdec
, fp
, out_fp
, byte_order
);
275 BT_LOGE("Cannot decode packet: index=%zu, mdec-addr=%p",
283 /* Make sure the whole string ends with a null character */
284 tret
= fputc('\0', out_fp
);
286 BT_LOGE("Cannot append '\\0' to the decoded metadata buffer: "
287 "mdec-addr=%p", mdec
);
291 /* Close stream, which also flushes the buffer */
292 ret
= bt_close_memstream(buf
, &size
, out_fp
);
294 BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p",
295 strerror(errno
), mdec
);
305 if (bt_close_memstream(buf
, &size
, out_fp
)) {
306 BT_LOGE("Cannot close memory stream: %s: mdec-addr=%p",
307 strerror(errno
), mdec
);
321 int ctf_metadata_decoder_packetized_file_stream_to_buf(
322 FILE *fp
, char **buf
, int byte_order
)
324 return ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
325 NULL
, fp
, buf
, byte_order
);
329 struct ctf_metadata_decoder
*ctf_metadata_decoder_create(
330 int64_t clock_class_offset_ns
, const char *name
)
332 struct ctf_metadata_decoder
*mdec
=
333 g_new0(struct ctf_metadata_decoder
, 1);
335 BT_LOGD("Creating CTF metadata decoder: "
336 "clock-class-offset-ns=%" PRId64
", name=\"%s\"",
337 clock_class_offset_ns
, name
);
340 BT_LOGE_STR("Failed to allocate one CTF metadata decoder.");
344 mdec
->visitor
= ctf_visitor_generate_ir_create(clock_class_offset_ns
,
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
);
354 BT_LOGD("Created CTF metadata decoder: "
355 "clock-class-offset-ns=%" PRId64
", name=\"%s\", addr=%p",
356 clock_class_offset_ns
, name
, mdec
);
363 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder
*mdec
)
369 BT_LOGD("Destroying CTF metadata decoder: addr=%p", mdec
);
370 ctf_visitor_generate_ir_destroy(mdec
->visitor
);
375 enum ctf_metadata_decoder_status
ctf_metadata_decoder_decode(
376 struct ctf_metadata_decoder
*mdec
, FILE *fp
)
378 enum ctf_metadata_decoder_status status
=
379 CTF_METADATA_DECODER_STATUS_OK
;
381 struct ctf_scanner
*scanner
= NULL
;
383 bool close_fp
= false;
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
);
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
;
398 if (strlen(buf
) == 0) {
399 /* An empty metadata packet is OK. */
403 /* Convert the real file pointer to a memory file pointer */
404 fp
= bt_fmemopen(buf
, strlen(buf
), "rb");
407 BT_LOGE("Cannot memory-open metadata buffer: %s: "
408 "mdec-addr=%p", strerror(errno
), mdec
);
409 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
413 unsigned int major
, minor
;
415 const long init_pos
= ftell(fp
);
417 BT_LOGD("Metadata stream is plain text: mdec-addr=%p", mdec
);
419 /* Check text-only metadata header and version */
420 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
422 BT_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
423 "mdec-addr=%p", mdec
);
426 BT_LOGD("Found metadata stream version in signature: version=%u.%u", major
, minor
);
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
,
432 status
= CTF_METADATA_DECODER_STATUS_INVAL_VERSION
;
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
;
444 if (BT_LOG_ON_VERBOSE
) {
448 /* Allocate a scanner and append the metadata text content */
449 scanner
= ctf_scanner_alloc();
451 BT_LOGE("Cannot allocate a metadata lexical scanner: "
452 "mdec-addr=%p", mdec
);
453 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
458 ret
= ctf_scanner_append_ast(scanner
, fp
);
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
;
466 ret
= ctf_visitor_semantic_check(0, &scanner
->ast
->root
);
468 BT_LOGE("Validation of the metadata semantics failed: "
469 "mdec-addr=%p", mdec
);
470 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
474 ret
= ctf_visitor_generate_ir_visit_node(mdec
->visitor
,
475 &scanner
->ast
->root
);
481 BT_LOGD("While visiting metadata AST: incomplete data: "
482 "mdec-addr=%p", mdec
);
483 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
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
;
494 ctf_scanner_free(scanner
);
499 if (fp
&& close_fp
) {
501 BT_LOGE("Cannot close metadata file stream: "
502 "mdec-addr=%p", mdec
);
514 struct bt_ctf_trace
*ctf_metadata_decoder_get_trace(
515 struct ctf_metadata_decoder
*mdec
)
517 return ctf_visitor_generate_ir_get_trace(mdec
->visitor
);