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"
41 #include "../common/metadata/ast.h"
42 #include "../common/metadata/scanner.h"
44 #define TSDL_MAGIC 0x75d11d57
46 struct packet_header
{
50 uint32_t content_size
;
52 uint8_t compression_scheme
;
53 uint8_t encryption_scheme
;
54 uint8_t checksum_scheme
;
57 } __attribute__((__packed__
));
59 static struct ctf_fs_file
*get_file(struct ctf_fs_component
*ctf_fs
,
60 const char *trace_path
)
62 struct ctf_fs_file
*file
= ctf_fs_file_create(ctf_fs
);
68 g_string_append(file
->path
, trace_path
);
69 g_string_append(file
->path
, "/" CTF_FS_METADATA_FILENAME
);
71 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
79 ctf_fs_file_destroy(file
);
88 bool is_packetized(struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
)
94 len
= fread(&magic
, sizeof(magic
), 1, file
->fp
);
99 if (magic
== TSDL_MAGIC
) {
101 ctf_fs
->metadata
.bo
= BYTE_ORDER
;
102 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
104 ctf_fs
->metadata
.bo
= BYTE_ORDER
== BIG_ENDIAN
?
105 LITTLE_ENDIAN
: BIG_ENDIAN
;
115 bool is_version_valid(unsigned int major
, unsigned int minor
)
117 return major
== 1 && minor
== 8;
121 int decode_packet(struct ctf_fs_component
*ctf_fs
, FILE *in_fp
, FILE *out_fp
)
123 struct packet_header header
;
124 size_t readlen
, writelen
, toread
;
125 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
128 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
129 if (feof(in_fp
) != 0) {
136 if (ctf_fs
->metadata
.bo
!= BYTE_ORDER
) {
137 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
138 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
139 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
140 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
143 if (header
.compression_scheme
) {
144 PERR("Metadata packet compression not supported yet\n");
148 if (header
.encryption_scheme
) {
149 PERR("Metadata packet encryption not supported yet\n");
153 if (header
.checksum
|| header
.checksum_scheme
) {
154 PERR("Metadata packet checksum verification not supported yet\n");
158 if (!is_version_valid(header
.major
, header
.minor
)) {
159 PERR("Invalid metadata version: %u.%u\n", header
.major
,
164 /* Set expected trace UUID if not set; otherwise validate it */
165 if (!ctf_fs
->metadata
.is_uuid_set
) {
166 memcpy(ctf_fs
->metadata
.uuid
, header
.uuid
, sizeof(header
.uuid
));
167 ctf_fs
->metadata
.is_uuid_set
= true;
168 } else if (bt_uuid_compare(header
.uuid
, ctf_fs
->metadata
.uuid
)) {
169 PERR("Metadata UUID mismatch between packets of the same file\n");
173 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
174 PERR("Bad metadata packet content size: %u\n",
175 header
.content_size
);
179 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
182 readlen
= fread(buf
, sizeof(uint8_t),
183 MIN(sizeof(buf
) - 1, toread
), in_fp
);
185 PERR("Cannot read metadata packet buffer (at position %ld)\n",
190 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
191 if (writelen
< readlen
|| ferror(out_fp
)) {
192 PERR("Cannot write decoded metadata text to buffer\n");
200 /* Read leftover padding */
201 toread
= (header
.packet_size
- header
.content_size
) /
203 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
205 PWARN("Missing padding at the end of the metadata file\n");
222 int packetized_file_to_buf(struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
,
229 size_t packet_index
= 0;
231 out_fp
= babeltrace_open_memstream((char **) buf
, &size
);
232 if (out_fp
== NULL
) {
233 PERR("Cannot open memory stream: %s\n", strerror(errno
));
238 if (feof(file
->fp
) != 0) {
242 tret
= decode_packet(ctf_fs
, file
->fp
, out_fp
);
244 PERR("Cannot decode packet #%zu\n", packet_index
);
251 /* Make sure the whole string ends with a null character */
252 tret
= fputc('\0', out_fp
);
254 PERR("Cannot append '\\0' to the decoded metadata buffer\n");
258 /* Close stream, which also flushes the buffer */
259 ret
= babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
261 PERR("Cannot close memory stream: %s\n", strerror(errno
));
271 babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
282 void ctf_fs_metadata_set_trace(struct ctf_fs_component
*ctf_fs
)
285 struct ctf_fs_file
*file
= get_file(ctf_fs
, ctf_fs
->trace_path
->str
);
286 struct ctf_scanner
*scanner
= NULL
;
291 PERR("Cannot create metadata file object\n");
295 if (babeltrace_debug
) {
299 if (is_packetized(ctf_fs
, file
)) {
300 PDBG("Metadata file \"%s\" is packetized\n", file
->path
->str
);
301 ret
= packetized_file_to_buf(ctf_fs
, file
, &buf
);
308 ctf_fs
->metadata
.text
= (char *) cbuf
;
310 /* Convert the real file pointer to a memory file pointer */
311 ret
= fclose(file
->fp
);
314 PERR("Cannot close file \"%s\": %s\n", file
->path
->str
,
319 file
->fp
= babeltrace_fmemopen(cbuf
, strlen(cbuf
), "rb");
321 PERR("Cannot memory-open metadata buffer: %s\n",
326 unsigned int major
, minor
;
329 PDBG("Metadata file \"%s\" is plain text\n", file
->path
->str
);
331 /* Check text-only metadata header and version */
332 nr_items
= fscanf(file
->fp
, "/* CTF %10u.%10u", &major
, &minor
);
334 PWARN("Ill-shapen or missing \"/* CTF major.minor\" header in plain text metadata file\n");
336 PDBG("Metadata version: %u.%u\n", major
, minor
);
338 if (!is_version_valid(major
, minor
)) {
339 PERR("Invalid metadata version: %u.%u\n", major
, minor
);
346 /* Allocate a scanner and append the metadata text content */
347 scanner
= ctf_scanner_alloc();
349 PERR("Cannot allocate a metadata lexical scanner\n");
353 ret
= ctf_scanner_append_ast(scanner
, file
->fp
);
355 PERR("Cannot create the metadata AST\n");
359 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
361 PERR("Metadata semantic validation failed\n");
365 ret
= ctf_visitor_generate_ir(ctf_fs
->error_fp
, &scanner
->ast
->root
,
366 &ctf_fs
->metadata
.trace
);
368 PERR("Cannot create trace object from metadata AST\n");
375 if (ctf_fs
->metadata
.text
) {
376 free(ctf_fs
->metadata
.text
);
377 ctf_fs
->metadata
.text
= NULL
;
382 ctf_fs_file_destroy(file
);
386 ctf_scanner_free(scanner
);
390 int ctf_fs_metadata_init(struct ctf_fs_metadata
*metadata
)
392 /* Nothing to initialize for the moment */
396 void ctf_fs_metadata_fini(struct ctf_fs_metadata
*metadata
)
398 if (metadata
->text
) {
399 free(metadata
->text
);
402 if (metadata
->trace
) {
403 BT_PUT(metadata
->trace
);
This page took 0.059164 seconds and 5 git commands to generate.