X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;ds=sidebyside;f=formats%2Fctf%2Fctf.c;h=29f95fec1bcae16ba7a8dcc53665c51d18d7729b;hb=2b9a764d5e9e90d1a84e9eb2d433679ad65e91bf;hp=13c87eae176651fe97b1fd0c25d3871e5f02f6a4;hpb=7f4b5c4d89ec59fed423be83ca49811459329679;p=babeltrace.git diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 13c87eae..29f95fec 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -52,7 +52,9 @@ extern int yydebug; +static struct trace_descriptor *ctf_open_trace(const char *path, int flags); +static void ctf_close_trace(struct trace_descriptor *descriptor); static @@ -63,8 +65,8 @@ rw_dispatch read_dispatch_table[] = { [ CTF_TYPE_STRING ] = ctf_string_read, [ CTF_TYPE_STRUCT ] = ctf_struct_rw, [ CTF_TYPE_VARIANT ] = ctf_variant_rw, - [ CTF_TYPE_ARRAY ] = ctf_array_rw, - [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw, + [ CTF_TYPE_ARRAY ] = ctf_array_read, + [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read, }; static @@ -75,8 +77,8 @@ rw_dispatch write_dispatch_table[] = { [ CTF_TYPE_STRING ] = ctf_string_write, [ CTF_TYPE_STRUCT ] = ctf_struct_rw, [ CTF_TYPE_VARIANT ] = ctf_variant_rw, - [ CTF_TYPE_ARRAY ] = ctf_array_rw, - [ CTF_TYPE_SEQUENCE ] = ctf_sequence_rw, + [ CTF_TYPE_ARRAY ] = ctf_array_write, + [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write, }; static @@ -85,6 +87,32 @@ struct format ctf_format = { .close_trace = ctf_close_trace, }; +static +void ctf_update_timestamp(struct ctf_stream *stream, + struct definition_integer *integer_definition) +{ + struct declaration_integer *integer_declaration = + integer_definition->declaration; + uint64_t oldval, newval, updateval; + + if (integer_declaration->len == 64) { + stream->timestamp = integer_definition->value._unsigned; + return; + } + /* keep low bits */ + oldval = stream->timestamp; + oldval &= (1ULL << integer_declaration->len) - 1; + newval = integer_definition->value._unsigned; + /* Test for overflow by comparing low bits */ + if (newval < oldval) + newval += 1ULL << integer_declaration->len; + /* updateval contains old high bits, and new low bits (sum) */ + updateval = stream->timestamp; + updateval &= ~((1ULL << integer_declaration->len) - 1); + updateval += newval; + stream->timestamp = updateval; +} + static int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) { @@ -93,7 +121,6 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) struct ctf_stream_class *stream_class = stream->stream_class; struct ctf_event *event_class; uint64_t id = 0; - int len_index; int ret; if (pos->offset == EOF) @@ -101,38 +128,45 @@ int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream) /* Read event header */ if (stream_class->event_header) { + struct definition_integer *integer_definition; + struct definition *variant; + ret = generic_rw(ppos, &stream_class->event_header->p); if (ret) goto error; /* lookup event id */ - len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl, - g_quark_from_static_string("id")); - if (len_index >= 0) { - struct definition_integer *defint; - struct definition *field; - - field = struct_definition_get_field_from_index(stream_class->event_header, len_index); - assert(field->declaration->id == CTF_TYPE_INTEGER); - defint = container_of(field, struct definition_integer, p); - assert(defint->declaration->signedness == FALSE); - id = defint->value._unsigned; /* set id */ + integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE); + if (integer_definition) { + id = integer_definition->value._unsigned; + } else { + struct definition_enum *enum_definition; + + enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE); + if (enum_definition) { + id = enum_definition->integer->value._unsigned; + } } - /* lookup timestamp */ - len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl, - g_quark_from_static_string("timestamp")); - if (len_index >= 0) { - struct definition_integer *defint; - struct definition *field; - - field = struct_definition_get_field_from_index(stream_class->event_header, len_index); - assert(field->declaration->id == CTF_TYPE_INTEGER); - defint = container_of(field, struct definition_integer, p); - assert(defint->declaration->signedness == FALSE); - /* update timestamp */ - stream->timestamp = defint->value._unsigned; + variant = lookup_variant(&stream_class->event_header->p, "v"); + if (variant) { + integer_definition = lookup_integer(variant, "id", FALSE); + if (integer_definition) { + id = integer_definition->value._unsigned; + } } + /* lookup timestamp */ + integer_definition = lookup_integer(&stream_class->event_header->p, "timestamp", FALSE); + if (integer_definition) { + ctf_update_timestamp(stream, integer_definition); + } else { + if (variant) { + integer_definition = lookup_integer(variant, "timestamp", FALSE); + if (integer_definition) { + ctf_update_timestamp(stream, integer_definition); + } + } + } } /* Read stream-declared event context */ @@ -179,23 +213,32 @@ int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream) struct ctf_stream_class *stream_class = stream->stream_class; struct ctf_event *event_class; uint64_t id = 0; - int len_index; int ret; /* print event header */ if (stream_class->event_header) { + struct definition_integer *integer_definition; + struct definition *variant; + /* lookup event id */ - len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl, - g_quark_from_static_string("id")); - if (len_index >= 0) { - struct definition_integer *defint; - struct definition *field; - - field = struct_definition_get_field_from_index(stream_class->event_header, len_index); - assert(field->declaration->id == CTF_TYPE_INTEGER); - defint = container_of(field, struct definition_integer, p); - assert(defint->declaration->signedness == FALSE); - id = defint->value._unsigned; /* set id */ + integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE); + if (integer_definition) { + id = integer_definition->value._unsigned; + } else { + struct definition_enum *enum_definition; + + enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE); + if (enum_definition) { + id = enum_definition->integer->value._unsigned; + } + } + + variant = lookup_variant(&stream_class->event_header->p, "v"); + if (variant) { + integer_definition = lookup_integer(variant, "id", FALSE); + if (integer_definition) { + id = integer_definition->value._unsigned; + } } ret = generic_rw(pos, &stream_class->event_header->p); @@ -364,7 +407,12 @@ void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence) /* Lookup context/packet size in index */ pos->content_size = index->content_size; pos->packet_size = index->packet_size; - pos->offset = index->data_offset; + if (index->data_offset < index->content_size) + pos->offset = index->data_offset; + else { + pos->offset = EOF; + return; + } } /* map new base. Need mapping length from header. */ pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot, @@ -453,7 +501,11 @@ int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in, ret = -EINVAL; break; } - printf("read %s\n", buf); + if (babeltrace_debug) { + fprintf(stdout, "[debug] metadata packet read: %s\n", + buf); + } + writelen = fwrite(buf, sizeof(char), readlen, out); if (writelen < readlen) { ret = -EIO; @@ -481,6 +533,11 @@ int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, int ret; in = *fp; + /* + * Using strlen on *buf instead of size of open_memstream + * because its size includes garbage at the end (after final + * \0). This is the allocated size, not the actual string size. + */ out = open_memstream(buf, &size); if (out == NULL) return -errno; @@ -498,7 +555,7 @@ int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, fclose(out); /* flush the buffer */ fclose(in); /* open for reading */ - *fp = fmemopen(*buf, size, "rb"); + *fp = fmemopen(*buf, strlen(*buf), "rb"); return 0; } @@ -883,6 +940,7 @@ error: return ret; } +static struct trace_descriptor *ctf_open_trace(const char *path, int flags) { struct ctf_trace *td; @@ -921,6 +979,7 @@ void ctf_close_file_stream(struct ctf_file_stream *file_stream) close(file_stream->pos.fd); } +static void ctf_close_trace(struct trace_descriptor *tdp) { struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent); @@ -930,7 +989,10 @@ void ctf_close_trace(struct trace_descriptor *tdp) for (i = 0; i < td->streams->len; i++) { struct ctf_stream_class *stream; int j; + stream = g_ptr_array_index(td->streams, i); + if (!stream) + continue; for (j = 0; j < stream->files->len; j++) { struct ctf_file_stream *file_stream; file_stream = g_ptr_array_index(stream->files, j);