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