+static
+uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
+ struct bt_trace_handle *handle)
+{
+ struct ctf_trace *tin;
+ uint64_t begin = ULLONG_MAX;
+ int i, j;
+
+ tin = container_of(descriptor, struct ctf_trace, parent);
+
+ if (!tin)
+ goto error;
+
+ /* for each stream_class */
+ for (i = 0; i < tin->streams->len; i++) {
+ struct ctf_stream_declaration *stream_class;
+
+ stream_class = g_ptr_array_index(tin->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_file_stream *cfs;
+ struct ctf_stream_pos *stream_pos;
+ struct packet_index *index;
+
+ stream = g_ptr_array_index(stream_class->streams, j);
+ cfs = container_of(stream, struct ctf_file_stream,
+ parent);
+ stream_pos = &cfs->pos;
+
+ index = &g_array_index(stream_pos->packet_index,
+ struct packet_index, 0);
+ if (index->timestamp_begin < begin)
+ begin = index->timestamp_begin;
+ }
+ }
+
+ return begin;
+
+error:
+ return -1ULL;
+}
+
+static
+uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
+ struct bt_trace_handle *handle)
+{
+ struct ctf_trace *tin;
+ uint64_t end = 0;
+ int i, j;
+
+ tin = container_of(descriptor, struct ctf_trace, parent);
+
+ if (!tin)
+ goto error;
+
+ /* for each stream_class */
+ for (i = 0; i < tin->streams->len; i++) {
+ struct ctf_stream_declaration *stream_class;
+
+ stream_class = g_ptr_array_index(tin->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_file_stream *cfs;
+ struct ctf_stream_pos *stream_pos;
+ struct packet_index *index;
+
+ stream = g_ptr_array_index(stream_class->streams, j);
+ cfs = container_of(stream, struct ctf_file_stream,
+ parent);
+ stream_pos = &cfs->pos;
+
+ index = &g_array_index(stream_pos->packet_index,
+ struct packet_index,
+ stream_pos->packet_index->len - 1);
+ if (index->timestamp_end > end)
+ end = index->timestamp_end;
+ }
+ }
+
+ return end;
+
+error:
+ return -1ULL;
+}
+
+/*
+ * Update stream current timestamp, keep at clock frequency.
+ */
+static
+void ctf_update_timestamp(struct ctf_stream_definition *stream,
+ struct definition_integer *integer_definition)
+{
+ struct declaration_integer *integer_declaration =
+ integer_definition->declaration;
+ uint64_t oldval, newval, updateval;
+
+ if (unlikely(integer_declaration->len == 64)) {
+ stream->timestamp = integer_definition->value._unsigned;
+ return;
+ }
+ /* keep low bits */
+ oldval = stream->timestamp;
+ oldval &= (1ULL << integer_declaration->len) - 1;
+ newval = integer_definition->value._unsigned;
+ /* Test for overflow by comparing low bits */
+ if (newval < oldval)
+ newval += 1ULL << integer_declaration->len;
+ /* updateval contains old high bits, and new low bits (sum) */
+ updateval = stream->timestamp;
+ updateval &= ~((1ULL << integer_declaration->len) - 1);
+ updateval += newval;
+ stream->prev_timestamp = stream->timestamp;
+ stream->timestamp = updateval;
+}
+
+/*
+ * Print timestamp, rescaling clock frequency to nanoseconds and
+ * applying offsets as needed (unix time).
+ */
+void ctf_print_timestamp(FILE *fp,
+ struct ctf_stream_definition *stream,
+ uint64_t timestamp)
+{
+ uint64_t ts_sec = 0, ts_nsec;
+
+ if (opt_clock_raw) {
+ ts_nsec = ctf_get_timestamp_raw(stream, timestamp);
+ } else {
+ ts_nsec = ctf_get_timestamp(stream, timestamp);
+ }
+
+ /* Add command-line offset */
+ ts_sec += opt_clock_offset;
+
+ ts_sec += ts_nsec / NSEC_PER_SEC;
+ ts_nsec = ts_nsec % NSEC_PER_SEC;
+
+ if (!opt_clock_seconds) {
+ struct tm tm;
+ time_t time_s = (time_t) ts_sec;
+
+ if (!opt_clock_gmt) {
+ struct tm *res;
+
+ res = localtime_r(&time_s, &tm);
+ if (!res) {
+ fprintf(stderr, "[warning] Unable to get localtime.\n");
+ goto seconds;
+ }
+ } else {
+ struct tm *res;
+
+ res = gmtime_r(&time_s, &tm);
+ if (!res) {
+ fprintf(stderr, "[warning] Unable to get gmtime.\n");
+ goto seconds;
+ }
+ }
+ if (opt_clock_date) {
+ char timestr[26];
+ size_t res;
+
+ /* Print date and time */
+ res = strftime(timestr, sizeof(timestr),
+ "%F ", &tm);
+ if (!res) {
+ fprintf(stderr, "[warning] Unable to print ascii time.\n");
+ goto seconds;
+ }
+ fprintf(fp, "%s", timestr);
+ }
+ /* Print time in HH:MM:SS.ns */
+ fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
+ tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
+ goto end;
+ }
+seconds:
+ fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
+ ts_sec, ts_nsec);
+
+end:
+ return;
+}
+
+static
+int ctf_read_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
+{
+ struct ctf_stream_pos *pos =
+ container_of(ppos, struct ctf_stream_pos, parent);
+ struct ctf_stream_declaration *stream_class = stream->stream_class;
+ struct ctf_event_definition *event;
+ uint64_t id = 0;
+ int ret;
+
+ /* We need to check for EOF here for empty files. */
+ if (unlikely(pos->offset == EOF))
+ return EOF;
+
+ ctf_pos_get_event(pos);
+
+ /* save the current position as a restore point */
+ pos->last_offset = pos->offset;
+ /* we just read the event, it is consumed when used by the caller */
+ stream->consumed = 0;
+
+ /*
+ * This is the EOF check after we've advanced the position in
+ * ctf_pos_get_event.
+ */
+ if (unlikely(pos->offset == EOF))
+ return EOF;
+ assert(pos->offset < pos->content_size);
+
+ /* Read event header */
+ if (likely(stream->stream_event_header)) {
+ struct definition_integer *integer_definition;
+ struct definition *variant;
+
+ ret = generic_rw(ppos, &stream->stream_event_header->p);
+ if (unlikely(ret))
+ goto error;
+ /* lookup event id */
+ integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
+ if (integer_definition) {
+ id = integer_definition->value._unsigned;
+ } else {
+ struct definition_enum *enum_definition;
+
+ enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
+ if (enum_definition) {
+ id = enum_definition->integer->value._unsigned;
+ }
+ }
+
+ variant = lookup_variant(&stream->stream_event_header->p, "v");
+ if (variant) {
+ integer_definition = lookup_integer(variant, "id", FALSE);
+ if (integer_definition) {
+ id = integer_definition->value._unsigned;
+ }
+ }
+ stream->event_id = id;
+
+ /* lookup timestamp */
+ stream->has_timestamp = 0;
+ integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
+ if (integer_definition) {
+ ctf_update_timestamp(stream, integer_definition);
+ stream->has_timestamp = 1;
+ } else {
+ if (variant) {
+ integer_definition = lookup_integer(variant, "timestamp", FALSE);
+ if (integer_definition) {
+ ctf_update_timestamp(stream, integer_definition);
+ stream->has_timestamp = 1;
+ }
+ }
+ }
+ }
+
+ /* Read stream-declared event context */
+ if (stream->stream_event_context) {
+ ret = generic_rw(ppos, &stream->stream_event_context->p);
+ if (ret)
+ goto error;
+ }
+
+ if (unlikely(id >= stream_class->events_by_id->len)) {
+ fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
+ return -EINVAL;
+ }
+ event = g_ptr_array_index(stream->events_by_id, id);
+ if (unlikely(!event)) {
+ fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
+ return -EINVAL;
+ }
+
+ /* Read event-declared event context */
+ if (event->event_context) {
+ ret = generic_rw(ppos, &event->event_context->p);
+ if (ret)
+ goto error;
+ }
+
+ /* Read event payload */
+ if (likely(event->event_fields)) {
+ ret = generic_rw(ppos, &event->event_fields->p);
+ if (ret)
+ goto error;
+ }
+
+ return 0;
+
+error:
+ fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
+ return ret;
+}
+
+static
+int ctf_write_event(struct stream_pos *pos, struct ctf_stream_definition *stream)
+{
+ struct ctf_stream_declaration *stream_class = stream->stream_class;
+ struct ctf_event_definition *event;
+ uint64_t id;
+ int ret;
+
+ id = stream->event_id;
+
+ /* print event header */
+ if (likely(stream->stream_event_header)) {
+ ret = generic_rw(pos, &stream->stream_event_header->p);
+ if (ret)
+ goto error;
+ }
+
+ /* print stream-declared event context */
+ if (stream->stream_event_context) {
+ ret = generic_rw(pos, &stream->stream_event_context->p);
+ if (ret)
+ goto error;
+ }
+
+ if (unlikely(id >= stream_class->events_by_id->len)) {
+ fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
+ return -EINVAL;
+ }
+ event = g_ptr_array_index(stream->events_by_id, id);
+ if (unlikely(!event)) {
+ fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
+ return -EINVAL;
+ }
+
+ /* print event-declared event context */
+ if (event->event_context) {
+ ret = generic_rw(pos, &event->event_context->p);
+ if (ret)
+ goto error;
+ }
+
+ /* Read and print event payload */
+ if (likely(event->event_fields)) {
+ ret = generic_rw(pos, &event->event_fields->p);
+ if (ret)
+ goto error;
+ }
+
+ return 0;
+
+error:
+ fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
+ return ret;
+}
+