2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
27 #include <uuid/uuid.h>
31 #include <sys/types.h>
39 #include "metadata/ctf-scanner.h"
40 #include "metadata/ctf-parser.h"
41 #include "metadata/ctf-ast.h"
44 * We currently simply map a page to read the packet header and packet
45 * context to get the packet length and content length. (in bits)
47 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
49 #define UUID_LEN 16 /* uuid by value len */
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
58 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
60 void ctf_close_trace(struct trace_descriptor
*descriptor
);
63 rw_dispatch read_dispatch_table
[] = {
64 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
65 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
66 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
67 [ CTF_TYPE_STRING
] = ctf_string_read
,
68 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
69 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
70 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
71 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
75 rw_dispatch write_dispatch_table
[] = {
76 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
77 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
78 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
79 [ CTF_TYPE_STRING
] = ctf_string_write
,
80 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
81 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
82 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
83 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
87 struct format ctf_format
= {
88 .open_trace
= ctf_open_trace
,
89 .close_trace
= ctf_close_trace
,
93 void ctf_update_timestamp(struct ctf_stream
*stream
,
94 struct definition_integer
*integer_definition
)
96 struct declaration_integer
*integer_declaration
=
97 integer_definition
->declaration
;
98 uint64_t oldval
, newval
, updateval
;
100 if (unlikely(integer_declaration
->len
== 64)) {
101 stream
->timestamp
= integer_definition
->value
._unsigned
;
105 oldval
= stream
->timestamp
;
106 oldval
&= (1ULL << integer_declaration
->len
) - 1;
107 newval
= integer_definition
->value
._unsigned
;
108 /* Test for overflow by comparing low bits */
110 newval
+= 1ULL << integer_declaration
->len
;
111 /* updateval contains old high bits, and new low bits (sum) */
112 updateval
= stream
->timestamp
;
113 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
115 stream
->timestamp
= updateval
;
119 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
121 struct ctf_stream_pos
*pos
=
122 container_of(ppos
, struct ctf_stream_pos
, parent
);
123 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
124 struct ctf_stream_event
*event
;
128 ctf_pos_get_event(pos
);
130 if (unlikely(pos
->offset
== EOF
))
132 assert(pos
->offset
< pos
->content_size
);
134 /* Read event header */
135 if (likely(stream
->stream_event_header
)) {
136 struct definition_integer
*integer_definition
;
137 struct definition
*variant
;
139 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
142 /* lookup event id */
143 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
144 if (integer_definition
) {
145 id
= integer_definition
->value
._unsigned
;
147 struct definition_enum
*enum_definition
;
149 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
150 if (enum_definition
) {
151 id
= enum_definition
->integer
->value
._unsigned
;
155 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
157 integer_definition
= lookup_integer(variant
, "id", FALSE
);
158 if (integer_definition
) {
159 id
= integer_definition
->value
._unsigned
;
162 stream
->event_id
= id
;
164 /* lookup timestamp */
165 stream
->has_timestamp
= 0;
166 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
167 if (integer_definition
) {
168 ctf_update_timestamp(stream
, integer_definition
);
169 stream
->has_timestamp
= 1;
172 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
173 if (integer_definition
) {
174 ctf_update_timestamp(stream
, integer_definition
);
175 stream
->has_timestamp
= 1;
181 /* Read stream-declared event context */
182 if (stream
->stream_event_context
) {
183 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
188 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
189 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
192 event
= g_ptr_array_index(stream
->events_by_id
, id
);
193 if (unlikely(!event
)) {
194 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
198 /* Read event-declared event context */
199 if (event
->event_context
) {
200 ret
= generic_rw(ppos
, &event
->event_context
->p
);
205 /* Read event payload */
206 if (likely(event
->event_fields
)) {
207 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
215 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
220 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
222 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
223 struct ctf_stream_event
*event
;
227 id
= stream
->event_id
;
229 /* print event header */
230 if (likely(stream
->stream_event_header
)) {
231 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
236 /* print stream-declared event context */
237 if (stream
->stream_event_context
) {
238 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
243 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
244 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
247 event
= g_ptr_array_index(stream
->events_by_id
, id
);
248 if (unlikely(!event
)) {
249 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
253 /* print event-declared event context */
254 if (event
->event_context
) {
255 ret
= generic_rw(pos
, &event
->event_context
->p
);
260 /* Read and print event payload */
261 if (likely(event
->event_fields
)) {
262 ret
= generic_rw(pos
, &event
->event_fields
->p
);
270 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
274 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
277 pos
->mmap_offset
= 0;
278 pos
->packet_size
= 0;
279 pos
->content_size
= 0;
280 pos
->content_size_loc
= NULL
;
286 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
287 sizeof(struct packet_index
));
289 pos
->packet_index
= NULL
;
290 switch (open_flags
& O_ACCMODE
) {
292 pos
->prot
= PROT_READ
;
293 pos
->flags
= MAP_PRIVATE
;
294 pos
->parent
.rw_table
= read_dispatch_table
;
295 pos
->parent
.event_cb
= ctf_read_event
;
298 pos
->prot
= PROT_WRITE
; /* Write has priority */
299 pos
->flags
= MAP_SHARED
;
300 pos
->parent
.rw_table
= write_dispatch_table
;
301 pos
->parent
.event_cb
= ctf_write_event
;
303 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
310 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
314 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
315 *pos
->content_size_loc
= pos
->offset
;
318 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
320 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
325 (void) g_array_free(pos
->packet_index
, TRUE
);
328 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
330 struct ctf_file_stream
*file_stream
=
331 container_of(pos
, struct ctf_file_stream
, pos
);
334 struct packet_index
*index
;
336 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
337 *pos
->content_size_loc
= pos
->offset
;
341 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
343 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
351 * The caller should never ask for ctf_move_pos across packets,
352 * except to get exactly at the beginning of the next packet.
354 if (pos
->prot
== PROT_WRITE
) {
357 /* The writer will add padding */
358 assert(pos
->offset
+ offset
== pos
->packet_size
);
359 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
362 assert(offset
== 0); /* only seek supported for now */
368 pos
->content_size
= -1U; /* Unknown at this point */
369 pos
->packet_size
= WRITE_PACKET_LEN
;
370 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
371 pos
->packet_size
/ CHAR_BIT
);
378 if (pos
->offset
== EOF
)
380 /* The reader will expect us to skip padding */
381 assert(pos
->offset
+ offset
== pos
->content_size
);
385 assert(offset
== 0); /* only seek supported for now */
391 if (pos
->cur_index
>= pos
->packet_index
->len
) {
395 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
397 pos
->mmap_offset
= index
->offset
;
399 /* Lookup context/packet size in index */
400 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
401 pos
->content_size
= index
->content_size
;
402 pos
->packet_size
= index
->packet_size
;
403 if (index
->data_offset
< index
->content_size
) {
404 pos
->offset
= 0; /* will read headers */
405 } else if (index
->data_offset
== index
->content_size
) {
407 pos
->offset
= index
->data_offset
;
409 goto read_next_packet
;
415 /* map new base. Need mapping length from header. */
416 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
417 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
418 if (pos
->base
== MAP_FAILED
) {
419 fprintf(stdout
, "[error] mmap error %s.\n",
424 /* update trace_packet_header and stream_packet_context */
425 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
426 /* Read packet header */
427 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
430 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
431 /* Read packet context */
432 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
438 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
444 len
= fread(&magic
, sizeof(magic
), 1, fp
);
448 if (magic
== TSDL_MAGIC
) {
450 td
->byte_order
= BYTE_ORDER
;
451 CTF_TRACE_SET_FIELD(td
, byte_order
);
452 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
454 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
455 LITTLE_ENDIAN
: BIG_ENDIAN
;
456 CTF_TRACE_SET_FIELD(td
, byte_order
);
464 * Returns 0 on success, -1 on error.
467 int check_version(unsigned int major
, unsigned int minor
)
482 /* eventually return an error instead of warning */
484 fprintf(stdout
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
490 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
493 struct metadata_packet_header header
;
494 size_t readlen
, writelen
, toread
;
495 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
498 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
502 if (td
->byte_order
!= BYTE_ORDER
) {
503 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
504 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
505 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
506 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
509 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
510 if (header
.compression_scheme
) {
511 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
512 header
.compression_scheme
);
515 if (header
.encryption_scheme
) {
516 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
517 header
.encryption_scheme
);
520 if (header
.checksum_scheme
) {
521 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
522 header
.checksum_scheme
);
525 if (check_version(header
.major
, header
.minor
) < 0)
527 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
528 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
529 CTF_TRACE_SET_FIELD(td
, uuid
);
531 if (uuid_compare(header
.uuid
, td
->uuid
))
535 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
538 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
543 if (babeltrace_debug
) {
545 fprintf(stdout
, "[debug] metadata packet read: %s\n",
549 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
550 if (writelen
< readlen
) {
560 ret
= 0; /* continue reading next packet */
567 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
568 ret
= fseek(in
, toread
, SEEK_CUR
);
570 fprintf(stdout
, "[warning] Missing padding at end of file\n");
577 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
586 * Using strlen on *buf instead of size of open_memstream
587 * because its size includes garbage at the end (after final
588 * \0). This is the allocated size, not the actual string size.
590 out
= open_memstream(buf
, &size
);
595 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
604 fclose(out
); /* flush the buffer */
606 /* open for reading */
607 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
612 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
614 struct ctf_scanner
*scanner
;
615 struct ctf_file_stream
*metadata_stream
;
620 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
621 td
->metadata
= &metadata_stream
->parent
;
622 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
623 if (metadata_stream
->pos
.fd
< 0) {
624 fprintf(stdout
, "Unable to open metadata.\n");
625 return metadata_stream
->pos
.fd
;
628 if (babeltrace_debug
)
631 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
633 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
638 if (packet_metadata(td
, fp
)) {
639 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
641 goto end_packet_read
;
643 unsigned int major
, minor
;
646 td
->byte_order
= BYTE_ORDER
;
648 /* Check text-only metadata header and version */
649 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
651 fprintf(stdout
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
652 if (check_version(major
, minor
) < 0) {
654 goto end_packet_read
;
659 scanner
= ctf_scanner_alloc(fp
);
661 fprintf(stdout
, "[error] Error allocating scanner\n");
663 goto end_scanner_alloc
;
665 ret
= ctf_scanner_append_ast(scanner
);
667 fprintf(stdout
, "[error] Error creating AST\n");
671 if (babeltrace_debug
) {
672 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
674 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
679 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
681 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
684 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
687 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
691 ctf_scanner_free(scanner
);
697 close(metadata_stream
->pos
.fd
);
699 g_free(metadata_stream
);
704 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
705 struct ctf_stream
*stream
,
706 struct ctf_event
*event
)
708 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
710 if (event
->context_decl
) {
711 struct definition
*definition
=
712 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
713 stream
->parent_def_scope
, 0, 0, "event.context");
717 stream_event
->event_context
= container_of(definition
,
718 struct definition_struct
, p
);
719 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
721 if (event
->fields_decl
) {
722 struct definition
*definition
=
723 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
724 stream
->parent_def_scope
, 0, 0, "event.fields");
728 stream_event
->event_fields
= container_of(definition
,
729 struct definition_struct
, p
);
730 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
735 if (stream_event
->event_fields
)
736 definition_unref(&stream_event
->event_fields
->p
);
737 if (stream_event
->event_context
)
738 definition_unref(&stream_event
->event_context
->p
);
743 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
745 struct ctf_stream_class
*stream_class
;
749 if (stream
->stream_definitions_created
)
752 stream_class
= stream
->stream_class
;
754 if (stream_class
->packet_context_decl
) {
755 struct definition
*definition
=
756 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
757 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
762 stream
->stream_packet_context
= container_of(definition
,
763 struct definition_struct
, p
);
764 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
766 if (stream_class
->event_header_decl
) {
767 struct definition
*definition
=
768 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
769 stream
->parent_def_scope
, 0, 0, "stream.event.header");
774 stream
->stream_event_header
=
775 container_of(definition
, struct definition_struct
, p
);
776 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
778 if (stream_class
->event_context_decl
) {
779 struct definition
*definition
=
780 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
781 stream
->parent_def_scope
, 0, 0, "stream.event.context");
786 stream
->stream_event_context
=
787 container_of(definition
, struct definition_struct
, p
);
788 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
790 stream
->events_by_id
= g_ptr_array_new();
791 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
792 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
793 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
794 struct ctf_stream_event
*stream_event
;
798 stream_event
= create_event_definitions(td
, stream
, event
);
801 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
806 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
807 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
809 g_free(stream_event
);
811 g_ptr_array_free(stream
->events_by_id
, TRUE
);
813 if (stream
->stream_event_context
)
814 definition_unref(&stream
->stream_event_context
->p
);
815 if (stream
->stream_event_header
)
816 definition_unref(&stream
->stream_event_header
->p
);
817 if (stream
->stream_packet_context
)
818 definition_unref(&stream
->stream_packet_context
->p
);
824 int create_stream_packet_index(struct ctf_trace
*td
,
825 struct ctf_file_stream
*file_stream
)
827 struct ctf_stream_class
*stream
;
829 struct ctf_stream_pos
*pos
;
830 struct stat filestats
;
831 struct packet_index packet_index
;
832 int first_packet
= 1;
835 pos
= &file_stream
->pos
;
837 ret
= fstat(pos
->fd
, &filestats
);
841 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
844 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
845 uint64_t stream_id
= 0;
849 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
851 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
857 /* map new base. Need mapping length from header. */
858 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
859 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
860 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
861 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
862 pos
->offset
= 0; /* Position of the packet header */
864 packet_index
.offset
= pos
->mmap_offset
;
865 packet_index
.content_size
= 0;
866 packet_index
.packet_size
= 0;
867 packet_index
.timestamp_begin
= 0;
868 packet_index
.timestamp_end
= 0;
870 /* read and check header, set stream id (and check) */
871 if (file_stream
->parent
.trace_packet_header
) {
872 /* Read packet header */
873 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
876 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
877 if (len_index
>= 0) {
878 struct definition_integer
*defint
;
879 struct definition
*field
;
881 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
882 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
883 defint
= container_of(field
, struct definition_integer
, p
);
884 assert(defint
->declaration
->signedness
== FALSE
);
885 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
886 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
887 defint
->value
._unsigned
,
888 file_stream
->pos
.packet_index
->len
,
889 (ssize_t
) pos
->mmap_offset
);
895 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
896 if (len_index
>= 0) {
897 struct definition_array
*defarray
;
898 struct definition
*field
;
900 uint8_t uuidval
[UUID_LEN
];
902 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
903 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
904 defarray
= container_of(field
, struct definition_array
, p
);
905 assert(array_len(defarray
) == UUID_LEN
);
906 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
908 for (i
= 0; i
< UUID_LEN
; i
++) {
909 struct definition
*elem
;
910 struct definition_integer
*defint
;
912 elem
= array_index(defarray
, i
);
914 defint
= container_of(elem
, struct definition_integer
, p
);
915 uuidval
[i
] = defint
->value
._unsigned
;
917 ret
= uuid_compare(td
->uuid
, uuidval
);
919 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
925 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
926 if (len_index
>= 0) {
927 struct definition_integer
*defint
;
928 struct definition
*field
;
930 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
931 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
932 defint
= container_of(field
, struct definition_integer
, p
);
933 assert(defint
->declaration
->signedness
== FALSE
);
934 stream_id
= defint
->value
._unsigned
;
938 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
939 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
943 file_stream
->parent
.stream_id
= stream_id
;
944 if (stream_id
>= td
->streams
->len
) {
945 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
948 stream
= g_ptr_array_index(td
->streams
, stream_id
);
950 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
953 file_stream
->parent
.stream_class
= stream
;
954 ret
= create_stream_definitions(td
, &file_stream
->parent
);
960 if (file_stream
->parent
.stream_packet_context
) {
961 /* Read packet context */
962 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
965 /* read content size from header */
966 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
967 if (len_index
>= 0) {
968 struct definition_integer
*defint
;
969 struct definition
*field
;
971 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
972 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
973 defint
= container_of(field
, struct definition_integer
, p
);
974 assert(defint
->declaration
->signedness
== FALSE
);
975 packet_index
.content_size
= defint
->value
._unsigned
;
977 /* Use file size for packet size */
978 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
981 /* read packet size from header */
982 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
983 if (len_index
>= 0) {
984 struct definition_integer
*defint
;
985 struct definition
*field
;
987 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
988 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
989 defint
= container_of(field
, struct definition_integer
, p
);
990 assert(defint
->declaration
->signedness
== FALSE
);
991 packet_index
.packet_size
= defint
->value
._unsigned
;
993 /* Use content size if non-zero, else file size */
994 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
997 /* read timestamp begin from header */
998 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
999 if (len_index
>= 0) {
1000 struct definition_integer
*defint
;
1001 struct definition
*field
;
1003 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1004 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1005 defint
= container_of(field
, struct definition_integer
, p
);
1006 assert(defint
->declaration
->signedness
== FALSE
);
1007 packet_index
.timestamp_begin
= defint
->value
._unsigned
;
1010 /* read timestamp end from header */
1011 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1012 if (len_index
>= 0) {
1013 struct definition_integer
*defint
;
1014 struct definition
*field
;
1016 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1017 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1018 defint
= container_of(field
, struct definition_integer
, p
);
1019 assert(defint
->declaration
->signedness
== FALSE
);
1020 packet_index
.timestamp_end
= defint
->value
._unsigned
;
1023 /* Use file size for packet size */
1024 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1025 /* Use content size if non-zero, else file size */
1026 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1029 /* Validate content size and packet size values */
1030 if (packet_index
.content_size
> packet_index
.packet_size
) {
1031 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1032 packet_index
.content_size
, packet_index
.packet_size
);
1036 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1037 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1038 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1042 /* Save position after header and context */
1043 packet_index
.data_offset
= pos
->offset
;
1045 /* add index to packet array */
1046 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1048 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1051 /* Move pos back to beginning of file */
1052 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1058 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1062 if (td
->packet_header_decl
) {
1063 struct definition
*definition
=
1064 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1065 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1070 stream
->trace_packet_header
=
1071 container_of(definition
, struct definition_struct
, p
);
1072 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1082 * Note: many file streams can inherit from the same stream class
1083 * description (metadata).
1086 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
1089 struct ctf_file_stream
*file_stream
;
1091 ret
= openat(td
->dirfd
, path
, flags
);
1094 file_stream
= g_new0(struct ctf_file_stream
, 1);
1095 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1096 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1099 ret
= create_stream_packet_index(td
, file_stream
);
1102 /* Add stream file to stream class */
1103 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1104 &file_stream
->parent
);
1108 if (file_stream
->parent
.trace_packet_header
)
1109 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1111 ctf_fini_pos(&file_stream
->pos
);
1112 close(file_stream
->pos
.fd
);
1113 g_free(file_stream
);
1119 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
1122 struct dirent
*dirent
;
1123 struct dirent
*diriter
;
1128 /* Open trace directory */
1129 td
->dir
= opendir(path
);
1131 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1136 td
->dirfd
= open(path
, 0);
1137 if (td
->dirfd
< 0) {
1138 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1144 * Keep the metadata file separate.
1147 ret
= ctf_open_trace_metadata_read(td
);
1149 goto error_metadata
;
1153 * Open each stream: for each file, try to open, check magic
1154 * number, and get the stream ID to add to the right location in
1158 dirent_len
= offsetof(struct dirent
, d_name
) +
1159 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1161 dirent
= malloc(dirent_len
);
1164 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1166 fprintf(stdout
, "[error] Readdir error.\n");
1171 /* Ignore hidden files, ., .. and metadata. */
1172 if (!strncmp(diriter
->d_name
, ".", 1)
1173 || !strcmp(diriter
->d_name
, "..")
1174 || !strcmp(diriter
->d_name
, "metadata"))
1176 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
1178 fprintf(stdout
, "[error] Open file stream error.\n");
1197 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
1199 struct ctf_trace
*td
;
1202 td
= g_new0(struct ctf_trace
, 1);
1204 switch (flags
& O_ACCMODE
) {
1207 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1210 ret
= ctf_open_trace_read(td
, path
, flags
);
1215 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1218 fprintf(stdout
, "[error] Incorrect open flags.\n");
1229 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1231 ctf_fini_pos(&file_stream
->pos
);
1232 close(file_stream
->pos
.fd
);
1236 void ctf_close_trace(struct trace_descriptor
*tdp
)
1238 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1242 for (i
= 0; i
< td
->streams
->len
; i
++) {
1243 struct ctf_stream_class
*stream
;
1246 stream
= g_ptr_array_index(td
->streams
, i
);
1249 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1250 struct ctf_file_stream
*file_stream
;
1251 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1252 ctf_close_file_stream(file_stream
);
1256 g_ptr_array_free(td
->streams
, TRUE
);
1262 void __attribute__((constructor
)) ctf_init(void)
1266 ctf_format
.name
= g_quark_from_static_string("ctf");
1267 ret
= bt_register_format(&ctf_format
);
1271 /* TODO: finalize */