2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 * Some functions are based on older functions written by Mathieu Desnoyers.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include <babeltrace/compat/uuid.h>
32 #include <babeltrace/compat/memstream.h>
34 #define PRINT_ERR_STREAM ctf_fs->error_fp
35 #define PRINT_PREFIX "ctf-fs-metadata"
39 #include "ctf-fs-file.h"
40 #include "metadata-parsing/ctf-ast.h"
41 #include "metadata-parsing/ctf-scanner.h"
43 #define TSDL_MAGIC 0x75d11d57
45 struct packet_header
{
49 uint32_t content_size
;
51 uint8_t compression_scheme
;
52 uint8_t encryption_scheme
;
53 uint8_t checksum_scheme
;
56 } __attribute__((__packed__
));
58 static struct ctf_fs_file
*get_file(struct ctf_fs
*ctf_fs
,
59 const char *trace_path
)
61 struct ctf_fs_file
*file
= ctf_fs_file_create(ctf_fs
);
67 g_string_append(file
->path
, trace_path
);
68 g_string_append(file
->path
, "/" CTF_FS_METADATA_FILENAME
);
70 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
78 ctf_fs_file_destroy(file
);
87 bool is_packetized(struct ctf_fs
*ctf_fs
, struct ctf_fs_file
*file
)
93 len
= fread(&magic
, sizeof(magic
), 1, file
->fp
);
98 if (magic
== TSDL_MAGIC
) {
100 ctf_fs
->metadata
.bo
= BYTE_ORDER
;
101 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
103 ctf_fs
->metadata
.bo
= BYTE_ORDER
== BIG_ENDIAN
?
104 LITTLE_ENDIAN
: BIG_ENDIAN
;
114 bool is_version_valid(unsigned int major
, unsigned int minor
)
116 return major
== 1 && minor
== 8;
120 int decode_packet(struct ctf_fs
*ctf_fs
, FILE *in_fp
, FILE *out_fp
)
122 struct packet_header header
;
123 size_t readlen
, writelen
, toread
;
124 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
127 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
128 if (feof(in_fp
) != 0) {
135 if (ctf_fs
->metadata
.bo
!= BYTE_ORDER
) {
136 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
137 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
138 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
139 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
142 if (header
.compression_scheme
) {
143 PERR("Metadata packet compression not supported yet\n");
147 if (header
.encryption_scheme
) {
148 PERR("Metadata packet encryption not supported yet\n");
152 if (header
.checksum
|| header
.checksum_scheme
) {
153 PERR("Metadata packet checksum verification not supported yet\n");
157 if (!is_version_valid(header
.major
, header
.minor
)) {
158 PERR("Invalid metadata version: %u.%u\n", header
.major
,
163 /* Set expected trace UUID if not set; otherwise validate it */
164 if (!ctf_fs
->metadata
.is_uuid_set
) {
165 memcpy(ctf_fs
->metadata
.uuid
, header
.uuid
, sizeof(header
.uuid
));
166 ctf_fs
->metadata
.is_uuid_set
= true;
167 } else if (bt_uuid_compare(header
.uuid
, ctf_fs
->metadata
.uuid
)) {
168 PERR("Metadata UUID mismatch between packets of the same file\n");
172 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
173 PERR("Bad metadata packet content size: %u\n",
174 header
.content_size
);
178 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
181 readlen
= fread(buf
, sizeof(uint8_t),
182 MIN(sizeof(buf
) - 1, toread
), in_fp
);
184 PERR("Cannot read metadata packet buffer (at position %ld)\n",
189 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
190 if (writelen
< readlen
|| ferror(out_fp
)) {
191 PERR("Cannot write decoded metadata text to buffer\n");
199 /* Read leftover padding */
200 toread
= (header
.packet_size
- header
.content_size
) /
202 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
204 PWARN("Missing padding at the end of the metadata file\n");
221 int packetized_file_to_buf(struct ctf_fs
*ctf_fs
, struct ctf_fs_file
*file
,
228 size_t packet_index
= 0;
230 out_fp
= babeltrace_open_memstream((char **) buf
, &size
);
231 if (out_fp
== NULL
) {
232 PERR("Cannot open memory stream: %s\n", strerror(errno
));
237 if (feof(file
->fp
) != 0) {
241 tret
= decode_packet(ctf_fs
, file
->fp
, out_fp
);
243 PERR("Cannot decode packet #%zu\n", packet_index
);
250 /* Make sure the whole string ends with a null character */
251 tret
= fputc('\0', out_fp
);
253 PERR("Cannot append '\\0' to the decoded metadata buffer\n");
257 /* Close stream, which also flushes the buffer */
258 ret
= babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
260 PERR("Cannot close memory stream: %s\n", strerror(errno
));
270 babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
281 void ctf_fs_metadata_set_trace(struct ctf_fs
*ctf_fs
)
284 struct ctf_fs_file
*file
= get_file(ctf_fs
, ctf_fs
->trace_path
->str
);
285 struct ctf_scanner
*scanner
= NULL
;
290 PERR("Cannot create metadata file object\n");
294 if (babeltrace_debug
) {
298 if (is_packetized(ctf_fs
, file
)) {
299 PDBG("Metadata file \"%s\" is packetized\n", file
->path
->str
);
300 ret
= packetized_file_to_buf(ctf_fs
, file
, &buf
);
307 ctf_fs
->metadata
.text
= (char *) cbuf
;
309 /* Convert the real file pointer to a memory file pointer */
310 ret
= fclose(file
->fp
);
313 PERR("Cannot close file \"%s\": %s\n", file
->path
->str
,
318 file
->fp
= babeltrace_fmemopen(cbuf
, strlen(cbuf
), "rb");
320 PERR("Cannot memory-open metadata buffer: %s\n",
325 unsigned int major
, minor
;
328 PDBG("Metadata file \"%s\" is plain text\n", file
->path
->str
);
330 /* Check text-only metadata header and version */
331 nr_items
= fscanf(file
->fp
, "/* CTF %10u.%10u", &major
, &minor
);
333 PWARN("Ill-shapen or missing \"/* CTF major.minor\" header in plain text metadata file\n");
335 PDBG("Metadata version: %u.%u\n", major
, minor
);
337 if (!is_version_valid(major
, minor
)) {
338 PERR("Invalid metadata version: %u.%u\n", major
, minor
);
345 /* Allocate a scanner and append the metadata text content */
346 scanner
= ctf_scanner_alloc();
348 PERR("Cannot allocate a metadata lexical scanner\n");
352 ret
= ctf_scanner_append_ast(scanner
, file
->fp
);
354 PERR("Cannot create the metadata AST\n");
358 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
360 PERR("Metadata semantic validation failed\n");
364 ret
= ctf_visitor_generate_ir(ctf_fs
->error_fp
, &scanner
->ast
->root
,
365 &ctf_fs
->metadata
.trace
);
367 PERR("Cannot create trace object from metadata AST\n");
374 if (ctf_fs
->metadata
.text
) {
375 free(ctf_fs
->metadata
.text
);
376 ctf_fs
->metadata
.text
= NULL
;
381 ctf_fs_file_destroy(file
);
385 ctf_scanner_free(scanner
);
389 int ctf_fs_metadata_init(struct ctf_fs_metadata
*metadata
)
391 /* Nothing to initialize for the moment */
395 void ctf_fs_metadata_deinit(struct ctf_fs_metadata
*metadata
)
397 if (metadata
->text
) {
398 free(metadata
->text
);
401 if (metadata
->trace
) {
402 BT_PUT(metadata
->trace
);
This page took 0.040124 seconds and 4 git commands to generate.