2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
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"
17 #include "common/assert.h"
18 #include "common/uuid.h"
19 #include "compat/memstream.h"
20 #include <babeltrace2/babeltrace.h>
28 #include "parser-wrap.h"
30 #define TSDL_MAGIC 0x75d11d57
32 struct ctf_metadata_decoder
{
33 struct ctf_scanner
*scanner
;
35 struct ctf_visitor_generate_ir
*visitor
;
39 struct ctf_metadata_decoder_config config
;
40 struct meta_log_config log_cfg
;
43 struct packet_header
{
47 uint32_t content_size
;
49 uint8_t compression_scheme
;
50 uint8_t encryption_scheme
;
51 uint8_t checksum_scheme
;
54 } __attribute__((__packed__
));
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
);
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
)
71 *is_packetized
= false;
72 len
= fread(&magic
, sizeof(magic
), 1, fp
);
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.");
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
;
98 struct ctf_metadata_decoder
*ctf_metadata_decoder_create(
99 const struct ctf_metadata_decoder_config
*config
)
101 struct ctf_metadata_decoder
*mdec
=
102 g_new0(struct ctf_metadata_decoder
, 1);
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
);
112 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, config
->log_level
,
114 "Failed to allocate one CTF metadata decoder.");
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
);
127 mdec
->text
= g_string_new(NULL
);
129 BT_COMP_LOGE("Failed to allocate one GString: "
130 "mdec-addr=%p", mdec
);
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
);
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
,
151 ctf_metadata_decoder_destroy(mdec
);
159 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder
*mdec
)
166 ctf_scanner_free(mdec
->scanner
);
170 g_string_free(mdec
->text
, TRUE
);
173 BT_COMP_LOGD("Destroying CTF metadata decoder: addr=%p", mdec
);
174 ctf_visitor_generate_ir_destroy(mdec
->visitor
);
179 enum ctf_metadata_decoder_status
ctf_metadata_decoder_append_content(
180 struct ctf_metadata_decoder
*mdec
, FILE *fp
)
182 enum ctf_metadata_decoder_status status
=
183 CTF_METADATA_DECODER_STATUS_OK
;
186 bool close_fp
= false;
191 ret
= ctf_metadata_decoder_is_packetized(fp
, &is_packetized
, &mdec
->bo
,
192 mdec
->config
.log_level
, mdec
->config
.self_comp
);
194 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
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
);
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
;
211 if (strlen(buf
) == 0) {
212 /* An empty metadata packet is OK. */
216 /* Convert the real file pointer to a memory file pointer */
217 fp
= bt_fmemopen(buf
, strlen(buf
), "rb");
220 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
221 "mdec-addr=%p", strerror(errno
), mdec
);
222 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
226 unsigned int major
, minor
;
228 const long init_pos
= ftell(fp
);
230 BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec
);
233 BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
234 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
238 /* Check text-only metadata header and version */
239 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
241 BT_COMP_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
242 "mdec-addr=%p", mdec
);
245 BT_COMP_LOGI("Found metadata stream version in signature: version=%u.%u", major
, minor
);
247 if (!ctf_metadata_decoder_is_packet_version_valid(major
,
249 BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
250 "version=%u.%u, mdec-addr=%p", major
, minor
,
252 status
= CTF_METADATA_DECODER_STATUS_INVAL_VERSION
;
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
;
265 if (BT_LOG_ON_TRACE
) {
270 /* Save the file's position: we'll seek back to append the plain text */
273 if (mdec
->config
.keep_plain_text
) {
274 start_pos
= ftell(fp
);
277 /* Append the metadata text content */
278 ret
= ctf_scanner_append_ast(mdec
->scanner
, fp
);
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
;
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
);
291 BT_COMP_LOGE("Failed to seek file: ret=%d, mdec-addr=%p",
293 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
297 ret
= bt_common_append_file_content_to_g_string(mdec
->text
, fp
);
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
;
306 ret
= ctf_visitor_semantic_check(0, &mdec
->scanner
->ast
->root
,
309 BT_COMP_LOGE("Validation of the metadata semantics failed: "
310 "mdec-addr=%p", mdec
);
311 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
315 if (mdec
->config
.create_trace_class
) {
316 ret
= ctf_visitor_generate_ir_visit_node(mdec
->visitor
,
317 &mdec
->scanner
->ast
->root
);
323 BT_COMP_LOGD("While visiting metadata AST: incomplete data: "
324 "mdec-addr=%p", mdec
);
325 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
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
;
340 if (fp
&& close_fp
) {
342 BT_COMP_LOGE("Cannot close metadata file stream: "
343 "mdec-addr=%p", mdec
);
353 bt_trace_class
*ctf_metadata_decoder_get_ir_trace_class(
354 struct ctf_metadata_decoder
*mdec
)
357 BT_ASSERT_DBG(mdec
->config
.create_trace_class
);
358 return ctf_visitor_generate_ir_get_ir_trace_class(mdec
->visitor
);
362 struct ctf_trace_class
*ctf_metadata_decoder_borrow_ctf_trace_class(
363 struct ctf_metadata_decoder
*mdec
)
366 BT_ASSERT_DBG(mdec
->config
.create_trace_class
);
367 return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec
->visitor
);
371 const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder
*mdec
)
374 BT_ASSERT_DBG(mdec
->config
.keep_plain_text
);
375 return mdec
->text
->str
;
379 int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder
*mdec
)
386 int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder
*mdec
,
393 if (!mdec
->is_uuid_set
) {
398 bt_uuid_copy(uuid
, mdec
->uuid
);
405 enum ctf_metadata_decoder_status
find_uuid_in_trace_decl(
406 struct ctf_metadata_decoder
*mdec
, struct ctf_node
*trace_node
,
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
;
415 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
416 if (entry_node
->type
== NODE_CTF_EXPRESSION
) {
419 left
= ctf_ast_concatenate_unary_strings(
420 &entry_node
->u
.ctf_expression
.left
);
422 BT_COMP_LOGE("Cannot concatenate unary strings.");
423 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
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
);
433 BT_COMP_LOGE("Invalid trace's `uuid` attribute.");
434 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
446 status
= CTF_METADATA_DECODER_STATUS_NONE
;
454 enum ctf_metadata_decoder_status
ctf_metadata_decoder_get_trace_class_uuid(
455 struct ctf_metadata_decoder
*mdec
, bt_uuid_t uuid
)
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
;
463 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
468 bt_list_entry(root_node
->u
.root
.trace
.next
,
469 struct ctf_node
, siblings
);
471 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
475 status
= find_uuid_in_trace_decl(mdec
, trace_node
, uuid
);