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.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 (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 if (pos
->offset
== EOF
)
131 /* Read event header */
132 if (stream
->stream_event_header
) {
133 struct definition_integer
*integer_definition
;
134 struct definition
*variant
;
136 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
139 /* lookup event id */
140 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
141 if (integer_definition
) {
142 id
= integer_definition
->value
._unsigned
;
144 struct definition_enum
*enum_definition
;
146 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
147 if (enum_definition
) {
148 id
= enum_definition
->integer
->value
._unsigned
;
152 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
154 integer_definition
= lookup_integer(variant
, "id", FALSE
);
155 if (integer_definition
) {
156 id
= integer_definition
->value
._unsigned
;
160 /* lookup timestamp */
161 stream
->has_timestamp
= 0;
162 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
163 if (integer_definition
) {
164 ctf_update_timestamp(stream
, integer_definition
);
165 stream
->has_timestamp
= 1;
168 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
169 if (integer_definition
) {
170 ctf_update_timestamp(stream
, integer_definition
);
171 stream
->has_timestamp
= 1;
177 /* Read stream-declared event context */
178 if (stream
->stream_event_context
) {
179 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
184 if (id
>= stream_class
->events_by_id
->len
) {
185 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
188 event
= g_ptr_array_index(stream
->events_by_id
, id
);
190 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
194 /* Read event-declared event context */
195 if (event
->event_context
) {
196 ret
= generic_rw(ppos
, &event
->event_context
->p
);
201 /* Read event payload */
202 if (event
->event_fields
) {
203 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
211 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
216 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
218 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
219 struct ctf_stream_event
*event
;
223 /* print event header */
224 if (stream
->stream_event_header
) {
225 struct definition_integer
*integer_definition
;
226 struct definition
*variant
;
228 /* lookup event id */
229 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
230 if (integer_definition
) {
231 id
= integer_definition
->value
._unsigned
;
233 struct definition_enum
*enum_definition
;
235 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
236 if (enum_definition
) {
237 id
= enum_definition
->integer
->value
._unsigned
;
241 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
243 integer_definition
= lookup_integer(variant
, "id", FALSE
);
244 if (integer_definition
) {
245 id
= integer_definition
->value
._unsigned
;
249 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
254 /* print stream-declared event context */
255 if (stream
->stream_event_context
) {
256 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
261 if (id
>= stream_class
->events_by_id
->len
) {
262 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
265 event
= g_ptr_array_index(stream
->events_by_id
, id
);
267 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
271 /* print event-declared event context */
272 if (event
->event_context
) {
273 ret
= generic_rw(pos
, &event
->event_context
->p
);
278 /* Read and print event payload */
279 if (event
->event_fields
) {
280 ret
= generic_rw(pos
, &event
->event_fields
->p
);
288 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
292 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
295 pos
->mmap_offset
= 0;
296 pos
->packet_size
= 0;
297 pos
->content_size
= 0;
298 pos
->content_size_loc
= NULL
;
304 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
305 sizeof(struct packet_index
));
307 pos
->packet_index
= NULL
;
308 switch (open_flags
& O_ACCMODE
) {
310 pos
->prot
= PROT_READ
;
311 pos
->flags
= MAP_PRIVATE
;
312 pos
->parent
.rw_table
= read_dispatch_table
;
313 pos
->parent
.event_cb
= ctf_read_event
;
316 pos
->prot
= PROT_WRITE
; /* Write has priority */
317 pos
->flags
= MAP_SHARED
;
318 pos
->parent
.rw_table
= write_dispatch_table
;
319 pos
->parent
.event_cb
= ctf_write_event
;
321 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
328 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
332 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
333 *pos
->content_size_loc
= pos
->offset
;
336 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
338 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
343 (void) g_array_free(pos
->packet_index
, TRUE
);
346 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
348 struct ctf_file_stream
*file_stream
=
349 container_of(pos
, struct ctf_file_stream
, pos
);
352 struct packet_index
*index
;
354 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
355 *pos
->content_size_loc
= pos
->offset
;
359 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
361 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
369 * The caller should never ask for ctf_move_pos across packets,
370 * except to get exactly at the beginning of the next packet.
372 if (pos
->prot
== PROT_WRITE
) {
375 /* The writer will add padding */
376 assert(pos
->offset
+ offset
== pos
->packet_size
);
377 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
380 assert(offset
== 0); /* only seek supported for now */
386 pos
->content_size
= -1U; /* Unknown at this point */
387 pos
->packet_size
= WRITE_PACKET_LEN
;
388 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
389 pos
->packet_size
/ CHAR_BIT
);
395 /* The reader will expect us to skip padding */
396 assert(pos
->offset
+ offset
== pos
->content_size
);
400 assert(offset
== 0); /* only seek supported for now */
406 if (pos
->cur_index
>= pos
->packet_index
->len
) {
410 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
412 pos
->mmap_offset
= index
->offset
;
414 /* Lookup context/packet size in index */
415 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
416 pos
->content_size
= index
->content_size
;
417 pos
->packet_size
= index
->packet_size
;
418 if (index
->data_offset
<= index
->content_size
) {
419 pos
->offset
= 0; /* will read headers */
425 /* map new base. Need mapping length from header. */
426 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
427 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
428 if (pos
->base
== MAP_FAILED
) {
429 fprintf(stdout
, "[error] mmap error %s.\n",
434 /* update trace_packet_header and stream_packet_context */
435 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
436 /* Read packet header */
437 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
440 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
441 /* Read packet context */
442 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
448 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
454 len
= fread(&magic
, sizeof(magic
), 1, fp
);
458 if (magic
== TSDL_MAGIC
) {
460 td
->byte_order
= BYTE_ORDER
;
461 CTF_TRACE_SET_FIELD(td
, byte_order
);
462 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
464 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
465 LITTLE_ENDIAN
: BIG_ENDIAN
;
466 CTF_TRACE_SET_FIELD(td
, byte_order
);
474 * Returns 0 on success, -1 on error.
477 int check_version(unsigned int major
, unsigned int minor
)
492 /* eventually return an error instead of warning */
494 fprintf(stdout
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
500 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
503 struct metadata_packet_header header
;
504 size_t readlen
, writelen
, toread
;
505 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
508 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
512 if (td
->byte_order
!= BYTE_ORDER
) {
513 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
514 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
515 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
516 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
519 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
520 if (header
.compression_scheme
) {
521 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
522 header
.compression_scheme
);
525 if (header
.encryption_scheme
) {
526 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
527 header
.encryption_scheme
);
530 if (header
.checksum_scheme
) {
531 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
532 header
.checksum_scheme
);
535 if (check_version(header
.major
, header
.minor
) < 0)
537 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
538 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
539 CTF_TRACE_SET_FIELD(td
, uuid
);
541 if (uuid_compare(header
.uuid
, td
->uuid
))
545 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
548 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
553 if (babeltrace_debug
) {
555 fprintf(stdout
, "[debug] metadata packet read: %s\n",
559 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
560 if (writelen
< readlen
) {
570 ret
= 0; /* continue reading next packet */
577 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
578 ret
= fseek(in
, toread
, SEEK_CUR
);
580 fprintf(stdout
, "[warning] Missing padding at end of file\n");
587 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
596 * Using strlen on *buf instead of size of open_memstream
597 * because its size includes garbage at the end (after final
598 * \0). This is the allocated size, not the actual string size.
600 out
= open_memstream(buf
, &size
);
605 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
614 fclose(out
); /* flush the buffer */
616 /* open for reading */
617 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
622 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
624 struct ctf_scanner
*scanner
;
625 struct ctf_file_stream
*metadata_stream
;
630 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
631 td
->metadata
= &metadata_stream
->parent
;
632 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
633 if (metadata_stream
->pos
.fd
< 0) {
634 fprintf(stdout
, "Unable to open metadata.\n");
635 return metadata_stream
->pos
.fd
;
638 if (babeltrace_debug
)
641 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
643 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
648 if (packet_metadata(td
, fp
)) {
649 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
651 goto end_packet_read
;
653 unsigned int major
, minor
;
656 td
->byte_order
= BYTE_ORDER
;
658 /* Check text-only metadata header and version */
659 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
661 fprintf(stdout
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
662 if (check_version(major
, minor
) < 0) {
664 goto end_packet_read
;
669 scanner
= ctf_scanner_alloc(fp
);
671 fprintf(stdout
, "[error] Error allocating scanner\n");
673 goto end_scanner_alloc
;
675 ret
= ctf_scanner_append_ast(scanner
);
677 fprintf(stdout
, "[error] Error creating AST\n");
681 if (babeltrace_debug
) {
682 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
684 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
689 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
691 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
694 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
697 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
701 ctf_scanner_free(scanner
);
707 close(metadata_stream
->pos
.fd
);
709 g_free(metadata_stream
);
714 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
715 struct ctf_stream
*stream
,
716 struct ctf_event
*event
)
718 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
720 if (event
->context_decl
) {
721 struct definition
*definition
=
722 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
723 stream
->parent_def_scope
, 0, 0, "event.context");
727 stream_event
->event_context
= container_of(definition
,
728 struct definition_struct
, p
);
729 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
731 if (event
->fields_decl
) {
732 struct definition
*definition
=
733 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
734 stream
->parent_def_scope
, 0, 0, "event.fields");
738 stream_event
->event_fields
= container_of(definition
,
739 struct definition_struct
, p
);
740 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
745 if (stream_event
->event_fields
)
746 definition_unref(&stream_event
->event_fields
->p
);
747 if (stream_event
->event_context
)
748 definition_unref(&stream_event
->event_context
->p
);
753 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
755 struct ctf_stream_class
*stream_class
;
759 if (stream
->stream_definitions_created
)
762 stream_class
= stream
->stream_class
;
764 if (stream_class
->packet_context_decl
) {
765 struct definition
*definition
=
766 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
767 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
772 stream
->stream_packet_context
= container_of(definition
,
773 struct definition_struct
, p
);
774 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
776 if (stream_class
->event_header_decl
) {
777 struct definition
*definition
=
778 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
779 stream
->parent_def_scope
, 0, 0, "stream.event.header");
784 stream
->stream_event_header
=
785 container_of(definition
, struct definition_struct
, p
);
786 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
788 if (stream_class
->event_context_decl
) {
789 struct definition
*definition
=
790 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
791 stream
->parent_def_scope
, 0, 0, "stream.event.context");
796 stream
->stream_event_context
=
797 container_of(definition
, struct definition_struct
, p
);
798 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
800 stream
->events_by_id
= g_ptr_array_new();
801 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
802 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
803 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
804 struct ctf_stream_event
*stream_event
;
808 stream_event
= create_event_definitions(td
, stream
, event
);
811 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
816 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
817 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
819 g_free(stream_event
);
821 g_ptr_array_free(stream
->events_by_id
, TRUE
);
823 if (stream
->stream_event_context
)
824 definition_unref(&stream
->stream_event_context
->p
);
825 if (stream
->stream_event_header
)
826 definition_unref(&stream
->stream_event_header
->p
);
827 if (stream
->stream_packet_context
)
828 definition_unref(&stream
->stream_packet_context
->p
);
834 int create_stream_packet_index(struct ctf_trace
*td
,
835 struct ctf_file_stream
*file_stream
)
837 struct ctf_stream_class
*stream
;
839 struct ctf_stream_pos
*pos
;
840 struct stat filestats
;
841 struct packet_index packet_index
;
842 int first_packet
= 1;
845 pos
= &file_stream
->pos
;
847 ret
= fstat(pos
->fd
, &filestats
);
851 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
854 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
855 uint64_t stream_id
= 0;
859 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
861 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
867 /* map new base. Need mapping length from header. */
868 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
869 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
870 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
871 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
872 pos
->offset
= 0; /* Position of the packet header */
874 packet_index
.offset
= pos
->mmap_offset
;
875 packet_index
.content_size
= 0;
876 packet_index
.packet_size
= 0;
877 packet_index
.timestamp_begin
= 0;
878 packet_index
.timestamp_end
= 0;
880 /* read and check header, set stream id (and check) */
881 if (file_stream
->parent
.trace_packet_header
) {
882 /* Read packet header */
883 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
886 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
887 if (len_index
>= 0) {
888 struct definition_integer
*defint
;
889 struct definition
*field
;
891 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
892 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
893 defint
= container_of(field
, struct definition_integer
, p
);
894 assert(defint
->declaration
->signedness
== FALSE
);
895 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
896 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
897 defint
->value
._unsigned
,
898 file_stream
->pos
.packet_index
->len
,
899 (ssize_t
) pos
->mmap_offset
);
905 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
906 if (len_index
>= 0) {
907 struct definition_array
*defarray
;
908 struct definition
*field
;
910 uint8_t uuidval
[UUID_LEN
];
912 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
913 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
914 defarray
= container_of(field
, struct definition_array
, p
);
915 assert(array_len(defarray
) == UUID_LEN
);
916 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
918 for (i
= 0; i
< UUID_LEN
; i
++) {
919 struct definition
*elem
;
920 struct definition_integer
*defint
;
922 elem
= array_index(defarray
, i
);
924 defint
= container_of(elem
, struct definition_integer
, p
);
925 uuidval
[i
] = defint
->value
._unsigned
;
927 ret
= uuid_compare(td
->uuid
, uuidval
);
929 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
935 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
936 if (len_index
>= 0) {
937 struct definition_integer
*defint
;
938 struct definition
*field
;
940 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
941 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
942 defint
= container_of(field
, struct definition_integer
, p
);
943 assert(defint
->declaration
->signedness
== FALSE
);
944 stream_id
= defint
->value
._unsigned
;
948 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
949 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
953 file_stream
->parent
.stream_id
= stream_id
;
954 if (stream_id
>= td
->streams
->len
) {
955 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
958 stream
= g_ptr_array_index(td
->streams
, stream_id
);
960 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
963 file_stream
->parent
.stream_class
= stream
;
964 ret
= create_stream_definitions(td
, &file_stream
->parent
);
970 if (file_stream
->parent
.stream_packet_context
) {
971 /* Read packet context */
972 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
975 /* read content size from header */
976 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
977 if (len_index
>= 0) {
978 struct definition_integer
*defint
;
979 struct definition
*field
;
981 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
982 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
983 defint
= container_of(field
, struct definition_integer
, p
);
984 assert(defint
->declaration
->signedness
== FALSE
);
985 packet_index
.content_size
= defint
->value
._unsigned
;
987 /* Use file size for packet size */
988 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
991 /* read packet size from header */
992 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
993 if (len_index
>= 0) {
994 struct definition_integer
*defint
;
995 struct definition
*field
;
997 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
998 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
999 defint
= container_of(field
, struct definition_integer
, p
);
1000 assert(defint
->declaration
->signedness
== FALSE
);
1001 packet_index
.packet_size
= defint
->value
._unsigned
;
1003 /* Use content size if non-zero, else file size */
1004 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1007 /* read timestamp begin from header */
1008 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1009 if (len_index
>= 0) {
1010 struct definition_integer
*defint
;
1011 struct definition
*field
;
1013 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1014 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1015 defint
= container_of(field
, struct definition_integer
, p
);
1016 assert(defint
->declaration
->signedness
== FALSE
);
1017 packet_index
.timestamp_begin
= defint
->value
._unsigned
;
1020 /* read timestamp end from header */
1021 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1022 if (len_index
>= 0) {
1023 struct definition_integer
*defint
;
1024 struct definition
*field
;
1026 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1027 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1028 defint
= container_of(field
, struct definition_integer
, p
);
1029 assert(defint
->declaration
->signedness
== FALSE
);
1030 packet_index
.timestamp_end
= defint
->value
._unsigned
;
1033 /* Use file size for packet size */
1034 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1035 /* Use content size if non-zero, else file size */
1036 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1039 /* Validate content size and packet size values */
1040 if (packet_index
.content_size
> packet_index
.packet_size
) {
1041 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1042 packet_index
.content_size
, packet_index
.packet_size
);
1046 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1047 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1048 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1052 /* Save position after header and context */
1053 packet_index
.data_offset
= pos
->offset
;
1055 /* add index to packet array */
1056 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1058 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1061 /* Move pos back to beginning of file */
1062 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1068 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1072 if (td
->packet_header_decl
) {
1073 struct definition
*definition
=
1074 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1075 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1080 stream
->trace_packet_header
=
1081 container_of(definition
, struct definition_struct
, p
);
1082 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1092 * Note: many file streams can inherit from the same stream class
1093 * description (metadata).
1096 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
1099 struct ctf_file_stream
*file_stream
;
1101 ret
= openat(td
->dirfd
, path
, flags
);
1104 file_stream
= g_new0(struct ctf_file_stream
, 1);
1105 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1106 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1109 ret
= create_stream_packet_index(td
, file_stream
);
1112 /* Add stream file to stream class */
1113 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1114 &file_stream
->parent
);
1118 if (file_stream
->parent
.trace_packet_header
)
1119 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1121 ctf_fini_pos(&file_stream
->pos
);
1122 close(file_stream
->pos
.fd
);
1123 g_free(file_stream
);
1129 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
1132 struct dirent
*dirent
;
1133 struct dirent
*diriter
;
1138 /* Open trace directory */
1139 td
->dir
= opendir(path
);
1141 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1146 td
->dirfd
= open(path
, 0);
1147 if (td
->dirfd
< 0) {
1148 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1154 * Keep the metadata file separate.
1157 ret
= ctf_open_trace_metadata_read(td
);
1159 goto error_metadata
;
1163 * Open each stream: for each file, try to open, check magic
1164 * number, and get the stream ID to add to the right location in
1168 dirent_len
= offsetof(struct dirent
, d_name
) +
1169 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1171 dirent
= malloc(dirent_len
);
1174 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1176 fprintf(stdout
, "[error] Readdir error.\n");
1181 /* Ignore hidden files, ., .. and metadata. */
1182 if (!strncmp(diriter
->d_name
, ".", 1)
1183 || !strcmp(diriter
->d_name
, "..")
1184 || !strcmp(diriter
->d_name
, "metadata"))
1186 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
1188 fprintf(stdout
, "[error] Open file stream error.\n");
1207 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
1209 struct ctf_trace
*td
;
1212 td
= g_new0(struct ctf_trace
, 1);
1214 switch (flags
& O_ACCMODE
) {
1217 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1220 ret
= ctf_open_trace_read(td
, path
, flags
);
1225 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1228 fprintf(stdout
, "[error] Incorrect open flags.\n");
1239 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1241 ctf_fini_pos(&file_stream
->pos
);
1242 close(file_stream
->pos
.fd
);
1246 void ctf_close_trace(struct trace_descriptor
*tdp
)
1248 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1252 for (i
= 0; i
< td
->streams
->len
; i
++) {
1253 struct ctf_stream_class
*stream
;
1256 stream
= g_ptr_array_index(td
->streams
, i
);
1259 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1260 struct ctf_file_stream
*file_stream
;
1261 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1262 ctf_close_file_stream(file_stream
);
1266 g_ptr_array_free(td
->streams
, TRUE
);
1272 void __attribute__((constructor
)) ctf_init(void)
1276 ctf_format
.name
= g_quark_from_static_string("ctf");
1277 ret
= bt_register_format(&ctf_format
);
1281 /* TODO: finalize */