+static
+int trace_pre_handler(struct bt_trace_descriptor *td_write,
+ struct bt_context *ctx)
+{
+ struct ctf_text_stream_pos *sout;
+ struct trace_collection *tc;
+ int ret, i;
+
+ sout = container_of(td_write, struct ctf_text_stream_pos,
+ trace_descriptor);
+
+ if (!sout->parent.pre_trace_cb)
+ return 0;
+
+ tc = ctx->tc;
+ for (i = 0; i < tc->array->len; i++) {
+ struct bt_trace_descriptor *td =
+ g_ptr_array_index(tc->array, i);
+
+ ret = sout->parent.pre_trace_cb(&sout->parent, td);
+ if (ret) {
+ fprintf(stderr, "[error] Writing to trace pre handler failed.\n");
+ goto end;
+ }
+ }
+ ret = 0;
+end:
+ return ret;
+}
+
+static
+int trace_post_handler(struct bt_trace_descriptor *td_write,
+ struct bt_context *ctx)
+{
+ struct ctf_text_stream_pos *sout;
+ struct trace_collection *tc;
+ int ret, i;
+
+ sout = container_of(td_write, struct ctf_text_stream_pos,
+ trace_descriptor);
+
+ if (!sout->parent.post_trace_cb)
+ return 0;
+
+ tc = ctx->tc;
+ for (i = 0; i < tc->array->len; i++) {
+ struct bt_trace_descriptor *td =
+ g_ptr_array_index(tc->array, i);
+
+ ret = sout->parent.post_trace_cb(&sout->parent, td);
+ if (ret) {
+ fprintf(stderr, "[error] Writing to trace post handler failed.\n");
+ goto end;
+ }
+ }
+ ret = 0;
+end:
+ return ret;
+}
+
+static
+struct bt_ctf_iter *iter_create_intersect(struct bt_context *ctx,
+ struct bt_iter_pos *inter_begin_pos,
+ struct bt_iter_pos *inter_end_pos)
+{
+ uint64_t begin = 0, end = ULLONG_MAX;
+ /* Useless but needed for bt_iter_create_time_pos. */
+ struct bt_iter bt_iter;
+ int ret;
+
+ ret = ctf_find_packets_intersection(ctx, &begin, &end);
+ if (ret == 1) {
+ fprintf(stderr, "[error] No intersection found between trace files.\n");
+ ret = -1;
+ goto error;
+ } else if (ret != 0) {
+ goto error;
+ }
+ inter_begin_pos = bt_iter_create_time_pos(&bt_iter, begin);
+ if (!inter_begin_pos) {
+ goto error;
+ }
+ inter_end_pos = bt_iter_create_time_pos(&bt_iter, end);
+ if (!inter_end_pos) {
+ goto error;
+ }
+
+ return bt_ctf_iter_create(ctx, inter_begin_pos,
+ inter_end_pos);
+error:
+ return NULL;
+}
+
+static
+int convert_trace(struct bt_trace_descriptor *td_write,