Update message
[babeltrace.git] / formats / ctf / ctf.c
index 6e45ea3612f603664cbee66fb5d0d90e512f215a..fee9a5578d809de68f233c2b855980a6f1fb0173 100644 (file)
@@ -3,7 +3,9 @@
  *
  * Format registration.
  *
- * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -156,14 +158,17 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
                }
 
                /* lookup timestamp */
+               stream->has_timestamp = 0;
                integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
                if (integer_definition) {
                        ctf_update_timestamp(stream, integer_definition);
+                       stream->has_timestamp = 1;
                } else {
                        if (variant) {
                                integer_definition = lookup_integer(variant, "timestamp", FALSE);
                                if (integer_definition) {
                                        ctf_update_timestamp(stream, integer_definition);
+                                       stream->has_timestamp = 1;
                                }
                        }
                }
@@ -410,9 +415,9 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
                file_stream->parent.timestamp = index->timestamp_begin;
                pos->content_size = index->content_size;
                pos->packet_size = index->packet_size;
-               if (index->data_offset < index->content_size)
+               if (index->data_offset <= index->content_size) {
                        pos->offset = 0;        /* will read headers */
-               else {
+               else {
                        pos->offset = EOF;
                        return;
                }
@@ -453,24 +458,51 @@ int packet_metadata(struct ctf_trace *td, FILE *fp)
        if (magic == TSDL_MAGIC) {
                ret = 1;
                td->byte_order = BYTE_ORDER;
+               CTF_TRACE_SET_FIELD(td, byte_order);
        } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
                ret = 1;
                td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
                                        LITTLE_ENDIAN : BIG_ENDIAN;
+               CTF_TRACE_SET_FIELD(td, byte_order);
        }
-       CTF_TRACE_SET_FIELD(td, byte_order);
 end:
        rewind(fp);
        return ret;
 }
 
+/*
+ * Returns 0 on success, -1 on error.
+ */
+static
+int check_version(unsigned int major, unsigned int minor)
+{
+       switch (major) {
+       case 1:
+               switch (minor) {
+               case 8:
+                       return 0;
+               default:
+                       goto warning;
+               }
+       default:
+               goto warning;
+               
+       }
+
+       /* eventually return an error instead of warning */
+warning:
+       fprintf(stdout, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
+               major, minor);
+       return 0;
+}
+
 static
 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                                        FILE *out)
 {
        struct metadata_packet_header header;
        size_t readlen, writelen, toread;
-       char buf[4096];
+       char buf[4096 + 1];     /* + 1 for debug-mode \0 */
        int ret = 0;
 
        readlen = fread(&header, header_sizeof(header), 1, in);
@@ -500,6 +532,8 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
                        header.checksum_scheme);
                return -EINVAL;
        }
+       if (check_version(header.major, header.minor) < 0)
+               return -EINVAL;
        if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
                memcpy(td->uuid, header.uuid, sizeof(header.uuid));
                CTF_TRACE_SET_FIELD(td, uuid);
@@ -511,12 +545,13 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
        toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
 
        for (;;) {
-               readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in);
+               readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
                if (ferror(in)) {
                        ret = -EINVAL;
                        break;
                }
                if (babeltrace_debug) {
+                       buf[readlen] = '\0';
                        fprintf(stdout, "[debug] metadata packet read: %s\n",
                                buf);
                }
@@ -605,6 +640,21 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td)
                ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
                if (ret)
                        goto end_packet_read;
+       } else {
+               unsigned int major, minor;
+               ssize_t nr_items;
+
+               td->byte_order = BYTE_ORDER;
+
+               /* Check text-only metadata header and version */
+               nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
+               if (nr_items < 2)
+                       fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
+               if (check_version(major, minor) < 0) {
+                       ret = -EINVAL;
+                       goto end_packet_read;
+               }
+               rewind(fp);
        }
 
        scanner = ctf_scanner_alloc(fp);
@@ -633,7 +683,7 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td)
                goto end;
        }
        ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
-                       td, BYTE_ORDER);
+                       td, td->byte_order);
        if (ret) {
                fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
                goto end;
@@ -902,13 +952,12 @@ int create_stream_packet_index(struct ctf_trace *td,
                                return -EINVAL;
                        }
                        file_stream->parent.stream_class = stream;
+                       ret = create_stream_definitions(td, &file_stream->parent);
+                       if (ret)
+                               return ret;
                }
                first_packet = 0;
 
-               ret = create_stream_definitions(td, &file_stream->parent);
-               if (ret)
-                       return ret;
-
                if (file_stream->parent.stream_packet_context) {
                        /* Read packet context */
                        ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
This page took 0.025552 seconds and 4 git commands to generate.