+
+void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
+ struct mmap_stream *mmap_info)
+{
+ pos->mmap_offset = 0;
+ pos->packet_size = 0;
+ pos->content_size = 0;
+ pos->content_size_loc = NULL;
+ pos->fd = mmap_info->fd;
+ pos->base_mma = NULL;
+ pos->offset = 0;
+ pos->dummy = false;
+ pos->cur_index = 0;
+ pos->packet_cycles_index = NULL;
+ pos->packet_real_index = NULL;
+ pos->prot = PROT_READ;
+ pos->flags = MAP_PRIVATE;
+ pos->parent.rw_table = read_dispatch_table;
+ pos->parent.event_cb = ctf_read_event;
+}
+
+static
+int prepare_mmap_stream_definition(struct ctf_trace *td,
+ struct ctf_file_stream *file_stream)
+{
+ 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 mmap_stream *mmap_info,
+ void (*packet_seek)(struct 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 slock 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)
+ definition_unref(&file_stream->parent.trace_packet_header->p);
+error_def:
+ g_free(file_stream);
+ return ret;
+}
+
+int ctf_open_mmap_trace_read(struct ctf_trace *td,
+ struct mmap_stream_list *mmap_list,
+ void (*packet_seek)(struct stream_pos *pos, size_t index,
+ int whence),
+ FILE *metadata_fp)
+{
+ int ret;
+ struct 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 trace_descriptor *ctf_open_mmap_trace(
+ struct mmap_stream_list *mmap_list,
+ void (*packet_seek)(struct 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 trace_descriptor *tdp)
+{
+ int i, j, k;
+ struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
+
+ /* for each stream_class */
+ for (i = 0; i < td->streams->len; i++) {
+ struct ctf_stream_declaration *stream_class;
+
+ stream_class = g_ptr_array_index(td->streams, i);
+ if (!stream_class)
+ continue;
+ /* for each file_stream */
+ for (j = 0; j < stream_class->streams->len; j++) {
+ struct ctf_stream_definition *stream;
+ struct ctf_stream_pos *stream_pos;
+ struct ctf_file_stream *cfs;
+
+ stream = g_ptr_array_index(stream_class->streams, j);
+ if (!stream)
+ continue;
+ cfs = container_of(stream, struct ctf_file_stream,
+ parent);
+ stream_pos = &cfs->pos;
+ if (!stream_pos->packet_cycles_index)
+ continue;
+
+ for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
+ struct packet_index *index;
+ struct packet_index new_index;
+
+ index = &g_array_index(stream_pos->packet_cycles_index,
+ struct packet_index, k);
+ memcpy(&new_index, index,
+ sizeof(struct packet_index));
+ new_index.timestamp_begin =
+ ctf_get_real_timestamp(stream,
+ index->timestamp_begin);
+ new_index.timestamp_end =
+ ctf_get_real_timestamp(stream,
+ index->timestamp_end);
+ g_array_append_val(stream_pos->packet_real_index,
+ new_index);
+ }
+ }
+ }
+ return 0;
+}
+