Fix: don't access missing ctf trace when getting metadata
[babeltrace.git] / formats / lttng-live / lttng-live-comm.c
index c19cc6460b5b06ee07ed46e6e5b5f54adf17d80a..8059b338b095dd4672021946ae497b3f8a45f7dd 100644 (file)
@@ -607,6 +607,11 @@ int append_metadata(struct lttng_live_ctx *ctx,
        struct lttng_live_viewer_stream *metadata;
        char *metadata_buf = NULL;
 
+       if (!viewer_stream->ctf_trace->handle) {
+               printf_verbose("append_metadata: trace handle not ready yet.\n");
+               return 0;
+       }
+
        printf_verbose("get_next_index: new metadata needed\n");
        ret = get_new_metadata(ctx, viewer_stream, &metadata_buf);
        if (ret < 0) {
@@ -1033,6 +1038,13 @@ retry:
        switch (be32toh(rp->status)) {
        case LTTNG_VIEWER_INDEX_INACTIVE:
                printf_verbose("get_next_index: inactive\n");
+
+               if (index->ts_cycles.timestamp_end ==
+                               be64toh(rp->timestamp_end)) {
+                       /* Already seen this timestamp. */
+                       (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
+               }
+
                memset(index, 0, sizeof(struct packet_index));
                index->ts_cycles.timestamp_end = be64toh(rp->timestamp_end);
                *stream_id = be64toh(rp->stream_id);
@@ -1269,14 +1281,30 @@ retry:
        }
 
        if (cur_index->content_size == 0) {
+               /* Beacon packet index */
                if (file_stream->parent.stream_class) {
                        file_stream->parent.cycles_timestamp =
                                cur_index->ts_cycles.timestamp_end;
                        file_stream->parent.real_timestamp = ctf_get_real_timestamp(
                                        &file_stream->parent,
                                        cur_index->ts_cycles.timestamp_end);
+
+                       /*
+                        * Duplicate the data from the previous index, because
+                        * the one we just received is only a beacon with no
+                        * relevant information except the timestamp_end. We
+                        * don't need to keep this timestamp_end because we already
+                        * updated the file_stream timestamps, so we only need
+                        * to keep the last real index data as prev_index. That
+                        * way, we keep the original prev timestamps and
+                        * discarded events counter. This is the same behaviour
+                        * as if we were reading a local trace, we would not
+                        * have fake indexes between real indexes.
+                        */
+                       memcpy(cur_index, prev_index, sizeof(struct packet_index));
                }
        } else {
+               /* Real packet index */
                if (file_stream->parent.stream_class) {
                        /* Convert the timestamps and append to the real_index. */
                        cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
@@ -1290,6 +1318,16 @@ retry:
                ctf_update_current_packet_index(&file_stream->parent,
                                prev_index, cur_index);
 
+               /*
+                * We need to check if we are in trace read or called
+                * from packet indexing.  In this last case, the
+                * collection is not there, so we cannot print the
+                * timestamps.
+                */
+               if ((&file_stream->parent)->stream_class->trace->parent.collection) {
+                       ctf_print_discarded_lost(stderr, &file_stream->parent);
+               }
+
                file_stream->parent.cycles_timestamp =
                                cur_index->ts_cycles.timestamp_begin;
                file_stream->parent.real_timestamp =
This page took 0.023446 seconds and 4 git commands to generate.