Replace libuuid with internal implementation
[babeltrace.git] / src / plugins / ctf / common / metadata / decoder.c
1 /*
2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 */
14
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"
19
20 #include <stdio.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <inttypes.h>
25 #include "common/assert.h"
26 #include "common/uuid.h"
27 #include "compat/memstream.h"
28 #include <babeltrace2/babeltrace.h>
29 #include <glib.h>
30 #include <string.h>
31
32 #include "ast.h"
33 #include "decoder.h"
34 #include "scanner.h"
35 #include "logging.h"
36
37 #define TSDL_MAGIC 0x75d11d57
38
39 extern
40 int yydebug;
41
42 struct ctf_metadata_decoder {
43 struct ctf_visitor_generate_ir *visitor;
44 bt_uuid_t uuid;
45 bool is_uuid_set;
46 int bo;
47 struct ctf_metadata_decoder_config config;
48 };
49
50 struct packet_header {
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 BT_HIDDEN
64 bool ctf_metadata_decoder_is_packetized(FILE *fp, int *byte_order,
65 bt_logging_level log_level, bt_self_component *self_comp)
66 {
67 uint32_t magic;
68 size_t len;
69 int ret = 0;
70
71 len = fread(&magic, sizeof(magic), 1, fp);
72 if (len != 1) {
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.");
75 goto end;
76 }
77
78 if (byte_order) {
79 if (magic == TSDL_MAGIC) {
80 ret = 1;
81 *byte_order = BYTE_ORDER;
82 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
83 ret = 1;
84 *byte_order = BYTE_ORDER == BIG_ENDIAN ?
85 LITTLE_ENDIAN : BIG_ENDIAN;
86 }
87 }
88
89 end:
90 rewind(fp);
91
92 return ret;
93 }
94
95 BT_HIDDEN
96 struct ctf_metadata_decoder *ctf_metadata_decoder_create(
97 const struct ctf_metadata_decoder_config *config)
98 {
99 struct ctf_metadata_decoder *mdec =
100 g_new0(struct ctf_metadata_decoder, 1);
101
102 BT_ASSERT(config);
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);
108
109 if (!mdec) {
110 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, config->log_level,
111 config->self_comp,
112 "Failed to allocate one CTF metadata decoder.");
113 goto end;
114 }
115
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);
122 mdec = NULL;
123 goto end;
124 }
125
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,
130 mdec);
131
132 end:
133 return mdec;
134 }
135
136 BT_HIDDEN
137 void ctf_metadata_decoder_destroy(struct ctf_metadata_decoder *mdec)
138 {
139 if (!mdec) {
140 return;
141 }
142
143 BT_COMP_LOGD("Destroying CTF metadata decoder: addr=%p", mdec);
144 ctf_visitor_generate_ir_destroy(mdec->visitor);
145 g_free(mdec);
146 }
147
148 BT_HIDDEN
149 enum ctf_metadata_decoder_status ctf_metadata_decoder_decode(
150 struct ctf_metadata_decoder *mdec, FILE *fp)
151 {
152 enum ctf_metadata_decoder_status status =
153 CTF_METADATA_DECODER_STATUS_OK;
154 int ret;
155 struct ctf_scanner *scanner = NULL;
156 char *buf = NULL;
157 bool close_fp = false;
158 struct meta_log_config log_cfg;
159
160 BT_ASSERT(mdec);
161 log_cfg.log_level = mdec->config.log_level;
162 log_cfg.self_comp = mdec->config.self_comp;
163
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);
171 if (ret) {
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;
175 goto end;
176 }
177
178 if (strlen(buf) == 0) {
179 /* An empty metadata packet is OK. */
180 goto end;
181 }
182
183 /* Convert the real file pointer to a memory file pointer */
184 fp = bt_fmemopen(buf, strlen(buf), "rb");
185 close_fp = true;
186 if (!fp) {
187 BT_COMP_LOGE("Cannot memory-open metadata buffer: %s: "
188 "mdec-addr=%p", strerror(errno), mdec);
189 status = CTF_METADATA_DECODER_STATUS_ERROR;
190 goto end;
191 }
192 } else {
193 unsigned int major, minor;
194 ssize_t nr_items;
195 const long init_pos = ftell(fp);
196
197 BT_COMP_LOGI("Metadata stream is plain text: mdec-addr=%p", mdec);
198
199 if (init_pos < 0) {
200 BT_COMP_LOGE_ERRNO("Failed to get current file position", ".");
201 status = CTF_METADATA_DECODER_STATUS_ERROR;
202 goto end;
203 }
204
205 /* Check text-only metadata header and version */
206 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
207 if (nr_items < 2) {
208 BT_COMP_LOGW("Missing \"/* CTF major.minor\" signature in plain text metadata file stream: "
209 "mdec-addr=%p", mdec);
210 }
211
212 BT_COMP_LOGI("Found metadata stream version in signature: version=%u.%u", major, minor);
213
214 if (!ctf_metadata_decoder_is_packet_version_valid(major,
215 minor)) {
216 BT_COMP_LOGE("Invalid metadata version found in plain text signature: "
217 "version=%u.%u, mdec-addr=%p", major, minor,
218 mdec);
219 status = CTF_METADATA_DECODER_STATUS_INVAL_VERSION;
220 goto end;
221 }
222
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;
227 goto end;
228 }
229 }
230
231 if (BT_LOG_ON_TRACE) {
232 yydebug = 1;
233 }
234
235 /* Allocate a scanner and append the metadata text content */
236 scanner = ctf_scanner_alloc();
237 if (!scanner) {
238 BT_COMP_LOGE("Cannot allocate a metadata lexical scanner: "
239 "mdec-addr=%p", mdec);
240 status = CTF_METADATA_DECODER_STATUS_ERROR;
241 goto end;
242 }
243
244 BT_ASSERT(fp);
245 ret = ctf_scanner_append_ast(scanner, fp);
246 if (ret) {
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;
250 goto end;
251 }
252
253 ret = ctf_visitor_semantic_check(0, &scanner->ast->root, &log_cfg);
254 if (ret) {
255 BT_COMP_LOGE("Validation of the metadata semantics failed: "
256 "mdec-addr=%p", mdec);
257 status = CTF_METADATA_DECODER_STATUS_ERROR;
258 goto end;
259 }
260
261 ret = ctf_visitor_generate_ir_visit_node(mdec->visitor,
262 &scanner->ast->root);
263 switch (ret) {
264 case 0:
265 /* Success */
266 break;
267 case -EINCOMPLETE:
268 BT_COMP_LOGD("While visiting metadata AST: incomplete data: "
269 "mdec-addr=%p", mdec);
270 status = CTF_METADATA_DECODER_STATUS_INCOMPLETE;
271 goto end;
272 default:
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;
276 goto end;
277 }
278
279 end:
280 if (scanner) {
281 ctf_scanner_free(scanner);
282 }
283
284 yydebug = 0;
285
286 if (fp && close_fp) {
287 if (fclose(fp)) {
288 BT_COMP_LOGE("Cannot close metadata file stream: "
289 "mdec-addr=%p", mdec);
290 }
291 }
292
293 if (buf) {
294 free(buf);
295 }
296
297 return status;
298 }
299
300 BT_HIDDEN
301 bt_trace_class *ctf_metadata_decoder_get_ir_trace_class(
302 struct ctf_metadata_decoder *mdec)
303 {
304 return ctf_visitor_generate_ir_get_ir_trace_class(mdec->visitor);
305 }
306
307 BT_HIDDEN
308 struct ctf_trace_class *ctf_metadata_decoder_borrow_ctf_trace_class(
309 struct ctf_metadata_decoder *mdec)
310 {
311 return ctf_visitor_generate_ir_borrow_ctf_trace_class(mdec->visitor);
312 }
This page took 0.047071 seconds and 4 git commands to generate.