+ close(metadata_stream->pos.fd);
+ if (ret)
+ g_free(metadata_stream);
+ return ret;
+}
+
+static
+struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
+ struct ctf_stream *stream,
+ struct ctf_event *event)
+{
+ struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
+
+ if (event->context_decl) {
+ struct definition *definition =
+ event->context_decl->p.definition_new(&event->context_decl->p,
+ stream->parent_def_scope, 0, 0, "event.context");
+ if (!definition) {
+ goto error;
+ }
+ stream_event->event_context = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = stream_event->event_context->p.scope;
+ }
+ if (event->fields_decl) {
+ struct definition *definition =
+ event->fields_decl->p.definition_new(&event->fields_decl->p,
+ stream->parent_def_scope, 0, 0, "event.fields");
+ if (!definition) {
+ goto error;
+ }
+ stream_event->event_fields = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = stream_event->event_fields->p.scope;
+ }
+ return stream_event;
+
+error:
+ if (stream_event->event_fields)
+ definition_unref(&stream_event->event_fields->p);
+ if (stream_event->event_context)
+ definition_unref(&stream_event->event_context->p);
+ return NULL;
+}
+
+static
+int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
+{
+ struct ctf_stream_class *stream_class;
+ int ret;
+ int i;
+
+ if (stream->stream_definitions_created)
+ return 0;
+
+ stream_class = stream->stream_class;
+
+ if (stream_class->packet_context_decl) {
+ struct definition *definition =
+ stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.packet.context");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_packet_context = container_of(definition,
+ struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_packet_context->p.scope;
+ }
+ if (stream_class->event_header_decl) {
+ struct definition *definition =
+ stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.event.header");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_event_header =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_event_header->p.scope;
+ }
+ if (stream_class->event_context_decl) {
+ struct definition *definition =
+ stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
+ stream->parent_def_scope, 0, 0, "stream.event.context");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->stream_event_context =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->stream_event_context->p.scope;
+ }
+ stream->events_by_id = g_ptr_array_new();
+ g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
+ for (i = 0; i < stream->events_by_id->len; i++) {
+ struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
+ struct ctf_stream_event *stream_event;
+
+ if (!event)
+ continue;
+ stream_event = create_event_definitions(td, stream, event);
+ if (!stream_event)
+ goto error_event;
+ g_ptr_array_index(stream->events_by_id, i) = stream_event;
+ }
+ return 0;
+
+error_event:
+ for (i = 0; i < stream->events_by_id->len; i++) {
+ struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
+ if (stream_event)
+ g_free(stream_event);
+ }
+ g_ptr_array_free(stream->events_by_id, TRUE);
+error:
+ if (stream->stream_event_context)
+ definition_unref(&stream->stream_event_context->p);
+ if (stream->stream_event_header)
+ definition_unref(&stream->stream_event_header->p);
+ if (stream->stream_packet_context)
+ definition_unref(&stream->stream_packet_context->p);
+ return ret;
+}
+
+
+static
+int create_stream_packet_index(struct ctf_trace *td,
+ struct ctf_file_stream *file_stream)
+{
+ struct ctf_stream_class *stream;
+ int len_index;
+ struct ctf_stream_pos *pos;
+ struct stat filestats;
+ struct packet_index packet_index;
+ int first_packet = 1;
+ int ret;
+
+ pos = &file_stream->pos;
+
+ ret = fstat(pos->fd, &filestats);
+ if (ret < 0)
+ return ret;
+
+ if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
+ return -EINVAL;
+
+ for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
+ uint64_t stream_id = 0;
+
+ if (pos->base) {
+ /* unmap old base */
+ ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+ if (ret) {
+ fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
+ strerror(errno));
+ return ret;
+ }
+ pos->base = NULL;
+ }
+ /* map new base. Need mapping length from header. */
+ pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
+ MAP_PRIVATE, pos->fd, pos->mmap_offset);
+ pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
+ pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
+ pos->offset = 0; /* Position of the packet header */
+
+ packet_index.offset = pos->mmap_offset;
+ packet_index.content_size = 0;
+ packet_index.packet_size = 0;
+ packet_index.timestamp_begin = 0;
+ packet_index.timestamp_end = 0;
+ packet_index.events_discarded = 0;
+
+ /* read and check header, set stream id (and check) */
+ if (file_stream->parent.trace_packet_header) {
+ /* Read packet header */
+ ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
+ if (ret)
+ return ret;
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
+ if (len_index >= 0) {
+ struct definition *field;
+ uint64_t magic;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ magic = get_unsigned_int(field);
+ if (magic != CTF_MAGIC) {
+ fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
+ magic,
+ file_stream->pos.packet_index->len,
+ (ssize_t) pos->mmap_offset);
+ return -EINVAL;
+ }
+ }
+
+ /* check uuid */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
+ if (len_index >= 0) {
+ struct definition_array *defarray;
+ struct definition *field;
+ uint64_t i;
+ uint8_t uuidval[BABELTRACE_UUID_LEN];
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ assert(field->declaration->id == CTF_TYPE_ARRAY);
+ defarray = container_of(field, struct definition_array, p);
+ assert(array_len(defarray) == BABELTRACE_UUID_LEN);
+
+ for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
+ struct definition *elem;
+
+ elem = array_index(defarray, i);
+ uuidval[i] = get_unsigned_int(elem);
+ }
+ ret = babeltrace_uuid_compare(td->uuid, uuidval);
+ if (ret) {
+ fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
+ return -EINVAL;
+ }
+ }
+
+
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ stream_id = get_unsigned_int(field);
+ }
+ }
+
+ if (!first_packet && file_stream->parent.stream_id != stream_id) {
+ fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
+ return -EINVAL;
+ }
+ if (first_packet) {
+ file_stream->parent.stream_id = stream_id;
+ if (stream_id >= td->streams->len) {
+ fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
+ return -EINVAL;
+ }
+ stream = g_ptr_array_index(td->streams, stream_id);
+ if (!stream) {
+ fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
+ return -EINVAL;
+ }
+ file_stream->parent.stream_class = stream;
+ ret = create_stream_definitions(td, &file_stream->parent);
+ if (ret)
+ return ret;
+ }
+ first_packet = 0;
+
+ if (file_stream->parent.stream_packet_context) {
+ /* Read packet context */
+ ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
+ if (ret)
+ return ret;
+ /* read content size from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.content_size = get_unsigned_int(field);
+ } else {
+ /* Use file size for packet size */
+ packet_index.content_size = filestats.st_size * CHAR_BIT;
+ }
+
+ /* read packet size from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.packet_size = get_unsigned_int(field);
+ } else {
+ /* Use content size if non-zero, else file size */
+ packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
+ }
+
+ /* read timestamp begin from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.timestamp_begin = get_unsigned_int(field);
+ }
+
+ /* read timestamp end from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.timestamp_end = get_unsigned_int(field);
+ }
+
+ /* read events discarded from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.events_discarded = get_unsigned_int(field);
+ }
+ } else {
+ /* Use file size for packet size */
+ packet_index.content_size = filestats.st_size * CHAR_BIT;
+ /* Use content size if non-zero, else file size */
+ packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
+ }
+
+ /* Validate content size and packet size values */
+ if (packet_index.content_size > packet_index.packet_size) {
+ fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
+ packet_index.content_size, packet_index.packet_size);
+ return -EINVAL;
+ }
+
+ if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
+ fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
+ packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
+ return -EINVAL;
+ }
+
+ /* Save position after header and context */
+ packet_index.data_offset = pos->offset;
+
+ /* add index to packet array */
+ g_array_append_val(file_stream->pos.packet_index, packet_index);
+
+ pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
+ }
+
+ /* Move pos back to beginning of file */
+ ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
+
+ return 0;
+}
+
+static
+int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
+{
+ int ret;
+
+ if (td->packet_header_decl) {
+ struct definition *definition =
+ td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
+ stream->parent_def_scope, 0, 0, "trace.packet.header");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->trace_packet_header =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->trace_packet_header->p.scope;
+ }
+
+ return 0;
+
+error:
+ return ret;
+}
+
+/*
+ * Note: many file streams can inherit from the same stream class
+ * description (metadata).
+ */
+static
+int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
+ void (*packet_seek)(struct stream_pos *pos, size_t index,
+ int whence))
+{
+ int ret;
+ struct ctf_file_stream *file_stream;
+
+ ret = openat(td->dirfd, path, flags);
+ if (ret < 0) {
+ perror("File stream openat()");
+ goto error;
+ }
+ file_stream = g_new0(struct ctf_file_stream, 1);
+
+ if (packet_seek) {
+ file_stream->pos.packet_seek = packet_seek;
+ } else {
+ fprintf(stderr, "[error] packet_seek function undefined.\n");
+ ret = -1;
+ goto error_def;
+ }
+
+ ctf_init_pos(&file_stream->pos, ret, flags);
+ ret = create_trace_definitions(td, &file_stream->parent);
+ if (ret)
+ goto error_def;
+ /*
+ * For now, only a single slock is supported.
+ */
+ file_stream->parent.current_clock = td->single_clock;
+ ret = create_stream_packet_index(td, file_stream);
+ if (ret)
+ goto error_index;
+ /* Add stream file to stream class */
+ g_ptr_array_add(file_stream->parent.stream_class->streams,
+ &file_stream->parent);
+ return 0;
+
+error_index:
+ if (file_stream->parent.trace_packet_header)
+ definition_unref(&file_stream->parent.trace_packet_header->p);
+error_def:
+ ctf_fini_pos(&file_stream->pos);
+ close(file_stream->pos.fd);
+ g_free(file_stream);
+error: