+ return ret;
+}
+
+/*
+ * Make sure all the traces we know have a metadata stream or loop on
+ * ask_new_streams until it is done. This must be called before we call
+ * add_one_trace.
+ *
+ * Return 0 when all known traces have a metadata stream, a negative value
+ * on error.
+ */
+static
+int check_traces_metadata(struct lttng_live_ctx *ctx)
+{
+ int ret;
+ struct lttng_live_ctf_trace *trace;
+ GHashTableIter it;
+ gpointer key;
+ gpointer value;
+
+retry:
+ g_hash_table_iter_init(&it, ctx->session->ctf_traces);
+ while (g_hash_table_iter_next(&it, &key, &value)) {
+ trace = (struct lttng_live_ctf_trace *) value;
+ printf_verbose("Check trace %" PRIu64 " metadata\n", trace->ctf_trace_id);
+ while (!trace->metadata_stream) {
+ printf_verbose("Waiting for metadata stream\n");
+ if (lttng_live_should_quit()) {
+ ret = 0;
+ goto end;
+ }
+ ret = ask_new_streams(ctx);
+ if (ret < 0) {
+ goto end;
+ } else if (ret == 0) {
+ (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
+ } else {
+ /*
+ * If ask_new_stream got streams from a trace we did not know
+ * about until now, we have to reinitialize the iterator.
+ */
+ goto retry;
+ }
+ }
+ }
+
+ ret = 0;
+
+end:
+ printf_verbose("End check traces metadata\n");
+ return ret;
+}
+
+static
+int add_traces(struct lttng_live_ctx *ctx)
+{
+ int ret;
+ struct lttng_live_ctf_trace *trace;
+ GHashTableIter it;
+ gpointer key;
+ gpointer value;
+ unsigned int nr_traces;
+
+ printf_verbose("Begin add traces\n");
+
+retry:
+ nr_traces = g_hash_table_size(ctx->session->ctf_traces);
+
+ ret = check_traces_metadata(ctx);
+ if (ret < 0) {
+ goto end;
+ }
+
+ g_hash_table_iter_init(&it, ctx->session->ctf_traces);
+ while (g_hash_table_iter_next(&it, &key, &value)) {
+ trace = (struct lttng_live_ctf_trace *) value;
+ ret = add_one_trace(ctx, trace);
+ if (ret < 0) {
+ goto end;
+ }
+ /*
+ * If a new trace got added while we were adding the trace, the
+ * iterator is invalid and we have to restart.
+ */
+ if (g_hash_table_size(ctx->session->ctf_traces) != nr_traces) {
+ printf_verbose("New trace(s) added during add_one_trace()\n");
+ printf_verbose("JORAJ: GREP HERE\n");
+ goto retry;
+ }
+ }
+
+ ret = 0;
+
+end:
+ printf_verbose("End add traces\n");
+ return ret;