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