2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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"
25 #include "common/assert.h"
26 #include "common/uuid.h"
27 #include "compat/memstream.h"
28 #include <babeltrace2/babeltrace.h>
36 #include "parser-wrap.h"
38 #define TSDL_MAGIC 0x75d11d57
40 struct ctf_metadata_decoder
{
41 struct ctf_scanner
*scanner
;
43 struct ctf_visitor_generate_ir
*visitor
;
47 struct ctf_metadata_decoder_config config
;
48 struct meta_log_config log_cfg
;
51 struct packet_header
{
55 uint32_t content_size
;
57 uint8_t compression_scheme
;
58 uint8_t encryption_scheme
;
59 uint8_t checksum_scheme
;
62 } __attribute__((__packed__
));
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
);
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
)
79 *is_packetized
= false;
80 len
= fread(&magic
, sizeof(magic
), 1, fp
);
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.");
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
;
106 struct ctf_metadata_decoder
*ctf_metadata_decoder_create(
107 const struct ctf_metadata_decoder_config
*config
)
109 struct ctf_metadata_decoder
*mdec
=
110 g_new0(struct ctf_metadata_decoder
, 1);
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
);
120 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, config
->log_level
,
122 "Failed to allocate one CTF metadata decoder.");
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
);
135 mdec
->text
= g_string_new(NULL
);
137 BT_COMP_LOGE("Failed to allocate one GString: "
138 "mdec-addr=%p", mdec
);
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
);
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
,
159 ctf_metadata_decoder_destroy(mdec
);
167 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder
*mdec
)
174 ctf_scanner_free(mdec
->scanner
);
178 g_string_free(mdec
->text
, TRUE
);
181 BT_COMP_LOGD("Destroying CTF metadata decoder: addr=%p", mdec
);
182 ctf_visitor_generate_ir_destroy(mdec
->visitor
);
187 enum ctf_metadata_decoder_status
ctf_metadata_decoder_append_content(
188 struct ctf_metadata_decoder
*mdec
, FILE *fp
)
190 enum ctf_metadata_decoder_status status
=
191 CTF_METADATA_DECODER_STATUS_OK
;
194 bool close_fp
= false;
199 ret
= ctf_metadata_decoder_is_packetized(fp
, &is_packetized
, &mdec
->bo
,
200 mdec
->config
.log_level
, mdec
->config
.self_comp
);
202 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
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
);
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
;
219 if (strlen(buf
) == 0) {
220 /* An empty metadata packet is OK. */
224 /* Convert the real file pointer to a memory file pointer */
225 fp
= bt_fmemopen(buf
, strlen(buf
), "rb");
228 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
229 "mdec-addr=%p", strerror(errno
), mdec
);
230 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
234 unsigned int major
, minor
;
236 const long init_pos
= ftell(fp
);
238 BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec
);
241 BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
242 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
246 /* Check text-only metadata header and version */
247 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
249 BT_COMP_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
250 "mdec-addr=%p", mdec
);
253 BT_COMP_LOGI("Found metadata stream version in signature: version=%u.%u", major
, minor
);
255 if (!ctf_metadata_decoder_is_packet_version_valid(major
,
257 BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
258 "version=%u.%u, mdec-addr=%p", major
, minor
,
260 status
= CTF_METADATA_DECODER_STATUS_INVAL_VERSION
;
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
;
273 if (BT_LOG_ON_TRACE
) {
278 /* Save the file's position: we'll seek back to append the plain text */
281 if (mdec
->config
.keep_plain_text
) {
282 start_pos
= ftell(fp
);
285 /* Append the metadata text content */
286 ret
= ctf_scanner_append_ast(mdec
->scanner
, fp
);
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
;
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
);
299 BT_COMP_LOGE("Failed to seek file: ret=%d, mdec-addr=%p",
301 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
305 ret
= bt_common_append_file_content_to_g_string(mdec
->text
, fp
);
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
;
314 ret
= ctf_visitor_semantic_check(0, &mdec
->scanner
->ast
->root
,
317 BT_COMP_LOGE("Validation of the metadata semantics failed: "
318 "mdec-addr=%p", mdec
);
319 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
323 if (mdec
->config
.create_trace_class
) {
324 ret
= ctf_visitor_generate_ir_visit_node(mdec
->visitor
,
325 &mdec
->scanner
->ast
->root
);
331 BT_COMP_LOGD("While visiting metadata AST: incomplete data: "
332 "mdec-addr=%p", mdec
);
333 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
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
;
348 if (fp
&& close_fp
) {
350 BT_COMP_LOGE("Cannot close metadata file stream: "
351 "mdec-addr=%p", mdec
);
361 bt_trace_class
*ctf_metadata_decoder_get_ir_trace_class(
362 struct ctf_metadata_decoder
*mdec
)
365 BT_ASSERT_DBG(mdec
->config
.create_trace_class
);
366 return ctf_visitor_generate_ir_get_ir_trace_class(mdec
->visitor
);
370 struct ctf_trace_class
*ctf_metadata_decoder_borrow_ctf_trace_class(
371 struct ctf_metadata_decoder
*mdec
)
374 BT_ASSERT_DBG(mdec
->config
.create_trace_class
);
375 return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec
->visitor
);
379 const char *ctf_metadata_decoder_get_text(struct ctf_metadata_decoder
*mdec
)
382 BT_ASSERT_DBG(mdec
->config
.keep_plain_text
);
383 return mdec
->text
->str
;
387 int ctf_metadata_decoder_get_byte_order(struct ctf_metadata_decoder
*mdec
)
394 int ctf_metadata_decoder_get_uuid(struct ctf_metadata_decoder
*mdec
,
401 if (!mdec
->is_uuid_set
) {
406 bt_uuid_copy(uuid
, mdec
->uuid
);
413 enum ctf_metadata_decoder_status
find_uuid_in_trace_decl(
414 struct ctf_metadata_decoder
*mdec
, struct ctf_node
*trace_node
,
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
;
423 bt_list_for_each_entry(entry_node
, decl_list
, siblings
) {
424 if (entry_node
->type
== NODE_CTF_EXPRESSION
) {
427 left
= ctf_ast_concatenate_unary_strings(
428 &entry_node
->u
.ctf_expression
.left
);
430 BT_COMP_LOGE("Cannot concatenate unary strings.");
431 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
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
);
441 BT_COMP_LOGE("Invalid trace's `uuid` attribute.");
442 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
454 status
= CTF_METADATA_DECODER_STATUS_NONE
;
462 enum ctf_metadata_decoder_status
ctf_metadata_decoder_get_trace_class_uuid(
463 struct ctf_metadata_decoder
*mdec
, bt_uuid_t uuid
)
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
;
471 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
476 bt_list_entry(root_node
->u
.root
.trace
.next
,
477 struct ctf_node
, siblings
);
479 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
483 status
= find_uuid_in_trace_decl(mdec
, trace_node
, uuid
);