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 "plugins/comp-logging.h"
25 #include "common/assert.h"
26 #include "common/uuid.h"
27 #include "compat/memstream.h"
28 #include <babeltrace2/babeltrace.h>
37 #define TSDL_MAGIC 0x75d11d57
42 struct ctf_metadata_decoder
{
43 struct ctf_visitor_generate_ir
*visitor
;
47 struct ctf_metadata_decoder_config config
;
50 struct packet_header
{
54 uint32_t content_size
;
56 uint8_t compression_scheme
;
57 uint8_t encryption_scheme
;
58 uint8_t checksum_scheme
;
61 } __attribute__((__packed__
));
64 bool ctf_metadata_decoder_is_packetized(FILE *fp
, int *byte_order
,
65 bt_logging_level log_level
, bt_self_component
*self_comp
)
71 len
= fread(&magic
, sizeof(magic
), 1, fp
);
73 BT_COMP_LOG_CUR_LVL(BT_LOG_INFO
, log_level
, self_comp
,
74 "Cannot read first metadata packet header: assuming the stream is not packetized.");
79 if (magic
== TSDL_MAGIC
) {
81 *byte_order
= BYTE_ORDER
;
82 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
84 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
85 LITTLE_ENDIAN
: BIG_ENDIAN
;
96 struct ctf_metadata_decoder
*ctf_metadata_decoder_create(
97 const struct ctf_metadata_decoder_config
*config
)
99 struct ctf_metadata_decoder
*mdec
=
100 g_new0(struct ctf_metadata_decoder
, 1);
103 BT_COMP_LOG_CUR_LVL(BT_LOG_DEBUG
, config
->log_level
, config
->self_comp
,
104 "Creating CTF metadata decoder: "
105 "clock-class-offset-s=%" PRId64
", "
106 "clock-class-offset-ns=%" PRId64
,
107 config
->clock_class_offset_s
, config
->clock_class_offset_ns
);
110 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, config
->log_level
,
112 "Failed to allocate one CTF metadata decoder.");
116 mdec
->config
= *config
;
117 mdec
->visitor
= ctf_visitor_generate_ir_create(config
);
118 if (!mdec
->visitor
) {
119 BT_COMP_LOGE("Failed to create a CTF IR metadata AST visitor: "
120 "mdec-addr=%p", mdec
);
121 ctf_metadata_decoder_destroy(mdec
);
126 BT_COMP_LOGD("Creating CTF metadata decoder: "
127 "clock-class-offset-s=%" PRId64
", "
128 "clock-class-offset-ns=%" PRId64
", addr=%p",
129 config
->clock_class_offset_s
, config
->clock_class_offset_ns
,
137 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder
*mdec
)
143 BT_COMP_LOGD("Destroying CTF metadata decoder: addr=%p", mdec
);
144 ctf_visitor_generate_ir_destroy(mdec
->visitor
);
149 enum ctf_metadata_decoder_status
ctf_metadata_decoder_decode(
150 struct ctf_metadata_decoder
*mdec
, FILE *fp
)
152 enum ctf_metadata_decoder_status status
=
153 CTF_METADATA_DECODER_STATUS_OK
;
155 struct ctf_scanner
*scanner
= NULL
;
157 bool close_fp
= false;
158 struct meta_log_config log_cfg
;
161 log_cfg
.log_level
= mdec
->config
.log_level
;
162 log_cfg
.self_comp
= mdec
->config
.self_comp
;
164 if (ctf_metadata_decoder_is_packetized(fp
, &mdec
->bo
,
165 mdec
->config
.log_level
, mdec
->config
.self_comp
)) {
166 BT_COMP_LOGI("Metadata stream is packetized: mdec-addr=%p", mdec
);
167 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(fp
,
168 &buf
, mdec
->bo
, &mdec
->is_uuid_set
,
169 mdec
->uuid
, mdec
->config
.log_level
,
170 mdec
->config
.self_comp
);
172 BT_COMP_LOGE("Cannot decode packetized metadata packets to metadata text: "
173 "mdec-addr=%p, ret=%d", mdec
, ret
);
174 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
178 if (strlen(buf
) == 0) {
179 /* An empty metadata packet is OK. */
183 /* Convert the real file pointer to a memory file pointer */
184 fp
= bt_fmemopen(buf
, strlen(buf
), "rb");
187 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
188 "mdec-addr=%p", strerror(errno
), mdec
);
189 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
193 unsigned int major
, minor
;
195 const long init_pos
= ftell(fp
);
197 BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec
);
200 BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
201 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
205 /* Check text-only metadata header and version */
206 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
208 BT_COMP_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
209 "mdec-addr=%p", mdec
);
212 BT_COMP_LOGI("Found metadata stream version in signature: version=%u.%u", major
, minor
);
214 if (!ctf_metadata_decoder_is_packet_version_valid(major
,
216 BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
217 "version=%u.%u, mdec-addr=%p", major
, minor
,
219 status
= CTF_METADATA_DECODER_STATUS_INVAL_VERSION
;
223 if (fseek(fp
, init_pos
, SEEK_SET
)) {
224 BT_COMP_LOGE("Cannot seek metadata file stream to initial position: %s: "
225 "mdec-addr=%p", strerror(errno
), mdec
);
226 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
231 if (BT_LOG_ON_TRACE
) {
235 /* Allocate a scanner and append the metadata text content */
236 scanner
= ctf_scanner_alloc();
238 BT_COMP_LOGE("Cannot allocate a metadata lexical scanner: "
239 "mdec-addr=%p", mdec
);
240 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
245 ret
= ctf_scanner_append_ast(scanner
, fp
);
247 BT_COMP_LOGE("Cannot create the metadata AST out of the metadata text: "
248 "mdec-addr=%p", mdec
);
249 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
253 ret
= ctf_visitor_semantic_check(0, &scanner
->ast
->root
, &log_cfg
);
255 BT_COMP_LOGE("Validation of the metadata semantics failed: "
256 "mdec-addr=%p", mdec
);
257 status
= CTF_METADATA_DECODER_STATUS_ERROR
;
261 ret
= ctf_visitor_generate_ir_visit_node(mdec
->visitor
,
262 &scanner
->ast
->root
);
268 BT_COMP_LOGD("While visiting metadata AST: incomplete data: "
269 "mdec-addr=%p", mdec
);
270 status
= CTF_METADATA_DECODER_STATUS_INCOMPLETE
;
273 BT_COMP_LOGE("Failed to visit AST node to create CTF IR objects: "
274 "mdec-addr=%p, ret=%d", mdec
, ret
);
275 status
= CTF_METADATA_DECODER_STATUS_IR_VISITOR_ERROR
;
281 ctf_scanner_free(scanner
);
286 if (fp
&& close_fp
) {
288 BT_COMP_LOGE("Cannot close metadata file stream: "
289 "mdec-addr=%p", mdec
);
301 bt_trace_class
*ctf_metadata_decoder_get_ir_trace_class(
302 struct ctf_metadata_decoder
*mdec
)
304 return ctf_visitor_generate_ir_get_ir_trace_class(mdec
->visitor
);
308 struct ctf_trace_class
*ctf_metadata_decoder_borrow_ctf_trace_class(
309 struct ctf_metadata_decoder
*mdec
)
311 return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec
->visitor
);