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.
20 #include <babeltrace/compat/uuid-internal.h>
21 #include <babeltrace/compat/memstream-internal.h>
22 #include <babeltrace/ctf-ir/trace.h>
30 #define BT_LOG_TAG "PLUGIN-CTF-METADATA-DECODER"
33 #define TSDL_MAGIC 0x75d11d57
38 struct ctf_metadata_decoder
{
39 struct ctf_visitor_generate_ir
*visitor
;
45 struct packet_header
{
49 uint32_t content_size
;
51 uint8_t compression_scheme
;
52 uint8_t encryption_scheme
;
53 uint8_t checksum_scheme
;
56 } __attribute__((__packed__
));
59 bool ctf_metadata_decoder_is_packetized(FILE *fp
, int *byte_order
)
65 len
= fread(&magic
, sizeof(magic
), 1, fp
);
71 if (magic
== TSDL_MAGIC
) {
73 *byte_order
= BYTE_ORDER
;
74 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
76 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
77 LITTLE_ENDIAN
: BIG_ENDIAN
;
88 bool is_version_valid(unsigned int major
, unsigned int minor
)
90 return major
== 1 && minor
== 8;
94 int decode_packet(struct ctf_metadata_decoder
*mdec
, FILE *in_fp
, FILE *out_fp
,
97 struct packet_header header
;
98 size_t readlen
, writelen
, toread
;
99 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
102 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
103 if (feof(in_fp
) != 0) {
110 if (byte_order
!= BYTE_ORDER
) {
111 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
112 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
113 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
114 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
117 if (header
.compression_scheme
) {
118 BT_LOGE("Metadata packet compression not supported yet");
122 if (header
.encryption_scheme
) {
123 BT_LOGE("Metadata packet encryption not supported yet");
127 if (header
.checksum
|| header
.checksum_scheme
) {
128 BT_LOGE("Metadata packet checksum verification not supported yet");
132 if (!is_version_valid(header
.major
, header
.minor
)) {
133 BT_LOGE("Invalid metadata version: %u.%u", header
.major
,
138 /* Set expected trace UUID if not set; otherwise validate it */
140 if (!mdec
->is_uuid_set
) {
141 memcpy(mdec
->uuid
, header
.uuid
, sizeof(header
.uuid
));
142 mdec
->is_uuid_set
= true;
143 } else if (bt_uuid_compare(header
.uuid
, mdec
->uuid
)) {
144 BT_LOGE("Metadata UUID mismatch between packets of the same stream");
149 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
150 BT_LOGE("Bad metadata packet content size: %u",
151 header
.content_size
);
155 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
158 readlen
= fread(buf
, sizeof(uint8_t),
159 MIN(sizeof(buf
) - 1, toread
), in_fp
);
161 BT_LOGE("Cannot read metadata packet buffer (at position %ld)",
166 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
167 if (writelen
< readlen
|| ferror(out_fp
)) {
168 BT_LOGE("Cannot write decoded metadata text to buffer");
176 /* Read leftover padding */
177 toread
= (header
.packet_size
- header
.content_size
) /
179 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
181 BT_LOGW("Missing padding at the end of the metadata file");
198 int ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
199 struct ctf_metadata_decoder
*mdec
, FILE *fp
,
200 char **buf
, int byte_order
)
206 size_t packet_index
= 0;
208 out_fp
= bt_open_memstream(buf
, &size
);
209 if (out_fp
== NULL
) {
210 BT_LOGE("Cannot open memory stream: %s", strerror(errno
));
219 tret
= decode_packet(mdec
, fp
, out_fp
, byte_order
);
221 BT_LOGE("Cannot decode packet #%zu", packet_index
);
228 /* Make sure the whole string ends with a null character */
229 tret
= fputc('\0', out_fp
);
231 BT_LOGE("Cannot append '\\0' to the decoded metadata buffer");
235 /* Close stream, which also flushes the buffer */
236 ret
= bt_close_memstream(buf
, &size
, out_fp
);
238 BT_LOGE("Cannot close memory stream: %s", strerror(errno
));
248 bt_close_memstream(buf
, &size
, out_fp
);
261 int ctf_metadata_decoder_packetized_file_stream_to_buf(
262 FILE *fp
, char **buf
, int byte_order
)
264 return ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
265 NULL
, fp
, buf
, byte_order
);
269 struct ctf_metadata_decoder
*ctf_metadata_decoder_create(
270 int64_t clock_class_offset_ns
, const char *name
)
272 struct ctf_metadata_decoder
*mdec
=
273 g_new0(struct ctf_metadata_decoder
, 1);
279 mdec
->visitor
= ctf_visitor_generate_ir_create(clock_class_offset_ns
,
281 if (!mdec
->visitor
) {
282 ctf_metadata_decoder_destroy(mdec
);
292 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder
*mdec
)
298 ctf_visitor_generate_ir_destroy(mdec
->visitor
);
303 enum ctf_metadata_decoder_status
ctf_metadata_decoder_decode(
304 struct ctf_metadata_decoder
*mdec
, FILE *fp
)
306 enum ctf_metadata_decoder_status status
=
307 CTF_METADATA_DECODER_STATUS_OK
;
309 struct ctf_scanner
*scanner
= NULL
;
311 bool close_fp
= false;
315 if (ctf_metadata_decoder_is_packetized(fp
, &mdec
->bo
)) {
316 BT_LOGD("Metadata stream is packetized");
317 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf_with_mdec(
318 mdec
, fp
, &buf
, mdec
->bo
);
321 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
325 if (strlen(buf
) == 0) {
326 /* An empty metadata packet is OK. */
330 /* Convert the real file pointer to a memory file pointer */
331 fp
= bt_fmemopen(buf
, strlen(buf
), "rb");
334 BT_LOGE("Cannot memory-open metadata buffer: %s",
336 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
340 unsigned int major
, minor
;
342 const long init_pos
= ftell(fp
);
344 BT_LOGD("Metadata stream is plain text");
346 /* Check text-only metadata header and version */
347 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
349 BT_LOGW("Ill-shapen or missing \"/* CTF major.minor\" header in plain text metadata file stream");
352 BT_LOGD("Metadata version: %u.%u", major
, minor
);
354 if (!is_version_valid(major
, minor
)) {
355 BT_LOGE("Invalid metadata version: %u.%u", major
, minor
);
356 status
= CTF_METADATA_DECODER_STATUS_INVAL_VERSION
;
360 if (fseek(fp
, init_pos
, SEEK_SET
)) {
361 BT_LOGE("Cannot seek metadata file stream to initial position: %s",
363 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
368 if (BT_LOG_ON_VERBOSE
) {
372 /* Allocate a scanner and append the metadata text content */
373 scanner
= ctf_scanner_alloc();
375 BT_LOGE("Cannot allocate a metadata lexical scanner");
376 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
381 ret
= ctf_scanner_append_ast(scanner
, fp
);
383 BT_LOGE("Cannot create the metadata AST");
384 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
388 ret
= ctf_visitor_semantic_check(0, &scanner
->ast
->root
);
390 BT_LOGE("Metadata semantic validation failed");
391 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
395 ret
= ctf_visitor_generate_ir_visit_node(mdec
->visitor
,
396 &scanner
->ast
->root
);
402 BT_LOGD("While visiting AST: incomplete data");
403 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
406 BT_LOGE("Cannot visit AST node to create CTF IR objects");
407 status
= CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR
;
413 ctf_scanner_free(scanner
);
418 if (fp
&& close_fp
) {
420 BT_LOGE("Cannot close metadata file stream");
432 struct bt_ctf_trace
*ctf_metadata_decoder_get_trace(
433 struct ctf_metadata_decoder
*mdec
)
435 return ctf_visitor_generate_ir_get_trace(mdec
->visitor
);