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