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