+ if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
+ return -EINVAL;
+
+ for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
+ uint64_t stream_id = 0;
+
+ if (pos->base) {
+ /* unmap old base */
+ ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
+ if (ret) {
+ fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
+ strerror(errno));
+ return ret;
+ }
+ pos->base = NULL;
+ }
+ /* map new base. Need mapping length from header. */
+ pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
+ MAP_PRIVATE, pos->fd, pos->mmap_offset);
+ pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
+ pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
+ pos->offset = 0; /* Position of the packet header */
+
+ packet_index.offset = pos->mmap_offset;
+ packet_index.content_size = 0;
+ packet_index.packet_size = 0;
+ packet_index.timestamp_begin = 0;
+ packet_index.timestamp_end = 0;
+ packet_index.events_discarded = 0;
+
+ /* read and check header, set stream id (and check) */
+ if (file_stream->parent.trace_packet_header) {
+ /* Read packet header */
+ ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
+ if (ret)
+ return ret;
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
+ if (len_index >= 0) {
+ struct definition *field;
+ uint64_t magic;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ magic = get_unsigned_int(field);
+ if (magic != CTF_MAGIC) {
+ fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
+ magic,
+ file_stream->pos.packet_index->len,
+ (ssize_t) pos->mmap_offset);
+ return -EINVAL;
+ }
+ }
+
+ /* check uuid */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
+ if (len_index >= 0) {
+ struct definition_array *defarray;
+ struct definition *field;
+ uint64_t i;
+ uint8_t uuidval[UUID_LEN];
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ assert(field->declaration->id == CTF_TYPE_ARRAY);
+ defarray = container_of(field, struct definition_array, p);
+ assert(array_len(defarray) == UUID_LEN);
+
+ for (i = 0; i < UUID_LEN; i++) {
+ struct definition *elem;
+
+ elem = array_index(defarray, i);
+ uuidval[i] = get_unsigned_int(elem);
+ }
+ ret = uuid_compare(td->uuid, uuidval);
+ if (ret) {
+ fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
+ return -EINVAL;
+ }
+ }
+
+
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
+ stream_id = get_unsigned_int(field);
+ }
+ }
+
+ if (!first_packet && file_stream->parent.stream_id != stream_id) {
+ fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
+ return -EINVAL;
+ }
+ if (first_packet) {
+ 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);
+ return -EINVAL;
+ }
+ stream = g_ptr_array_index(td->streams, stream_id);
+ if (!stream) {
+ fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
+ return -EINVAL;
+ }
+ file_stream->parent.stream_class = stream;
+ ret = create_stream_definitions(td, &file_stream->parent);
+ if (ret)
+ return ret;
+ }
+ first_packet = 0;
+
+ if (file_stream->parent.stream_packet_context) {
+ /* Read packet context */
+ ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
+ if (ret)
+ return ret;
+ /* read content size from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.content_size = get_unsigned_int(field);
+ } else {
+ /* Use file size for packet size */
+ packet_index.content_size = filestats.st_size * CHAR_BIT;
+ }
+
+ /* read packet size from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.packet_size = get_unsigned_int(field);
+ } else {
+ /* Use content size if non-zero, else file size */
+ packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
+ }
+
+ /* read timestamp begin from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.timestamp_begin = get_unsigned_int(field);
+ }
+
+ /* read timestamp end from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.timestamp_end = get_unsigned_int(field);
+ }
+
+ /* read events discarded from header */
+ len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
+ if (len_index >= 0) {
+ struct definition *field;
+
+ field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
+ packet_index.events_discarded = get_unsigned_int(field);
+ }
+ } else {
+ /* Use file size for packet size */
+ packet_index.content_size = filestats.st_size * CHAR_BIT;
+ /* Use content size if non-zero, else file size */
+ packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
+ }
+
+ /* Validate content size and packet size values */
+ if (packet_index.content_size > packet_index.packet_size) {
+ fprintf(stderr, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
+ packet_index.content_size, packet_index.packet_size);
+ return -EINVAL;
+ }
+
+ if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
+ fprintf(stderr, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
+ packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
+ return -EINVAL;
+ }
+
+ /* Save position after header and context */
+ packet_index.data_offset = pos->offset;
+
+ /* add index to packet array */
+ g_array_append_val(file_stream->pos.packet_index, packet_index);
+
+ pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
+ }
+
+ /* Move pos back to beginning of file */
+ ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
+
+ return 0;
+}
+
+static
+int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
+{
+ int ret;
+
+ if (td->packet_header_decl) {
+ struct definition *definition =
+ td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
+ stream->parent_def_scope, 0, 0, "trace.packet.header");
+ if (!definition) {
+ ret = -EINVAL;
+ goto error;
+ }
+ stream->trace_packet_header =
+ container_of(definition, struct definition_struct, p);
+ stream->parent_def_scope = stream->trace_packet_header->p.scope;
+ }
+