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 #define NSEC_PER_SEC 1000000000LL
48 struct packet_header
{
52 uint32_t content_size
;
54 uint8_t compression_scheme
;
55 uint8_t encryption_scheme
;
56 uint8_t checksum_scheme
;
59 } __attribute__((__packed__
));
62 FILE *ctf_fs_metadata_open_file(const char *trace_path
)
64 GString
*metadata_path
= g_string_new(trace_path
);
71 g_string_append(metadata_path
, "/" CTF_FS_METADATA_FILENAME
);
72 fp
= fopen(metadata_path
->str
, "rb");
86 g_string_free(metadata_path
, TRUE
);
90 static struct ctf_fs_file
*get_file(struct ctf_fs_component
*ctf_fs
,
91 const char *trace_path
)
93 struct ctf_fs_file
*file
= ctf_fs_file_create(ctf_fs
);
99 g_string_append(file
->path
, trace_path
);
100 g_string_append(file
->path
, "/" CTF_FS_METADATA_FILENAME
);
102 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
110 ctf_fs_file_destroy(file
);
119 bool ctf_metadata_is_packetized(FILE *fp
, int *byte_order
)
125 len
= fread(&magic
, sizeof(magic
), 1, fp
);
131 if (magic
== TSDL_MAGIC
) {
133 *byte_order
= BYTE_ORDER
;
134 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
136 *byte_order
= BYTE_ORDER
== BIG_ENDIAN
?
137 LITTLE_ENDIAN
: BIG_ENDIAN
;
148 bool is_version_valid(unsigned int major
, unsigned int minor
)
150 return major
== 1 && minor
== 8;
154 int decode_packet(struct ctf_fs_component
*ctf_fs
, FILE *in_fp
, FILE *out_fp
,
157 struct packet_header header
;
158 size_t readlen
, writelen
, toread
;
159 uint8_t buf
[512 + 1]; /* + 1 for debug-mode \0 */
162 readlen
= fread(&header
, sizeof(header
), 1, in_fp
);
163 if (feof(in_fp
) != 0) {
170 if (byte_order
!= BYTE_ORDER
) {
171 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
172 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
173 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
174 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
177 if (header
.compression_scheme
) {
178 PERR("Metadata packet compression not supported yet\n");
182 if (header
.encryption_scheme
) {
183 PERR("Metadata packet encryption not supported yet\n");
187 if (header
.checksum
|| header
.checksum_scheme
) {
188 PERR("Metadata packet checksum verification not supported yet\n");
192 if (!is_version_valid(header
.major
, header
.minor
)) {
193 PERR("Invalid metadata version: %u.%u\n", header
.major
,
198 /* Set expected trace UUID if not set; otherwise validate it */
200 if (!ctf_fs
->metadata
->is_uuid_set
) {
201 memcpy(ctf_fs
->metadata
->uuid
, header
.uuid
, sizeof(header
.uuid
));
202 ctf_fs
->metadata
->is_uuid_set
= true;
203 } else if (bt_uuid_compare(header
.uuid
, ctf_fs
->metadata
->uuid
)) {
204 PERR("Metadata UUID mismatch between packets of the same file\n");
209 if ((header
.content_size
/ CHAR_BIT
) < sizeof(header
)) {
210 PERR("Bad metadata packet content size: %u\n",
211 header
.content_size
);
215 toread
= header
.content_size
/ CHAR_BIT
- sizeof(header
);
218 readlen
= fread(buf
, sizeof(uint8_t),
219 MIN(sizeof(buf
) - 1, toread
), in_fp
);
221 PERR("Cannot read metadata packet buffer (at position %ld)\n",
226 writelen
= fwrite(buf
, sizeof(uint8_t), readlen
, out_fp
);
227 if (writelen
< readlen
|| ferror(out_fp
)) {
228 PERR("Cannot write decoded metadata text to buffer\n");
236 /* Read leftover padding */
237 toread
= (header
.packet_size
- header
.content_size
) /
239 fseek_ret
= fseek(in_fp
, toread
, SEEK_CUR
);
241 PWARN("Missing padding at the end of the metadata file\n");
258 int ctf_metadata_packetized_file_to_buf(struct ctf_fs_component
*ctf_fs
,
259 FILE *fp
, uint8_t **buf
, int byte_order
)
265 size_t packet_index
= 0;
267 out_fp
= babeltrace_open_memstream((char **) buf
, &size
);
268 if (out_fp
== NULL
) {
269 PERR("Cannot open memory stream: %s\n", strerror(errno
));
278 tret
= decode_packet(ctf_fs
, fp
, out_fp
, byte_order
);
280 PERR("Cannot decode packet #%zu\n", packet_index
);
287 /* Make sure the whole string ends with a null character */
288 tret
= fputc('\0', out_fp
);
290 PERR("Cannot append '\\0' to the decoded metadata buffer\n");
294 /* Close stream, which also flushes the buffer */
295 ret
= babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
297 PERR("Cannot close memory stream: %s\n", strerror(errno
));
307 babeltrace_close_memstream((char **) buf
, &size
, out_fp
);
318 int ctf_fs_metadata_set_trace(struct ctf_fs_component
*ctf_fs
)
321 struct ctf_fs_file
*file
= get_file(ctf_fs
, ctf_fs
->trace_path
->str
);
322 struct ctf_scanner
*scanner
= NULL
;
327 PERR("Cannot create metadata file object\n");
331 if (babeltrace_debug
) {
335 if (ctf_metadata_is_packetized(file
->fp
, &ctf_fs
->metadata
->bo
)) {
336 PDBG("Metadata file \"%s\" is packetized\n", file
->path
->str
);
337 ret
= ctf_metadata_packetized_file_to_buf(ctf_fs
, file
->fp
,
338 &buf
, ctf_fs
->metadata
->bo
);
345 ctf_fs
->metadata
->text
= (char *) cbuf
;
347 /* Convert the real file pointer to a memory file pointer */
348 ret
= fclose(file
->fp
);
351 PERR("Cannot close file \"%s\": %s\n", file
->path
->str
,
356 file
->fp
= babeltrace_fmemopen(cbuf
, strlen(cbuf
), "rb");
358 PERR("Cannot memory-open metadata buffer: %s\n",
363 unsigned int major
, minor
;
366 PDBG("Metadata file \"%s\" is plain text\n", file
->path
->str
);
368 /* Check text-only metadata header and version */
369 nr_items
= fscanf(file
->fp
, "/* CTF %10u.%10u", &major
, &minor
);
371 PWARN("Ill-shapen or missing \"/* CTF major.minor\" header in plain text metadata file\n");
373 PDBG("Metadata version: %u.%u\n", major
, minor
);
375 if (!is_version_valid(major
, minor
)) {
376 PERR("Invalid metadata version: %u.%u\n", major
, minor
);
383 /* Allocate a scanner and append the metadata text content */
384 scanner
= ctf_scanner_alloc();
386 PERR("Cannot allocate a metadata lexical scanner\n");
390 ret
= ctf_scanner_append_ast(scanner
, file
->fp
);
392 PERR("Cannot create the metadata AST\n");
396 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
398 PERR("Metadata semantic validation failed\n");
402 ret
= ctf_visitor_generate_ir(ctf_fs
->error_fp
, &scanner
->ast
->root
,
403 &ctf_fs
->metadata
->trace
,
404 ctf_fs
->options
.clock_offset
* NSEC_PER_SEC
+
405 ctf_fs
->options
.clock_offset_ns
);
407 PERR("Cannot create trace object from metadata AST\n");
414 if (ctf_fs
->metadata
->text
) {
415 free(ctf_fs
->metadata
->text
);
416 ctf_fs
->metadata
->text
= NULL
;
423 ctf_fs_file_destroy(file
);
427 ctf_scanner_free(scanner
);
433 int ctf_fs_metadata_init(struct ctf_fs_metadata
*metadata
)
435 /* Nothing to initialize for the moment. */
439 void ctf_fs_metadata_fini(struct ctf_fs_metadata
*metadata
)
441 if (metadata
->text
) {
442 free(metadata
->text
);
445 if (metadata
->trace
) {
446 BT_PUT(metadata
->trace
);
This page took 0.050798 seconds and 4 git commands to generate.