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
,
59 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
62 void ctf_close_trace(struct trace_descriptor
*descriptor
);
65 rw_dispatch read_dispatch_table
[] = {
66 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
67 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
68 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
69 [ CTF_TYPE_STRING
] = ctf_string_read
,
70 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
71 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
72 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
73 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
77 rw_dispatch write_dispatch_table
[] = {
78 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
79 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
80 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
81 [ CTF_TYPE_STRING
] = ctf_string_write
,
82 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
83 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
84 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
85 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
89 struct format ctf_format
= {
90 .open_trace
= ctf_open_trace
,
91 .close_trace
= ctf_close_trace
,
95 void ctf_update_timestamp(struct ctf_stream
*stream
,
96 struct definition_integer
*integer_definition
)
98 struct declaration_integer
*integer_declaration
=
99 integer_definition
->declaration
;
100 uint64_t oldval
, newval
, updateval
;
102 if (unlikely(integer_declaration
->len
== 64)) {
103 stream
->timestamp
= integer_definition
->value
._unsigned
;
107 oldval
= stream
->timestamp
;
108 oldval
&= (1ULL << integer_declaration
->len
) - 1;
109 newval
= integer_definition
->value
._unsigned
;
110 /* Test for overflow by comparing low bits */
112 newval
+= 1ULL << integer_declaration
->len
;
113 /* updateval contains old high bits, and new low bits (sum) */
114 updateval
= stream
->timestamp
;
115 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
117 stream
->timestamp
= updateval
;
121 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
123 struct ctf_stream_pos
*pos
=
124 container_of(ppos
, struct ctf_stream_pos
, parent
);
125 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
126 struct ctf_stream_event
*event
;
130 ctf_pos_get_event(pos
);
132 if (unlikely(pos
->offset
== EOF
))
134 assert(pos
->offset
< pos
->content_size
);
136 /* Read event header */
137 if (likely(stream
->stream_event_header
)) {
138 struct definition_integer
*integer_definition
;
139 struct definition
*variant
;
141 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
144 /* lookup event id */
145 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
146 if (integer_definition
) {
147 id
= integer_definition
->value
._unsigned
;
149 struct definition_enum
*enum_definition
;
151 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
152 if (enum_definition
) {
153 id
= enum_definition
->integer
->value
._unsigned
;
157 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
159 integer_definition
= lookup_integer(variant
, "id", FALSE
);
160 if (integer_definition
) {
161 id
= integer_definition
->value
._unsigned
;
164 stream
->event_id
= id
;
166 /* lookup timestamp */
167 stream
->has_timestamp
= 0;
168 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
169 if (integer_definition
) {
170 ctf_update_timestamp(stream
, integer_definition
);
171 stream
->has_timestamp
= 1;
174 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
175 if (integer_definition
) {
176 ctf_update_timestamp(stream
, integer_definition
);
177 stream
->has_timestamp
= 1;
183 /* Read stream-declared event context */
184 if (stream
->stream_event_context
) {
185 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
190 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
191 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
194 event
= g_ptr_array_index(stream
->events_by_id
, id
);
195 if (unlikely(!event
)) {
196 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
200 /* Read event-declared event context */
201 if (event
->event_context
) {
202 ret
= generic_rw(ppos
, &event
->event_context
->p
);
207 /* Read event payload */
208 if (likely(event
->event_fields
)) {
209 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
217 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
222 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
224 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
225 struct ctf_stream_event
*event
;
229 id
= stream
->event_id
;
231 /* print event header */
232 if (likely(stream
->stream_event_header
)) {
233 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
238 /* print stream-declared event context */
239 if (stream
->stream_event_context
) {
240 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
245 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
246 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
249 event
= g_ptr_array_index(stream
->events_by_id
, id
);
250 if (unlikely(!event
)) {
251 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
255 /* print event-declared event context */
256 if (event
->event_context
) {
257 ret
= generic_rw(pos
, &event
->event_context
->p
);
262 /* Read and print event payload */
263 if (likely(event
->event_fields
)) {
264 ret
= generic_rw(pos
, &event
->event_fields
->p
);
272 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
276 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
279 pos
->mmap_offset
= 0;
280 pos
->packet_size
= 0;
281 pos
->content_size
= 0;
282 pos
->content_size_loc
= NULL
;
288 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
289 sizeof(struct packet_index
));
291 pos
->packet_index
= NULL
;
292 switch (open_flags
& O_ACCMODE
) {
294 pos
->prot
= PROT_READ
;
295 pos
->flags
= MAP_PRIVATE
;
296 pos
->parent
.rw_table
= read_dispatch_table
;
297 pos
->parent
.event_cb
= ctf_read_event
;
300 pos
->prot
= PROT_WRITE
; /* Write has priority */
301 pos
->flags
= MAP_SHARED
;
302 pos
->parent
.rw_table
= write_dispatch_table
;
303 pos
->parent
.event_cb
= ctf_write_event
;
305 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
312 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
316 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
317 *pos
->content_size_loc
= pos
->offset
;
320 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
322 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
327 (void) g_array_free(pos
->packet_index
, TRUE
);
330 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
332 struct ctf_file_stream
*file_stream
=
333 container_of(pos
, struct ctf_file_stream
, pos
);
336 struct packet_index
*index
;
338 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
339 *pos
->content_size_loc
= pos
->offset
;
343 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
345 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
353 * The caller should never ask for ctf_move_pos across packets,
354 * except to get exactly at the beginning of the next packet.
356 if (pos
->prot
== PROT_WRITE
) {
359 /* The writer will add padding */
360 assert(pos
->offset
+ offset
== pos
->packet_size
);
361 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
364 assert(offset
== 0); /* only seek supported for now */
370 pos
->content_size
= -1U; /* Unknown at this point */
371 pos
->packet_size
= WRITE_PACKET_LEN
;
372 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
373 pos
->packet_size
/ CHAR_BIT
);
380 if (pos
->offset
== EOF
)
382 /* The reader will expect us to skip padding */
383 assert(pos
->offset
+ offset
== pos
->content_size
);
387 assert(offset
== 0); /* only seek supported for now */
393 if (pos
->cur_index
>= pos
->packet_index
->len
) {
397 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
399 pos
->mmap_offset
= index
->offset
;
401 /* Lookup context/packet size in index */
402 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
403 pos
->content_size
= index
->content_size
;
404 pos
->packet_size
= index
->packet_size
;
405 if (index
->data_offset
< index
->content_size
) {
406 pos
->offset
= 0; /* will read headers */
407 } else if (index
->data_offset
== index
->content_size
) {
409 pos
->offset
= index
->data_offset
;
411 goto read_next_packet
;
417 /* map new base. Need mapping length from header. */
418 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
419 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
420 if (pos
->base
== MAP_FAILED
) {
421 fprintf(stdout
, "[error] mmap error %s.\n",
426 /* update trace_packet_header and stream_packet_context */
427 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
428 /* Read packet header */
429 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
432 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
433 /* Read packet context */
434 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
440 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
446 len
= fread(&magic
, sizeof(magic
), 1, fp
);
450 if (magic
== TSDL_MAGIC
) {
452 td
->byte_order
= BYTE_ORDER
;
453 CTF_TRACE_SET_FIELD(td
, byte_order
);
454 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
456 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
457 LITTLE_ENDIAN
: BIG_ENDIAN
;
458 CTF_TRACE_SET_FIELD(td
, byte_order
);
466 * Returns 0 on success, -1 on error.
469 int check_version(unsigned int major
, unsigned int minor
)
484 /* eventually return an error instead of warning */
486 fprintf(stdout
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
492 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
495 struct metadata_packet_header header
;
496 size_t readlen
, writelen
, toread
;
497 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
500 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
504 if (td
->byte_order
!= BYTE_ORDER
) {
505 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
506 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
507 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
508 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
511 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
512 if (header
.compression_scheme
) {
513 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
514 header
.compression_scheme
);
517 if (header
.encryption_scheme
) {
518 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
519 header
.encryption_scheme
);
522 if (header
.checksum_scheme
) {
523 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
524 header
.checksum_scheme
);
527 if (check_version(header
.major
, header
.minor
) < 0)
529 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
530 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
531 CTF_TRACE_SET_FIELD(td
, uuid
);
533 if (uuid_compare(header
.uuid
, td
->uuid
))
537 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
540 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
545 if (babeltrace_debug
) {
547 fprintf(stdout
, "[debug] metadata packet read: %s\n",
551 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
552 if (writelen
< readlen
) {
562 ret
= 0; /* continue reading next packet */
569 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
570 ret
= fseek(in
, toread
, SEEK_CUR
);
572 fprintf(stdout
, "[warning] Missing padding at end of file\n");
579 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
588 * Using strlen on *buf instead of size of open_memstream
589 * because its size includes garbage at the end (after final
590 * \0). This is the allocated size, not the actual string size.
592 out
= open_memstream(buf
, &size
);
597 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
606 fclose(out
); /* flush the buffer */
608 /* open for reading */
609 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
614 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
615 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
618 struct ctf_scanner
*scanner
;
619 struct ctf_file_stream
*metadata_stream
;
624 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
627 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
629 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
634 td
->metadata
= &metadata_stream
->parent
;
635 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
636 if (metadata_stream
->pos
.fd
< 0) {
637 fprintf(stdout
, "Unable to open metadata.\n");
638 return metadata_stream
->pos
.fd
;
641 if (babeltrace_debug
)
644 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
646 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
651 if (packet_metadata(td
, fp
)) {
652 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
654 goto end_packet_read
;
656 unsigned int major
, minor
;
659 td
->byte_order
= BYTE_ORDER
;
661 /* Check text-only metadata header and version */
662 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
664 fprintf(stdout
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
665 if (check_version(major
, minor
) < 0) {
667 goto end_packet_read
;
672 scanner
= ctf_scanner_alloc(fp
);
674 fprintf(stdout
, "[error] Error allocating scanner\n");
676 goto end_scanner_alloc
;
678 ret
= ctf_scanner_append_ast(scanner
);
680 fprintf(stdout
, "[error] Error creating AST\n");
684 if (babeltrace_debug
) {
685 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
687 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
692 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
694 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
697 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
700 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
704 ctf_scanner_free(scanner
);
710 close(metadata_stream
->pos
.fd
);
712 g_free(metadata_stream
);
717 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
718 struct ctf_stream
*stream
,
719 struct ctf_event
*event
)
721 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
723 if (event
->context_decl
) {
724 struct definition
*definition
=
725 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
726 stream
->parent_def_scope
, 0, 0, "event.context");
730 stream_event
->event_context
= container_of(definition
,
731 struct definition_struct
, p
);
732 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
734 if (event
->fields_decl
) {
735 struct definition
*definition
=
736 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
737 stream
->parent_def_scope
, 0, 0, "event.fields");
741 stream_event
->event_fields
= container_of(definition
,
742 struct definition_struct
, p
);
743 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
748 if (stream_event
->event_fields
)
749 definition_unref(&stream_event
->event_fields
->p
);
750 if (stream_event
->event_context
)
751 definition_unref(&stream_event
->event_context
->p
);
756 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
758 struct ctf_stream_class
*stream_class
;
762 if (stream
->stream_definitions_created
)
765 stream_class
= stream
->stream_class
;
767 if (stream_class
->packet_context_decl
) {
768 struct definition
*definition
=
769 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
770 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
775 stream
->stream_packet_context
= container_of(definition
,
776 struct definition_struct
, p
);
777 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
779 if (stream_class
->event_header_decl
) {
780 struct definition
*definition
=
781 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
782 stream
->parent_def_scope
, 0, 0, "stream.event.header");
787 stream
->stream_event_header
=
788 container_of(definition
, struct definition_struct
, p
);
789 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
791 if (stream_class
->event_context_decl
) {
792 struct definition
*definition
=
793 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
794 stream
->parent_def_scope
, 0, 0, "stream.event.context");
799 stream
->stream_event_context
=
800 container_of(definition
, struct definition_struct
, p
);
801 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
803 stream
->events_by_id
= g_ptr_array_new();
804 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
805 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
806 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
807 struct ctf_stream_event
*stream_event
;
811 stream_event
= create_event_definitions(td
, stream
, event
);
814 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
819 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
820 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
822 g_free(stream_event
);
824 g_ptr_array_free(stream
->events_by_id
, TRUE
);
826 if (stream
->stream_event_context
)
827 definition_unref(&stream
->stream_event_context
->p
);
828 if (stream
->stream_event_header
)
829 definition_unref(&stream
->stream_event_header
->p
);
830 if (stream
->stream_packet_context
)
831 definition_unref(&stream
->stream_packet_context
->p
);
837 int create_stream_packet_index(struct ctf_trace
*td
,
838 struct ctf_file_stream
*file_stream
)
840 struct ctf_stream_class
*stream
;
842 struct ctf_stream_pos
*pos
;
843 struct stat filestats
;
844 struct packet_index packet_index
;
845 int first_packet
= 1;
848 pos
= &file_stream
->pos
;
850 ret
= fstat(pos
->fd
, &filestats
);
854 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
857 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
858 uint64_t stream_id
= 0;
862 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
864 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
870 /* map new base. Need mapping length from header. */
871 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
872 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
873 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
874 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
875 pos
->offset
= 0; /* Position of the packet header */
877 packet_index
.offset
= pos
->mmap_offset
;
878 packet_index
.content_size
= 0;
879 packet_index
.packet_size
= 0;
880 packet_index
.timestamp_begin
= 0;
881 packet_index
.timestamp_end
= 0;
883 /* read and check header, set stream id (and check) */
884 if (file_stream
->parent
.trace_packet_header
) {
885 /* Read packet header */
886 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
889 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
890 if (len_index
>= 0) {
891 struct definition_integer
*defint
;
892 struct definition
*field
;
894 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
895 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
896 defint
= container_of(field
, struct definition_integer
, p
);
897 assert(defint
->declaration
->signedness
== FALSE
);
898 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
899 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
900 defint
->value
._unsigned
,
901 file_stream
->pos
.packet_index
->len
,
902 (ssize_t
) pos
->mmap_offset
);
908 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
909 if (len_index
>= 0) {
910 struct definition_array
*defarray
;
911 struct definition
*field
;
913 uint8_t uuidval
[UUID_LEN
];
915 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
916 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
917 defarray
= container_of(field
, struct definition_array
, p
);
918 assert(array_len(defarray
) == UUID_LEN
);
919 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
921 for (i
= 0; i
< UUID_LEN
; i
++) {
922 struct definition
*elem
;
923 struct definition_integer
*defint
;
925 elem
= array_index(defarray
, i
);
927 defint
= container_of(elem
, struct definition_integer
, p
);
928 uuidval
[i
] = defint
->value
._unsigned
;
930 ret
= uuid_compare(td
->uuid
, uuidval
);
932 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
938 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
939 if (len_index
>= 0) {
940 struct definition_integer
*defint
;
941 struct definition
*field
;
943 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
944 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
945 defint
= container_of(field
, struct definition_integer
, p
);
946 assert(defint
->declaration
->signedness
== FALSE
);
947 stream_id
= defint
->value
._unsigned
;
951 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
952 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
956 file_stream
->parent
.stream_id
= stream_id
;
957 if (stream_id
>= td
->streams
->len
) {
958 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
961 stream
= g_ptr_array_index(td
->streams
, stream_id
);
963 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
966 file_stream
->parent
.stream_class
= stream
;
967 ret
= create_stream_definitions(td
, &file_stream
->parent
);
973 if (file_stream
->parent
.stream_packet_context
) {
974 /* Read packet context */
975 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
978 /* read content size from header */
979 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
980 if (len_index
>= 0) {
981 struct definition_integer
*defint
;
982 struct definition
*field
;
984 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
985 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
986 defint
= container_of(field
, struct definition_integer
, p
);
987 assert(defint
->declaration
->signedness
== FALSE
);
988 packet_index
.content_size
= defint
->value
._unsigned
;
990 /* Use file size for packet size */
991 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
994 /* read packet size from header */
995 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
996 if (len_index
>= 0) {
997 struct definition_integer
*defint
;
998 struct definition
*field
;
1000 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1001 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1002 defint
= container_of(field
, struct definition_integer
, p
);
1003 assert(defint
->declaration
->signedness
== FALSE
);
1004 packet_index
.packet_size
= defint
->value
._unsigned
;
1006 /* Use content size if non-zero, else file size */
1007 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1010 /* read timestamp begin from header */
1011 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
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_begin
= defint
->value
._unsigned
;
1023 /* read timestamp end from header */
1024 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1025 if (len_index
>= 0) {
1026 struct definition_integer
*defint
;
1027 struct definition
*field
;
1029 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1030 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1031 defint
= container_of(field
, struct definition_integer
, p
);
1032 assert(defint
->declaration
->signedness
== FALSE
);
1033 packet_index
.timestamp_end
= defint
->value
._unsigned
;
1036 /* Use file size for packet size */
1037 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1038 /* Use content size if non-zero, else file size */
1039 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1042 /* Validate content size and packet size values */
1043 if (packet_index
.content_size
> packet_index
.packet_size
) {
1044 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1045 packet_index
.content_size
, packet_index
.packet_size
);
1049 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1050 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1051 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1055 /* Save position after header and context */
1056 packet_index
.data_offset
= pos
->offset
;
1058 /* add index to packet array */
1059 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1061 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1064 /* Move pos back to beginning of file */
1065 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1071 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1075 if (td
->packet_header_decl
) {
1076 struct definition
*definition
=
1077 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1078 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1083 stream
->trace_packet_header
=
1084 container_of(definition
, struct definition_struct
, p
);
1085 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1095 * Note: many file streams can inherit from the same stream class
1096 * description (metadata).
1099 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1100 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1104 struct ctf_file_stream
*file_stream
;
1106 ret
= openat(td
->dirfd
, path
, flags
);
1109 file_stream
= g_new0(struct ctf_file_stream
, 1);
1111 if (move_pos_slow
) {
1112 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1114 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1119 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1120 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1123 ret
= create_stream_packet_index(td
, file_stream
);
1126 /* Add stream file to stream class */
1127 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1128 &file_stream
->parent
);
1132 if (file_stream
->parent
.trace_packet_header
)
1133 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1135 ctf_fini_pos(&file_stream
->pos
);
1136 close(file_stream
->pos
.fd
);
1137 g_free(file_stream
);
1143 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
,
1144 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1148 struct dirent
*dirent
;
1149 struct dirent
*diriter
;
1154 /* Open trace directory */
1155 td
->dir
= opendir(path
);
1157 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1162 td
->dirfd
= open(path
, 0);
1163 if (td
->dirfd
< 0) {
1164 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1170 * Keep the metadata file separate.
1173 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
);
1175 goto error_metadata
;
1179 * Open each stream: for each file, try to open, check magic
1180 * number, and get the stream ID to add to the right location in
1184 dirent_len
= offsetof(struct dirent
, d_name
) +
1185 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1187 dirent
= malloc(dirent_len
);
1190 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1192 fprintf(stdout
, "[error] Readdir error.\n");
1197 /* Ignore hidden files, ., .. and metadata. */
1198 if (!strncmp(diriter
->d_name
, ".", 1)
1199 || !strcmp(diriter
->d_name
, "..")
1200 || !strcmp(diriter
->d_name
, "metadata"))
1202 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1204 fprintf(stdout
, "[error] Open file stream error.\n");
1223 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1224 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1227 struct ctf_trace
*td
;
1230 td
= g_new0(struct ctf_trace
, 1);
1232 switch (flags
& O_ACCMODE
) {
1235 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1238 ret
= ctf_open_trace_read(td
, path
, flags
, move_pos_slow
);
1243 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1246 fprintf(stdout
, "[error] Incorrect open flags.\n");
1257 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1259 ctf_fini_pos(&file_stream
->pos
);
1260 close(file_stream
->pos
.fd
);
1264 void ctf_close_trace(struct trace_descriptor
*tdp
)
1266 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1270 for (i
= 0; i
< td
->streams
->len
; i
++) {
1271 struct ctf_stream_class
*stream
;
1274 stream
= g_ptr_array_index(td
->streams
, i
);
1277 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1278 struct ctf_file_stream
*file_stream
;
1279 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1280 ctf_close_file_stream(file_stream
);
1284 g_ptr_array_free(td
->streams
, TRUE
);
1290 void __attribute__((constructor
)) ctf_init(void)
1294 ctf_format
.name
= g_quark_from_static_string("ctf");
1295 ret
= bt_register_format(&ctf_format
);
1299 /* TODO: finalize */