X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf%2Fctf.c;h=f078735c3ea9fd5307c46333b28b2c473867a7e1;hp=4043055824b5e24fe253a7835ce9cc95fd989ab1;hb=5f643ed7d0d2d21d3797938e34735ef8f1bd9186;hpb=03b1e5430ff003071767b232e0cc627d7d8147e9 diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 40430558..f078735c 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -125,8 +125,11 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) uint64_t id = 0; int ret; + ctf_pos_get_event(pos); + if (pos->offset == EOF) return EOF; + assert(pos->offset < pos->content_size); /* Read event header */ if (stream->stream_event_header) { @@ -390,8 +393,11 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) assert(off >= 0); pos->offset = 0; } else { + read_next_packet: switch (whence) { case SEEK_CUR: + if (pos->offset == EOF) + return; /* The reader will expect us to skip padding */ assert(pos->offset + offset == pos->content_size); ++pos->cur_index; @@ -415,9 +421,14 @@ 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 if (index->data_offset == index->content_size) { + /* empty packet */ + pos->offset = index->data_offset; + offset = 0; + goto read_next_packet; + } else { pos->offset = EOF; return; } @@ -458,17 +469,44 @@ 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) @@ -505,6 +543,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); @@ -539,10 +579,19 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in, toread -= readlen; if (!toread) { ret = 0; /* continue reading next packet */ - break; + goto read_padding; } } return ret; + +read_padding: + toread = (header.packet_size - header.content_size) / CHAR_BIT; + ret = fseek(in, toread, SEEK_CUR); + if (ret < 0) { + fprintf(stdout, "[warning] Missing padding at end of file\n"); + ret = 0; + } + return ret; } static @@ -607,11 +656,25 @@ int ctf_open_trace_metadata_read(struct ctf_trace *td) goto end_stream; } - td->byte_order = BYTE_ORDER; if (packet_metadata(td, fp)) { 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);