a62beebfcd99cce5dacafe88d96883c8492c14b4
[babeltrace.git] / src / 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 #define BT_COMP_LOG_SELF_COMP (mdec->config.self_comp)
16 #define BT_LOG_OUTPUT_LEVEL (mdec->config.log_level)
17 #define BT_LOG_TAG "PLUGIN/CTF/META/DECODER"
18 #include "logging/comp-logging.h"
19
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <inttypes.h>
25 #include "common/assert.h"
26 #include "common/uuid.h"
27 #include "compat/memstream.h"
28 #include <babeltrace2/babeltrace.h>
29 #include <glib.h>
30 #include <string.h>
31
32 #include "ast.h"
33 #include "decoder.h"
34 #include "scanner.h"
35 #include "logging.h"
36 #include "parser-wrap.h"
37
38 #define TSDL_MAGIC 0x75d11d57
39
40 struct ctf_metadata_decoder {
41 struct ctf_scanner *scanner;
42 GString *text;
43 struct ctf_visitor_generate_ir *visitor;
44 bt_uuid_t uuid;
45 bool is_uuid_set;
46 int bo;
47 struct ctf_metadata_decoder_config config;
48 struct meta_log_config log_cfg;
49 };
50
51 struct packet_header {
52 uint32_t magic;
53 bt_uuid_t uuid;
54 uint32_t checksum;
55 uint32_t content_size;
56 uint32_t packet_size;
57 uint8_t compression_scheme;
58 uint8_t encryption_scheme;
59 uint8_t checksum_scheme;
60 uint8_t major;
61 uint8_t minor;
62 } __attribute__((__packed__));
63
64 BT_HIDDEN
65 int ctf_metadata_decoder_packetized_file_stream_to_buf(FILE *fp,
66 char **buf, int byte_order, bool *is_uuid_set,
67 uint8_t *uuid, bt_logging_level log_level,
68 bt_self_component *self_comp);
69
70 BT_HIDDEN
71 int ctf_metadata_decoder_is_packetized(FILE *fp, bool *is_packetized,
72 int *byte_order, bt_logging_level log_level,
73 bt_self_component *self_comp)
74 {
75 uint32_t magic;
76 size_t len;
77 int ret = 0;
78
79 *is_packetized = false;
80 len = fread(&magic, sizeof(magic), 1, fp);
81 if (len != 1) {
82 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO, log_level, self_comp,
83 "Cannot read first metadata packet header: assuming the stream is not packetized.");
84 ret = -1;
85 goto end;
86 }
87
88 if (byte_order) {
89 if (magic == TSDL_MAGIC) {
90 *is_packetized = true;
91 *byte_order = BYTE_ORDER;
92 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
93 *is_packetized = true;
94 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
95 LITTLE_ENDIAN : BIG_ENDIAN;
96 }
97 }
98
99 end:
100 rewind(fp);
101
102 return ret;
103 }
104
105 BT_HIDDEN
106 struct ctf_metadata_decoder *ctf_metadata_decoder_create(
107 const struct ctf_metadata_decoder_config *config)
108 {
109 struct ctf_metadata_decoder *mdec =
110 g_new0(struct ctf_metadata_decoder, 1);
111
112 BT_ASSERT(config);
113 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG, config->log_level, config->self_comp,
114 "Creating CTF metadata decoder: "
115 "clock-class-offset-s=%" PRId64 ", "
116 "clock-class-offset-ns=%" PRId64,
117 config->clock_class_offset_s, config->clock_class_offset_ns);
118
119 if (!mdec) {
120 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, config->log_level,
121 config->self_comp,
122 "Failed to allocate one CTF metadata decoder.");
123 goto end;
124 }
125
126 mdec->log_cfg.log_level = config->log_level;
127 mdec->log_cfg.self_comp = config->self_comp;
128 mdec->scanner = ctf_scanner_alloc();
129 if (!mdec->scanner) {
130 BT_COMP_LOGE("Cannot allocate a metadata lexical scanner: "
131 "mdec-addr=%p", mdec);
132 goto error;
133 }
134
135 mdec->text = g_string_new(NULL);
136 if (!mdec->text) {
137 BT_COMP_LOGE("Failed to allocate one GString: "
138 "mdec-addr=%p", mdec);
139 goto error;
140 }
141
142 mdec->bo = -1;
143 mdec->config = *config;
144 mdec->visitor = ctf_visitor_generate_ir_create(config);
145 if (!mdec->visitor) {
146 BT_COMP_LOGE("Failed to create a CTF IR metadata AST visitor: "
147 "mdec-addr=%p", mdec);
148 goto error;
149 }
150
151 BT_COMP_LOGD("Creating CTF metadata decoder: "
152 "clock-class-offset-s=%" PRId64 ", "
153 "clock-class-offset-ns=%" PRId64 ", addr=%p",
154 config->clock_class_offset_s, config->clock_class_offset_ns,
155 mdec);
156 goto end;
157
158 error:
159 ctf_metadata_decoder_destroy(mdec);
160 mdec = NULL;
161
162 end:
163 return mdec;
164 }
165
166 BT_HIDDEN
167 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *mdec)
168 {
169 if (!mdec) {
170 return;
171 }
172
173 if (mdec->scanner) {
174 ctf_scanner_free(mdec->scanner);
175 }
176
177 if (mdec->text) {
178 g_string_free(mdec->text, TRUE);
179 }
180
181 BT_COMP_LOGD("Destroying CTF metadata decoder: addr=%p", mdec);
182 ctf_visitor_generate_ir_destroy(mdec->visitor);
183 g_free(mdec);
184 }
185
186 BT_HIDDEN
187 enum ctf_metadata_decoder_status ctf_metadata_decoder_append_content(
188 struct ctf_metadata_decoder *mdec, FILE *fp)
189 {
190 enum ctf_metadata_decoder_status status =
191 CTF_METADATA_DECODER_STATUS_OK;
192 int ret;
193 char *buf = NULL;
194 bool close_fp = false;
195 long start_pos = -1;
196 bool is_packetized;
197
198 BT_ASSERT(mdec);
199 ret = ctf_metadata_decoder_is_packetized(fp, &is_packetized, &mdec->bo,
200 mdec->config.log_level, mdec->config.self_comp);
201 if (ret) {
202 status = CTF_METADATA_DECODER_STATUS_ERROR;
203 goto end;
204 }
205
206 if (is_packetized) {
207 BT_COMP_LOGI("Metadata stream is packetized: mdec-addr=%p", mdec);
208 ret = ctf_metadata_decoder_packetized_file_stream_to_buf(fp,
209 &buf, mdec->bo, &mdec->is_uuid_set,
210 mdec->uuid, mdec->config.log_level,
211 mdec->config.self_comp);
212 if (ret) {
213 BT_COMP_LOGE("Cannot decode packetized metadata packets to metadata text: "
214 "mdec-addr=%p, ret=%d", mdec, ret);
215 status = CTF_METADATA_DECODER_STATUS_ERROR;
216 goto end;
217 }
218
219 if (strlen(buf) == 0) {
220 /* An empty metadata packet is OK. */
221 goto end;
222 }
223
224 /* Convert the real file pointer to a memory file pointer */
225 fp = bt_fmemopen(buf, strlen(buf), "rb");
226 close_fp = true;
227 if (!fp) {
228 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
229 "mdec-addr=%p", strerror(errno), mdec);
230 status = CTF_METADATA_DECODER_STATUS_ERROR;
231 goto end;
232 }
233 } else {
234 unsigned int major, minor;
235 ssize_t nr_items;
236 const long init_pos = ftell(fp);
237
238 BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec);
239
240 if (init_pos < 0) {
241 BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
242 status = CTF_METADATA_DECODER_STATUS_ERROR;
243 goto end;
244 }
245
246 /* Check text-only metadata header and version */
247 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
248 if (nr_items < 2) {
249 BT_COMP_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
250 "mdec-addr=%p", mdec);
251 }
252
253 BT_COMP_LOGI("Found metadata stream version in signature: version=%u.%u", major, minor);
254
255 if (!ctf_metadata_decoder_is_packet_version_valid(major,
256 minor)) {
257 BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
258 "version=%u.%u, mdec-addr=%p", major, minor,
259 mdec);
260 status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION;
261 goto end;
262 }
263
264 if (fseek(fp, init_pos, SEEK_SET)) {
265 BT_COMP_LOGE("Cannot seek metadata file stream to initial position: %s: "
266 "mdec-addr=%p", strerror(errno), mdec);
267 status = CTF_METADATA_DECODER_STATUS_ERROR;
268 goto end;
269 }
270 }
271
272 #if YYDEBUG
273 if (BT_LOG_ON_TRACE) {
274 yydebug = 1;
275 }
276 #endif
277
278 /* Save the file's position: we'll seek back to append the plain text */
279 BT_ASSERT(fp);
280
281 if (mdec->config.keep_plain_text) {
282 start_pos = ftell(fp);
283 }
284
285 /* Append the metadata text content */
286 ret = ctf_scanner_append_ast(mdec->scanner, fp);
287 if (ret) {
288 BT_COMP_LOGE("Cannot create the metadata AST out of the metadata text: "
289 "mdec-addr=%p", mdec);
290 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
291 goto end;
292 }
293
294 /* We know it's complete: append plain text */
295 if (mdec->config.keep_plain_text) {
296 BT_ASSERT(start_pos != -1);
297 ret = fseek(fp, start_pos, SEEK_SET);
298 if (ret) {
299 BT_COMP_LOGE("Failed to seek file: ret=%d, mdec-addr=%p",
300 ret, mdec);
301 status = CTF_METADATA_DECODER_STATUS_ERROR;
302 goto end;
303 }
304
305 ret = bt_common_append_file_content_to_g_string(mdec->text, fp);
306 if (ret) {
307 BT_COMP_LOGE("Failed to append to current plain text: "
308 "ret=%d, mdec-addr=%p", ret, mdec);
309 status = CTF_METADATA_DECODER_STATUS_ERROR;
310 goto end;
311 }
312 }
313
314 ret = ctf_visitor_semantic_check(0, &mdec->scanner->ast->root,
315 &mdec->log_cfg);
316 if (ret) {
317 BT_COMP_LOGE("Validation of the metadata semantics failed: "
318 "mdec-addr=%p", mdec);
319 status = CTF_METADATA_DECODER_STATUS_ERROR;
320 goto end;
321 }
322
323 if (mdec->config.create_trace_class) {
324 ret = ctf_visitor_generate_ir_visit_node(mdec->visitor,
325 &mdec->scanner->ast->root);
326 switch (ret) {
327 case 0:
328 /* Success */
329 break;
330 case -EINCOMPLETE:
331 BT_COMP_LOGD("While visiting metadata AST: incomplete data: "
332 "mdec-addr=%p", mdec);
333 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
334 goto end;
335 default:
336 BT_COMP_LOGE("Failed to visit AST node to create CTF IR objects: "
337 "mdec-addr=%p, ret=%d", mdec, ret);
338 status = CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR;
339 goto end;
340 }
341 }
342
343 end:
344 #if YYDEBUG
345 yydebug = 0;
346 #endif
347
348 if (fp && close_fp) {
349 if (fclose(fp)) {
350 BT_COMP_LOGE("Cannot close metadata file stream: "
351 "mdec-addr=%p", mdec);
352 }
353 }
354
355 free(buf);
356
357 return status;
358 }
359
360 BT_HIDDEN
361 bt_trace_class *ctf_metadata_decoder_get_ir_trace_class(
362 struct ctf_metadata_decoder *mdec)
363 {
364 BT_ASSERT_DBG(mdec);
365 BT_ASSERT_DBG(mdec->config.create_trace_class);
366 return ctf_visitor_generate_ir_get_ir_trace_class(mdec->visitor);
367 }
368
369 BT_HIDDEN
370 struct ctf_trace_class *ctf_metadata_decoder_borrow_ctf_trace_class(
371 struct ctf_metadata_decoder *mdec)
372 {
373 BT_ASSERT_DBG(mdec);
374 BT_ASSERT_DBG(mdec->config.create_trace_class);
375 return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec->visitor);
376 }
377
378 BT_HIDDEN
379 const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder *mdec)
380 {
381 BT_ASSERT_DBG(mdec);
382 BT_ASSERT_DBG(mdec->config.keep_plain_text);
383 return mdec->text->str;
384 }
385
386 BT_HIDDEN
387 int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder *mdec)
388 {
389 BT_ASSERT_DBG(mdec);
390 return mdec->bo;
391 }
392
393 BT_HIDDEN
394 int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder *mdec,
395 bt_uuid_t uuid)
396 {
397 int ret = 0;
398
399 BT_ASSERT_DBG(mdec);
400
401 if (!mdec->is_uuid_set) {
402 ret = -1;
403 goto end;
404 }
405
406 bt_uuid_copy(uuid, mdec->uuid);
407
408 end:
409 return ret;
410 }
411
412 static
413 enum ctf_metadata_decoder_status find_uuid_in_trace_decl(
414 struct ctf_metadata_decoder *mdec, struct ctf_node *trace_node,
415 bt_uuid_t uuid)
416 {
417 enum ctf_metadata_decoder_status status =
418 CTF_METADATA_DECODER_STATUS_OK;
419 struct ctf_node *entry_node;
420 struct bt_list_head *decl_list = &trace_node->u.trace.declaration_list;
421 char *left = NULL;
422
423 bt_list_for_each_entry(entry_node, decl_list, siblings) {
424 if (entry_node->type == NODE_CTF_EXPRESSION) {
425 int ret;
426
427 left = ctf_ast_concatenate_unary_strings(
428 &entry_node->u.ctf_expression.left);
429 if (!left) {
430 BT_COMP_LOGE("Cannot concatenate unary strings.");
431 status = CTF_METADATA_DECODER_STATUS_ERROR;
432 goto end;
433 }
434
435 if (strcmp(left, "uuid") == 0) {
436 ret = ctf_ast_get_unary_uuid(
437 &entry_node->u.ctf_expression.right,
438 uuid, mdec->config.log_level,
439 mdec->config.self_comp);
440 if (ret) {
441 BT_COMP_LOGE("Invalid trace's `uuid` attribute.");
442 status = CTF_METADATA_DECODER_STATUS_ERROR;
443 goto end;
444 }
445
446 goto end;
447 }
448
449 g_free(left);
450 left = NULL;
451 }
452 }
453
454 status = CTF_METADATA_DECODER_STATUS_NONE;
455
456 end:
457 g_free(left);
458 return status;
459 }
460
461 BT_HIDDEN
462 enum ctf_metadata_decoder_status ctf_metadata_decoder_get_trace_class_uuid(
463 struct ctf_metadata_decoder *mdec, bt_uuid_t uuid)
464 {
465 enum ctf_metadata_decoder_status status =
466 CTF_METADATA_DECODER_STATUS_INCOMPLETE;
467 struct ctf_node *root_node = &mdec->scanner->ast->root;
468 struct ctf_node *trace_node;
469
470 if (!root_node) {
471 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
472 goto end;
473 }
474
475 trace_node =
476 bt_list_entry(root_node->u.root.trace.next,
477 struct ctf_node, siblings);
478 if (!trace_node) {
479 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
480 goto end;
481 }
482
483 status = find_uuid_in_trace_decl(mdec, trace_node, uuid);
484
485 end:
486 return status;
487 }
This page took 0.037788 seconds and 3 git commands to generate.