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