2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
25 #include <uuid/uuid.h>
29 #include <sys/types.h>
37 #include "metadata/ctf-scanner.h"
38 #include "metadata/ctf-parser.h"
39 #include "metadata/ctf-ast.h"
42 * We currently simply map a page to read the packet header and packet
43 * context to get the packet length and content length. (in bits)
45 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
46 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
47 #define UUID_LEN 16 /* uuid by value len */
50 #define min(a, b) (((a) < (b)) ? (a) : (b))
56 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
58 void ctf_close_trace(struct trace_descriptor
*descriptor
);
61 rw_dispatch read_dispatch_table
[] = {
62 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
63 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
64 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
65 [ CTF_TYPE_STRING
] = ctf_string_read
,
66 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
67 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
68 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
69 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
73 rw_dispatch write_dispatch_table
[] = {
74 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
75 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
76 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
77 [ CTF_TYPE_STRING
] = ctf_string_write
,
78 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
79 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
80 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
81 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
85 struct format ctf_format
= {
86 .open_trace
= ctf_open_trace
,
87 .close_trace
= ctf_close_trace
,
91 void ctf_update_timestamp(struct ctf_stream
*stream
,
92 struct definition_integer
*integer_definition
)
94 struct declaration_integer
*integer_declaration
=
95 integer_definition
->declaration
;
96 uint64_t oldval
, newval
, updateval
;
98 if (integer_declaration
->len
== 64) {
99 stream
->timestamp
= integer_definition
->value
._unsigned
;
103 oldval
= stream
->timestamp
;
104 oldval
&= (1ULL << integer_declaration
->len
) - 1;
105 newval
= integer_definition
->value
._unsigned
;
106 /* Test for overflow by comparing low bits */
108 newval
+= 1ULL << integer_declaration
->len
;
109 /* updateval contains old high bits, and new low bits (sum) */
110 updateval
= stream
->timestamp
;
111 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
113 stream
->timestamp
= updateval
;
117 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
119 struct ctf_stream_pos
*pos
=
120 container_of(ppos
, struct ctf_stream_pos
, parent
);
121 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
122 struct ctf_stream_event
*event
;
126 if (pos
->offset
== EOF
)
129 /* Read event header */
130 if (stream
->stream_event_header
) {
131 struct definition_integer
*integer_definition
;
132 struct definition
*variant
;
134 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
137 /* lookup event id */
138 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
139 if (integer_definition
) {
140 id
= integer_definition
->value
._unsigned
;
142 struct definition_enum
*enum_definition
;
144 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
145 if (enum_definition
) {
146 id
= enum_definition
->integer
->value
._unsigned
;
150 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
152 integer_definition
= lookup_integer(variant
, "id", FALSE
);
153 if (integer_definition
) {
154 id
= integer_definition
->value
._unsigned
;
158 /* lookup timestamp */
159 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
160 if (integer_definition
) {
161 ctf_update_timestamp(stream
, integer_definition
);
164 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
165 if (integer_definition
) {
166 ctf_update_timestamp(stream
, integer_definition
);
172 /* Read stream-declared event context */
173 if (stream
->stream_event_context
) {
174 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
179 if (id
>= stream_class
->events_by_id
->len
) {
180 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
183 event
= g_ptr_array_index(stream
->events_by_id
, id
);
185 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
189 /* Read event-declared event context */
190 if (event
->event_context
) {
191 ret
= generic_rw(ppos
, &event
->event_context
->p
);
196 /* Read event payload */
197 if (event
->event_fields
) {
198 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
206 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
211 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
213 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
214 struct ctf_stream_event
*event
;
218 /* print event header */
219 if (stream
->stream_event_header
) {
220 struct definition_integer
*integer_definition
;
221 struct definition
*variant
;
223 /* lookup event id */
224 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
225 if (integer_definition
) {
226 id
= integer_definition
->value
._unsigned
;
228 struct definition_enum
*enum_definition
;
230 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
231 if (enum_definition
) {
232 id
= enum_definition
->integer
->value
._unsigned
;
236 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
238 integer_definition
= lookup_integer(variant
, "id", FALSE
);
239 if (integer_definition
) {
240 id
= integer_definition
->value
._unsigned
;
244 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
249 /* print stream-declared event context */
250 if (stream
->stream_event_context
) {
251 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
256 if (id
>= stream_class
->events_by_id
->len
) {
257 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
260 event
= g_ptr_array_index(stream
->events_by_id
, id
);
262 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
266 /* print event-declared event context */
267 if (event
->event_context
) {
268 ret
= generic_rw(pos
, &event
->event_context
->p
);
273 /* Read and print event payload */
274 if (event
->event_fields
) {
275 ret
= generic_rw(pos
, &event
->event_fields
->p
);
283 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
287 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
290 pos
->mmap_offset
= 0;
291 pos
->packet_size
= 0;
292 pos
->content_size
= 0;
293 pos
->content_size_loc
= NULL
;
299 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
300 sizeof(struct packet_index
));
302 pos
->packet_index
= NULL
;
303 switch (open_flags
& O_ACCMODE
) {
305 pos
->prot
= PROT_READ
;
306 pos
->flags
= MAP_PRIVATE
;
307 pos
->parent
.rw_table
= read_dispatch_table
;
308 pos
->parent
.event_cb
= ctf_read_event
;
311 pos
->prot
= PROT_WRITE
; /* Write has priority */
312 pos
->flags
= MAP_SHARED
;
313 pos
->parent
.rw_table
= write_dispatch_table
;
314 pos
->parent
.event_cb
= ctf_write_event
;
316 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
323 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
327 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
328 *pos
->content_size_loc
= pos
->offset
;
331 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
333 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
338 (void) g_array_free(pos
->packet_index
, TRUE
);
341 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
343 struct ctf_file_stream
*file_stream
=
344 container_of(pos
, struct ctf_file_stream
, pos
);
347 struct packet_index
*index
;
349 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
350 *pos
->content_size_loc
= pos
->offset
;
354 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
356 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
364 * The caller should never ask for ctf_move_pos across packets,
365 * except to get exactly at the beginning of the next packet.
367 if (pos
->prot
== PROT_WRITE
) {
370 /* The writer will add padding */
371 assert(pos
->offset
+ offset
== pos
->packet_size
);
372 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
375 assert(offset
== 0); /* only seek supported for now */
381 pos
->content_size
= -1U; /* Unknown at this point */
382 pos
->packet_size
= WRITE_PACKET_LEN
;
383 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
384 pos
->packet_size
/ CHAR_BIT
);
390 /* The reader will expect us to skip padding */
391 assert(pos
->offset
+ offset
== pos
->content_size
);
395 assert(offset
== 0); /* only seek supported for now */
401 if (pos
->cur_index
>= pos
->packet_index
->len
) {
405 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
407 pos
->mmap_offset
= index
->offset
;
409 /* Lookup context/packet size in index */
410 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
411 pos
->content_size
= index
->content_size
;
412 pos
->packet_size
= index
->packet_size
;
413 if (index
->data_offset
< index
->content_size
)
414 pos
->offset
= 0; /* will read headers */
420 /* map new base. Need mapping length from header. */
421 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
422 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
423 if (pos
->base
== MAP_FAILED
) {
424 fprintf(stdout
, "[error] mmap error %s.\n",
429 /* update trace_packet_header and stream_packet_context */
430 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
431 /* Read packet header */
432 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
435 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
436 /* Read packet context */
437 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
443 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
449 len
= fread(&magic
, sizeof(magic
), 1, fp
);
453 if (magic
== TSDL_MAGIC
) {
455 td
->byte_order
= BYTE_ORDER
;
456 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
458 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
459 LITTLE_ENDIAN
: BIG_ENDIAN
;
461 CTF_TRACE_SET_FIELD(td
, byte_order
);
468 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
471 struct metadata_packet_header header
;
472 size_t readlen
, writelen
, toread
;
476 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
480 if (td
->byte_order
!= BYTE_ORDER
) {
481 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
482 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
483 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
484 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
487 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
488 if (header
.compression_scheme
) {
489 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
490 header
.compression_scheme
);
493 if (header
.encryption_scheme
) {
494 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
495 header
.encryption_scheme
);
498 if (header
.checksum_scheme
) {
499 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
500 header
.checksum_scheme
);
503 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
504 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
505 CTF_TRACE_SET_FIELD(td
, uuid
);
507 if (uuid_compare(header
.uuid
, td
->uuid
))
511 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
514 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
519 if (babeltrace_debug
) {
520 fprintf(stdout
, "[debug] metadata packet read: %s\n",
524 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
525 if (writelen
< readlen
) {
535 ret
= 0; /* continue reading next packet */
543 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
552 * Using strlen on *buf instead of size of open_memstream
553 * because its size includes garbage at the end (after final
554 * \0). This is the allocated size, not the actual string size.
556 out
= open_memstream(buf
, &size
);
561 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
570 fclose(out
); /* flush the buffer */
572 /* open for reading */
573 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
578 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
580 struct ctf_scanner
*scanner
;
581 struct ctf_file_stream
*metadata_stream
;
586 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
587 td
->metadata
= &metadata_stream
->parent
;
588 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
589 if (metadata_stream
->pos
.fd
< 0) {
590 fprintf(stdout
, "Unable to open metadata.\n");
591 return metadata_stream
->pos
.fd
;
594 if (babeltrace_debug
)
597 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
599 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
604 if (packet_metadata(td
, fp
)) {
605 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
607 goto end_packet_read
;
610 scanner
= ctf_scanner_alloc(fp
);
612 fprintf(stdout
, "[error] Error allocating scanner\n");
614 goto end_scanner_alloc
;
616 ret
= ctf_scanner_append_ast(scanner
);
618 fprintf(stdout
, "[error] Error creating AST\n");
622 if (babeltrace_debug
) {
623 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
625 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
630 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
632 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
635 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
638 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
642 ctf_scanner_free(scanner
);
648 close(metadata_stream
->pos
.fd
);
650 g_free(metadata_stream
);
655 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
656 struct ctf_stream
*stream
,
657 struct ctf_event
*event
)
659 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
661 if (event
->context_decl
) {
662 struct definition
*definition
=
663 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
664 stream
->parent_def_scope
, 0, 0, "event.context");
668 stream_event
->event_context
= container_of(definition
,
669 struct definition_struct
, p
);
670 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
672 if (event
->fields_decl
) {
673 struct definition
*definition
=
674 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
675 stream
->parent_def_scope
, 0, 0, "event.fields");
679 stream_event
->event_fields
= container_of(definition
,
680 struct definition_struct
, p
);
681 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
686 if (stream_event
->event_fields
)
687 definition_unref(&stream_event
->event_fields
->p
);
688 if (stream_event
->event_context
)
689 definition_unref(&stream_event
->event_context
->p
);
694 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
696 struct ctf_stream_class
*stream_class
;
700 if (stream
->stream_definitions_created
)
703 stream_class
= stream
->stream_class
;
705 if (stream_class
->packet_context_decl
) {
706 struct definition
*definition
=
707 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
708 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
713 stream
->stream_packet_context
= container_of(definition
,
714 struct definition_struct
, p
);
715 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
717 if (stream_class
->event_header_decl
) {
718 struct definition
*definition
=
719 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
720 stream
->parent_def_scope
, 0, 0, "stream.event.header");
725 stream
->stream_event_header
=
726 container_of(definition
, struct definition_struct
, p
);
727 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
729 if (stream_class
->event_context_decl
) {
730 struct definition
*definition
=
731 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
732 stream
->parent_def_scope
, 0, 0, "stream.event.context");
737 stream
->stream_event_context
=
738 container_of(definition
, struct definition_struct
, p
);
739 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
741 stream
->events_by_id
= g_ptr_array_new();
742 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
743 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
744 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
745 struct ctf_stream_event
*stream_event
;
749 stream_event
= create_event_definitions(td
, stream
, event
);
752 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
757 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
758 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
760 g_free(stream_event
);
762 g_ptr_array_free(stream
->events_by_id
, TRUE
);
764 if (stream
->stream_event_context
)
765 definition_unref(&stream
->stream_event_context
->p
);
766 if (stream
->stream_event_header
)
767 definition_unref(&stream
->stream_event_header
->p
);
768 if (stream
->stream_packet_context
)
769 definition_unref(&stream
->stream_packet_context
->p
);
775 int create_stream_packet_index(struct ctf_trace
*td
,
776 struct ctf_file_stream
*file_stream
)
778 struct ctf_stream_class
*stream
;
780 struct ctf_stream_pos
*pos
;
781 struct stat filestats
;
782 struct packet_index packet_index
;
783 int first_packet
= 1;
786 pos
= &file_stream
->pos
;
788 ret
= fstat(pos
->fd
, &filestats
);
792 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
795 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
796 uint64_t stream_id
= 0;
800 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
802 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
808 /* map new base. Need mapping length from header. */
809 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
810 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
811 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
812 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
813 pos
->offset
= 0; /* Position of the packet header */
815 packet_index
.offset
= pos
->mmap_offset
;
816 packet_index
.content_size
= 0;
817 packet_index
.packet_size
= 0;
818 packet_index
.timestamp_begin
= 0;
819 packet_index
.timestamp_end
= 0;
821 /* read and check header, set stream id (and check) */
822 if (file_stream
->parent
.trace_packet_header
) {
823 /* Read packet header */
824 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
827 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
828 if (len_index
>= 0) {
829 struct definition_integer
*defint
;
830 struct definition
*field
;
832 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
833 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
834 defint
= container_of(field
, struct definition_integer
, p
);
835 assert(defint
->declaration
->signedness
== FALSE
);
836 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
837 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
838 defint
->value
._unsigned
,
839 file_stream
->pos
.packet_index
->len
,
840 (ssize_t
) pos
->mmap_offset
);
846 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
847 if (len_index
>= 0) {
848 struct definition_array
*defarray
;
849 struct definition
*field
;
851 uint8_t uuidval
[UUID_LEN
];
853 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
854 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
855 defarray
= container_of(field
, struct definition_array
, p
);
856 assert(array_len(defarray
) == UUID_LEN
);
857 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
859 for (i
= 0; i
< UUID_LEN
; i
++) {
860 struct definition
*elem
;
861 struct definition_integer
*defint
;
863 elem
= array_index(defarray
, i
);
865 defint
= container_of(elem
, struct definition_integer
, p
);
866 uuidval
[i
] = defint
->value
._unsigned
;
868 ret
= uuid_compare(td
->uuid
, uuidval
);
870 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
876 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
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 stream_id
= defint
->value
._unsigned
;
889 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
890 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
894 file_stream
->parent
.stream_id
= stream_id
;
895 if (stream_id
>= td
->streams
->len
) {
896 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
899 stream
= g_ptr_array_index(td
->streams
, stream_id
);
901 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
904 file_stream
->parent
.stream_class
= stream
;
908 ret
= create_stream_definitions(td
, &file_stream
->parent
);
912 if (file_stream
->parent
.stream_packet_context
) {
913 /* Read packet context */
914 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
917 /* read content size from header */
918 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
919 if (len_index
>= 0) {
920 struct definition_integer
*defint
;
921 struct definition
*field
;
923 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
924 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
925 defint
= container_of(field
, struct definition_integer
, p
);
926 assert(defint
->declaration
->signedness
== FALSE
);
927 packet_index
.content_size
= defint
->value
._unsigned
;
929 /* Use file size for packet size */
930 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
933 /* read packet size from header */
934 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
935 if (len_index
>= 0) {
936 struct definition_integer
*defint
;
937 struct definition
*field
;
939 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
940 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
941 defint
= container_of(field
, struct definition_integer
, p
);
942 assert(defint
->declaration
->signedness
== FALSE
);
943 packet_index
.packet_size
= defint
->value
._unsigned
;
945 /* Use content size if non-zero, else file size */
946 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
949 /* read timestamp begin from header */
950 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
951 if (len_index
>= 0) {
952 struct definition_integer
*defint
;
953 struct definition
*field
;
955 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
956 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
957 defint
= container_of(field
, struct definition_integer
, p
);
958 assert(defint
->declaration
->signedness
== FALSE
);
959 packet_index
.timestamp_begin
= defint
->value
._unsigned
;
962 /* read timestamp end from header */
963 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
964 if (len_index
>= 0) {
965 struct definition_integer
*defint
;
966 struct definition
*field
;
968 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
969 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
970 defint
= container_of(field
, struct definition_integer
, p
);
971 assert(defint
->declaration
->signedness
== FALSE
);
972 packet_index
.timestamp_end
= defint
->value
._unsigned
;
975 /* Use file size for packet size */
976 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
977 /* Use content size if non-zero, else file size */
978 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
981 /* Validate content size and packet size values */
982 if (packet_index
.content_size
> packet_index
.packet_size
) {
983 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
984 packet_index
.content_size
, packet_index
.packet_size
);
988 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
989 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
990 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
994 /* Save position after header and context */
995 packet_index
.data_offset
= pos
->offset
;
997 /* add index to packet array */
998 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1000 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1003 /* Move pos back to beginning of file */
1004 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1010 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1014 if (td
->packet_header_decl
) {
1015 struct definition
*definition
=
1016 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1017 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1022 stream
->trace_packet_header
=
1023 container_of(definition
, struct definition_struct
, p
);
1024 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1034 * Note: many file streams can inherit from the same stream class
1035 * description (metadata).
1038 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
1041 struct ctf_file_stream
*file_stream
;
1043 ret
= openat(td
->dirfd
, path
, flags
);
1046 file_stream
= g_new0(struct ctf_file_stream
, 1);
1047 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1048 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1051 ret
= create_stream_packet_index(td
, file_stream
);
1054 /* Add stream file to stream class */
1055 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1056 &file_stream
->parent
);
1060 if (file_stream
->parent
.trace_packet_header
)
1061 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1063 ctf_fini_pos(&file_stream
->pos
);
1064 close(file_stream
->pos
.fd
);
1065 g_free(file_stream
);
1071 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
1074 struct dirent
*dirent
;
1075 struct dirent
*diriter
;
1080 /* Open trace directory */
1081 td
->dir
= opendir(path
);
1083 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1088 td
->dirfd
= open(path
, 0);
1089 if (td
->dirfd
< 0) {
1090 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1096 * Keep the metadata file separate.
1099 ret
= ctf_open_trace_metadata_read(td
);
1101 goto error_metadata
;
1105 * Open each stream: for each file, try to open, check magic
1106 * number, and get the stream ID to add to the right location in
1110 dirent_len
= offsetof(struct dirent
, d_name
) +
1111 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1113 dirent
= malloc(dirent_len
);
1116 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1118 fprintf(stdout
, "[error] Readdir error.\n");
1123 /* Ignore hidden files, ., .. and metadata. */
1124 if (!strncmp(diriter
->d_name
, ".", 1)
1125 || !strcmp(diriter
->d_name
, "..")
1126 || !strcmp(diriter
->d_name
, "metadata"))
1128 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
1130 fprintf(stdout
, "[error] Open file stream error.\n");
1149 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
1151 struct ctf_trace
*td
;
1154 td
= g_new0(struct ctf_trace
, 1);
1156 switch (flags
& O_ACCMODE
) {
1159 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1162 ret
= ctf_open_trace_read(td
, path
, flags
);
1167 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1170 fprintf(stdout
, "[error] Incorrect open flags.\n");
1181 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1183 ctf_fini_pos(&file_stream
->pos
);
1184 close(file_stream
->pos
.fd
);
1188 void ctf_close_trace(struct trace_descriptor
*tdp
)
1190 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1194 for (i
= 0; i
< td
->streams
->len
; i
++) {
1195 struct ctf_stream_class
*stream
;
1198 stream
= g_ptr_array_index(td
->streams
, i
);
1201 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1202 struct ctf_file_stream
*file_stream
;
1203 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1204 ctf_close_file_stream(file_stream
);
1208 g_ptr_array_free(td
->streams
, TRUE
);
1214 void __attribute__((constructor
)) ctf_init(void)
1218 ctf_format
.name
= g_quark_from_static_string("ctf");
1219 ret
= bt_register_format(&ctf_format
);
1223 /* TODO: finalize */