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