+ struct ctf_stream_declaration *stream;
+ uint64_t stream_id = 0;
+ int ret;
+
+ 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);
+ ret = -EINVAL;
+ goto end;
+ }
+ stream = g_ptr_array_index(td->streams, stream_id);
+ if (!stream) {
+ fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
+ "in metadata.\n", stream_id);
+ ret = -EINVAL;
+ goto end;
+ }
+ file_stream->parent.stream_class = stream;
+ ret = create_stream_definitions(td, &file_stream->parent);
+end:
+ return ret;
+}
+
+static
+int ctf_open_mmap_stream_read(struct ctf_trace *td,
+ struct bt_mmap_stream *mmap_info,
+ void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+ int whence))
+{
+ int ret;
+ struct ctf_file_stream *file_stream;
+
+ file_stream = g_new0(struct ctf_file_stream, 1);
+ file_stream->pos.last_offset = LAST_OFFSET_POISON;
+ ctf_init_mmap_pos(&file_stream->pos, mmap_info);
+
+ file_stream->pos.packet_seek = packet_seek;
+
+ ret = create_trace_definitions(td, &file_stream->parent);
+ if (ret) {
+ goto error_def;
+ }
+
+ ret = prepare_mmap_stream_definition(td, file_stream);
+ if (ret)
+ goto error_index;
+
+ /*
+ * For now, only a single clock per trace is supported.
+ */
+ file_stream->parent.current_clock = td->single_clock;
+
+ /* 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)
+ bt_definition_unref(&file_stream->parent.trace_packet_header->p);
+error_def:
+ g_free(file_stream);
+ return ret;
+}
+
+static
+int ctf_open_mmap_trace_read(struct ctf_trace *td,
+ struct bt_mmap_stream_list *mmap_list,
+ void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+ int whence),
+ FILE *metadata_fp)
+{
+ int ret;
+ struct bt_mmap_stream *mmap_info;
+
+ ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
+ if (ret) {
+ goto error;
+ }
+
+ /*
+ * for each stream, try to open, check magic number, and get the
+ * stream ID to add to the right location in the stream array.
+ */
+ bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
+ ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
+ if (ret) {
+ fprintf(stderr, "[error] Open file mmap stream error.\n");
+ goto error;
+ }
+ }
+
+ return 0;
+
+error:
+ return ret;
+}
+
+static
+struct bt_trace_descriptor *ctf_open_mmap_trace(
+ struct bt_mmap_stream_list *mmap_list,
+ void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+ int whence),
+ FILE *metadata_fp)
+{
+ struct ctf_trace *td;
+ int ret;
+
+ if (!metadata_fp) {
+ fprintf(stderr, "[error] No metadata file pointer associated, "
+ "required for mmap parsing\n");
+ goto error;
+ }
+ if (!packet_seek) {
+ fprintf(stderr, "[error] packet_seek function undefined.\n");
+ goto error;
+ }
+ td = g_new0(struct ctf_trace, 1);
+ ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
+ if (ret)
+ goto error_free;
+
+ return &td->parent;
+
+error_free:
+ g_free(td);
+error:
+ return NULL;
+}
+
+static
+int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
+{
+ int i, j, k;