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
,
60 int whence
), FILE *metadata_fp
);
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 /* We need to check for EOF here for empty files. */
131 if (unlikely(pos
->offset
== EOF
))
134 ctf_pos_get_event(pos
);
137 * This is the EOF check after we've advanced the position in
140 if (unlikely(pos
->offset
== EOF
))
142 assert(pos
->offset
< pos
->content_size
);
144 /* Read event header */
145 if (likely(stream
->stream_event_header
)) {
146 struct definition_integer
*integer_definition
;
147 struct definition
*variant
;
149 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
152 /* lookup event id */
153 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
154 if (integer_definition
) {
155 id
= integer_definition
->value
._unsigned
;
157 struct definition_enum
*enum_definition
;
159 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
160 if (enum_definition
) {
161 id
= enum_definition
->integer
->value
._unsigned
;
165 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
167 integer_definition
= lookup_integer(variant
, "id", FALSE
);
168 if (integer_definition
) {
169 id
= integer_definition
->value
._unsigned
;
172 stream
->event_id
= id
;
174 /* lookup timestamp */
175 stream
->has_timestamp
= 0;
176 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
177 if (integer_definition
) {
178 ctf_update_timestamp(stream
, integer_definition
);
179 stream
->has_timestamp
= 1;
182 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
183 if (integer_definition
) {
184 ctf_update_timestamp(stream
, integer_definition
);
185 stream
->has_timestamp
= 1;
191 /* Read stream-declared event context */
192 if (stream
->stream_event_context
) {
193 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
198 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
199 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
202 event
= g_ptr_array_index(stream
->events_by_id
, id
);
203 if (unlikely(!event
)) {
204 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
208 /* Read event-declared event context */
209 if (event
->event_context
) {
210 ret
= generic_rw(ppos
, &event
->event_context
->p
);
215 /* Read event payload */
216 if (likely(event
->event_fields
)) {
217 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
225 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
230 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
232 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
233 struct ctf_stream_event
*event
;
237 id
= stream
->event_id
;
239 /* print event header */
240 if (likely(stream
->stream_event_header
)) {
241 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
246 /* print stream-declared event context */
247 if (stream
->stream_event_context
) {
248 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
253 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
254 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
257 event
= g_ptr_array_index(stream
->events_by_id
, id
);
258 if (unlikely(!event
)) {
259 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
263 /* print event-declared event context */
264 if (event
->event_context
) {
265 ret
= generic_rw(pos
, &event
->event_context
->p
);
270 /* Read and print event payload */
271 if (likely(event
->event_fields
)) {
272 ret
= generic_rw(pos
, &event
->event_fields
->p
);
280 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
284 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
287 pos
->mmap_offset
= 0;
288 pos
->packet_size
= 0;
289 pos
->content_size
= 0;
290 pos
->content_size_loc
= NULL
;
296 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
297 sizeof(struct packet_index
));
299 pos
->packet_index
= NULL
;
300 switch (open_flags
& O_ACCMODE
) {
302 pos
->prot
= PROT_READ
;
303 pos
->flags
= MAP_PRIVATE
;
304 pos
->parent
.rw_table
= read_dispatch_table
;
305 pos
->parent
.event_cb
= ctf_read_event
;
308 pos
->prot
= PROT_WRITE
; /* Write has priority */
309 pos
->flags
= MAP_SHARED
;
310 pos
->parent
.rw_table
= write_dispatch_table
;
311 pos
->parent
.event_cb
= ctf_write_event
;
313 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
320 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
324 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
325 *pos
->content_size_loc
= pos
->offset
;
328 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
330 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
335 (void) g_array_free(pos
->packet_index
, TRUE
);
338 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
340 struct ctf_file_stream
*file_stream
=
341 container_of(pos
, struct ctf_file_stream
, pos
);
344 struct packet_index
*index
;
346 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
347 *pos
->content_size_loc
= pos
->offset
;
351 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
353 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
361 * The caller should never ask for ctf_move_pos across packets,
362 * except to get exactly at the beginning of the next packet.
364 if (pos
->prot
== PROT_WRITE
) {
367 /* The writer will add padding */
368 assert(pos
->offset
+ offset
== pos
->packet_size
);
369 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
372 assert(offset
== 0); /* only seek supported for now */
378 pos
->content_size
= -1U; /* Unknown at this point */
379 pos
->packet_size
= WRITE_PACKET_LEN
;
380 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
381 pos
->packet_size
/ CHAR_BIT
);
388 if (pos
->offset
== EOF
)
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 */
415 } else if (index
->data_offset
== index
->content_size
) {
417 pos
->offset
= index
->data_offset
;
420 goto read_next_packet
;
426 /* map new base. Need mapping length from header. */
427 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
428 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
429 if (pos
->base
== MAP_FAILED
) {
430 fprintf(stdout
, "[error] mmap error %s.\n",
435 /* update trace_packet_header and stream_packet_context */
436 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
437 /* Read packet header */
438 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
441 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
442 /* Read packet context */
443 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
449 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
455 len
= fread(&magic
, sizeof(magic
), 1, fp
);
459 if (magic
== TSDL_MAGIC
) {
461 td
->byte_order
= BYTE_ORDER
;
462 CTF_TRACE_SET_FIELD(td
, byte_order
);
463 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
465 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
466 LITTLE_ENDIAN
: BIG_ENDIAN
;
467 CTF_TRACE_SET_FIELD(td
, byte_order
);
475 * Returns 0 on success, -1 on error.
478 int check_version(unsigned int major
, unsigned int minor
)
493 /* eventually return an error instead of warning */
495 fprintf(stdout
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
501 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
504 struct metadata_packet_header header
;
505 size_t readlen
, writelen
, toread
;
506 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
509 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
513 if (td
->byte_order
!= BYTE_ORDER
) {
514 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
515 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
516 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
517 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
520 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
521 if (header
.compression_scheme
) {
522 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
523 header
.compression_scheme
);
526 if (header
.encryption_scheme
) {
527 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
528 header
.encryption_scheme
);
531 if (header
.checksum_scheme
) {
532 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
533 header
.checksum_scheme
);
536 if (check_version(header
.major
, header
.minor
) < 0)
538 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
539 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
540 CTF_TRACE_SET_FIELD(td
, uuid
);
542 if (uuid_compare(header
.uuid
, td
->uuid
))
546 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
549 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
554 if (babeltrace_debug
) {
556 fprintf(stdout
, "[debug] metadata packet read: %s\n",
560 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
561 if (writelen
< readlen
) {
571 ret
= 0; /* continue reading next packet */
578 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
579 ret
= fseek(in
, toread
, SEEK_CUR
);
581 fprintf(stdout
, "[warning] Missing padding at end of file\n");
588 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
597 * Using strlen on *buf instead of size of open_memstream
598 * because its size includes garbage at the end (after final
599 * \0). This is the allocated size, not the actual string size.
601 out
= open_memstream(buf
, &size
);
606 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
615 fclose(out
); /* flush the buffer */
617 /* open for reading */
618 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
623 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
624 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
625 int whence
), FILE *metadata_fp
)
627 struct ctf_scanner
*scanner
;
628 struct ctf_file_stream
*metadata_stream
;
633 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
636 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
638 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
646 td
->metadata
= &metadata_stream
->parent
;
647 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
648 if (metadata_stream
->pos
.fd
< 0) {
649 fprintf(stdout
, "Unable to open metadata.\n");
650 return metadata_stream
->pos
.fd
;
653 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
655 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
660 if (babeltrace_debug
)
663 if (packet_metadata(td
, fp
)) {
664 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
666 goto end_packet_read
;
668 unsigned int major
, minor
;
671 td
->byte_order
= BYTE_ORDER
;
673 /* Check text-only metadata header and version */
674 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
676 fprintf(stdout
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
677 if (check_version(major
, minor
) < 0) {
679 goto end_packet_read
;
684 scanner
= ctf_scanner_alloc(fp
);
686 fprintf(stdout
, "[error] Error allocating scanner\n");
688 goto end_scanner_alloc
;
690 ret
= ctf_scanner_append_ast(scanner
);
692 fprintf(stdout
, "[error] Error creating AST\n");
696 if (babeltrace_debug
) {
697 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
699 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
704 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
706 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
709 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
712 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
716 ctf_scanner_free(scanner
);
722 close(metadata_stream
->pos
.fd
);
724 g_free(metadata_stream
);
729 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
730 struct ctf_stream
*stream
,
731 struct ctf_event
*event
)
733 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
735 if (event
->context_decl
) {
736 struct definition
*definition
=
737 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
738 stream
->parent_def_scope
, 0, 0, "event.context");
742 stream_event
->event_context
= container_of(definition
,
743 struct definition_struct
, p
);
744 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
746 if (event
->fields_decl
) {
747 struct definition
*definition
=
748 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
749 stream
->parent_def_scope
, 0, 0, "event.fields");
753 stream_event
->event_fields
= container_of(definition
,
754 struct definition_struct
, p
);
755 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
760 if (stream_event
->event_fields
)
761 definition_unref(&stream_event
->event_fields
->p
);
762 if (stream_event
->event_context
)
763 definition_unref(&stream_event
->event_context
->p
);
768 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
770 struct ctf_stream_class
*stream_class
;
774 if (stream
->stream_definitions_created
)
777 stream_class
= stream
->stream_class
;
779 if (stream_class
->packet_context_decl
) {
780 struct definition
*definition
=
781 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
782 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
787 stream
->stream_packet_context
= container_of(definition
,
788 struct definition_struct
, p
);
789 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
791 if (stream_class
->event_header_decl
) {
792 struct definition
*definition
=
793 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
794 stream
->parent_def_scope
, 0, 0, "stream.event.header");
799 stream
->stream_event_header
=
800 container_of(definition
, struct definition_struct
, p
);
801 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
803 if (stream_class
->event_context_decl
) {
804 struct definition
*definition
=
805 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
806 stream
->parent_def_scope
, 0, 0, "stream.event.context");
811 stream
->stream_event_context
=
812 container_of(definition
, struct definition_struct
, p
);
813 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
815 stream
->events_by_id
= g_ptr_array_new();
816 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
817 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
818 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
819 struct ctf_stream_event
*stream_event
;
823 stream_event
= create_event_definitions(td
, stream
, event
);
826 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
831 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
832 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
834 g_free(stream_event
);
836 g_ptr_array_free(stream
->events_by_id
, TRUE
);
838 if (stream
->stream_event_context
)
839 definition_unref(&stream
->stream_event_context
->p
);
840 if (stream
->stream_event_header
)
841 definition_unref(&stream
->stream_event_header
->p
);
842 if (stream
->stream_packet_context
)
843 definition_unref(&stream
->stream_packet_context
->p
);
849 int create_stream_packet_index(struct ctf_trace
*td
,
850 struct ctf_file_stream
*file_stream
)
852 struct ctf_stream_class
*stream
;
854 struct ctf_stream_pos
*pos
;
855 struct stat filestats
;
856 struct packet_index packet_index
;
857 int first_packet
= 1;
860 pos
= &file_stream
->pos
;
862 ret
= fstat(pos
->fd
, &filestats
);
866 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
869 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
870 uint64_t stream_id
= 0;
874 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
876 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
882 /* map new base. Need mapping length from header. */
883 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
884 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
885 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
886 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
887 pos
->offset
= 0; /* Position of the packet header */
889 packet_index
.offset
= pos
->mmap_offset
;
890 packet_index
.content_size
= 0;
891 packet_index
.packet_size
= 0;
892 packet_index
.timestamp_begin
= 0;
893 packet_index
.timestamp_end
= 0;
895 /* read and check header, set stream id (and check) */
896 if (file_stream
->parent
.trace_packet_header
) {
897 /* Read packet header */
898 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
901 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
902 if (len_index
>= 0) {
903 struct definition_integer
*defint
;
904 struct definition
*field
;
906 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
907 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
908 defint
= container_of(field
, struct definition_integer
, p
);
909 assert(defint
->declaration
->signedness
== FALSE
);
910 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
911 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
912 defint
->value
._unsigned
,
913 file_stream
->pos
.packet_index
->len
,
914 (ssize_t
) pos
->mmap_offset
);
920 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
921 if (len_index
>= 0) {
922 struct definition_array
*defarray
;
923 struct definition
*field
;
925 uint8_t uuidval
[UUID_LEN
];
927 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
928 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
929 defarray
= container_of(field
, struct definition_array
, p
);
930 assert(array_len(defarray
) == UUID_LEN
);
931 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
933 for (i
= 0; i
< UUID_LEN
; i
++) {
934 struct definition
*elem
;
935 struct definition_integer
*defint
;
937 elem
= array_index(defarray
, i
);
939 defint
= container_of(elem
, struct definition_integer
, p
);
940 uuidval
[i
] = defint
->value
._unsigned
;
942 ret
= uuid_compare(td
->uuid
, uuidval
);
944 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
950 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
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
.trace_packet_header
, 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 stream_id
= defint
->value
._unsigned
;
963 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
964 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
968 file_stream
->parent
.stream_id
= stream_id
;
969 if (stream_id
>= td
->streams
->len
) {
970 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
973 stream
= g_ptr_array_index(td
->streams
, stream_id
);
975 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
978 file_stream
->parent
.stream_class
= stream
;
979 ret
= create_stream_definitions(td
, &file_stream
->parent
);
985 if (file_stream
->parent
.stream_packet_context
) {
986 /* Read packet context */
987 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
990 /* read content size from header */
991 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
992 if (len_index
>= 0) {
993 struct definition_integer
*defint
;
994 struct definition
*field
;
996 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
997 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
998 defint
= container_of(field
, struct definition_integer
, p
);
999 assert(defint
->declaration
->signedness
== FALSE
);
1000 packet_index
.content_size
= defint
->value
._unsigned
;
1002 /* Use file size for packet size */
1003 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1006 /* read packet size from header */
1007 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1008 if (len_index
>= 0) {
1009 struct definition_integer
*defint
;
1010 struct definition
*field
;
1012 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1013 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1014 defint
= container_of(field
, struct definition_integer
, p
);
1015 assert(defint
->declaration
->signedness
== FALSE
);
1016 packet_index
.packet_size
= defint
->value
._unsigned
;
1018 /* Use content size if non-zero, else file size */
1019 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1022 /* read timestamp begin from header */
1023 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1024 if (len_index
>= 0) {
1025 struct definition_integer
*defint
;
1026 struct definition
*field
;
1028 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1029 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1030 defint
= container_of(field
, struct definition_integer
, p
);
1031 assert(defint
->declaration
->signedness
== FALSE
);
1032 packet_index
.timestamp_begin
= defint
->value
._unsigned
;
1035 /* read timestamp end from header */
1036 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1037 if (len_index
>= 0) {
1038 struct definition_integer
*defint
;
1039 struct definition
*field
;
1041 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1042 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
1043 defint
= container_of(field
, struct definition_integer
, p
);
1044 assert(defint
->declaration
->signedness
== FALSE
);
1045 packet_index
.timestamp_end
= defint
->value
._unsigned
;
1048 /* Use file size for packet size */
1049 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1050 /* Use content size if non-zero, else file size */
1051 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1054 /* Validate content size and packet size values */
1055 if (packet_index
.content_size
> packet_index
.packet_size
) {
1056 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1057 packet_index
.content_size
, packet_index
.packet_size
);
1061 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1062 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1063 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1067 /* Save position after header and context */
1068 packet_index
.data_offset
= pos
->offset
;
1070 /* add index to packet array */
1071 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1073 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1076 /* Move pos back to beginning of file */
1077 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1083 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1087 if (td
->packet_header_decl
) {
1088 struct definition
*definition
=
1089 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1090 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1095 stream
->trace_packet_header
=
1096 container_of(definition
, struct definition_struct
, p
);
1097 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1107 * Note: many file streams can inherit from the same stream class
1108 * description (metadata).
1111 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1112 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1116 struct ctf_file_stream
*file_stream
;
1118 ret
= openat(td
->dirfd
, path
, flags
);
1121 file_stream
= g_new0(struct ctf_file_stream
, 1);
1123 if (move_pos_slow
) {
1124 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1126 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1131 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1132 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1135 ret
= create_stream_packet_index(td
, file_stream
);
1138 /* Add stream file to stream class */
1139 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1140 &file_stream
->parent
);
1144 if (file_stream
->parent
.trace_packet_header
)
1145 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1147 ctf_fini_pos(&file_stream
->pos
);
1148 close(file_stream
->pos
.fd
);
1149 g_free(file_stream
);
1155 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
,
1156 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1157 int whence
), FILE *metadata_fp
)
1160 struct dirent
*dirent
;
1161 struct dirent
*diriter
;
1166 /* Open trace directory */
1167 td
->dir
= opendir(path
);
1169 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1174 td
->dirfd
= open(path
, 0);
1175 if (td
->dirfd
< 0) {
1176 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1182 * Keep the metadata file separate.
1185 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1187 goto error_metadata
;
1191 * Open each stream: for each file, try to open, check magic
1192 * number, and get the stream ID to add to the right location in
1196 dirent_len
= offsetof(struct dirent
, d_name
) +
1197 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1199 dirent
= malloc(dirent_len
);
1202 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1204 fprintf(stdout
, "[error] Readdir error.\n");
1209 /* Ignore hidden files, ., .. and metadata. */
1210 if (!strncmp(diriter
->d_name
, ".", 1)
1211 || !strcmp(diriter
->d_name
, "..")
1212 || !strcmp(diriter
->d_name
, "metadata"))
1214 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1216 fprintf(stdout
, "[error] Open file stream error.\n");
1235 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1236 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1237 int whence
), FILE *metadata_fp
)
1239 struct ctf_trace
*td
;
1242 td
= g_new0(struct ctf_trace
, 1);
1244 switch (flags
& O_ACCMODE
) {
1247 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1250 ret
= ctf_open_trace_read(td
, path
, flags
, move_pos_slow
, metadata_fp
);
1255 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1258 fprintf(stdout
, "[error] Incorrect open flags.\n");
1269 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1271 ctf_fini_pos(&file_stream
->pos
);
1272 close(file_stream
->pos
.fd
);
1276 void ctf_close_trace(struct trace_descriptor
*tdp
)
1278 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1282 for (i
= 0; i
< td
->streams
->len
; i
++) {
1283 struct ctf_stream_class
*stream
;
1286 stream
= g_ptr_array_index(td
->streams
, i
);
1289 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1290 struct ctf_file_stream
*file_stream
;
1291 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1292 ctf_close_file_stream(file_stream
);
1296 g_ptr_array_free(td
->streams
, TRUE
);
1302 void __attribute__((constructor
)) ctf_init(void)
1306 ctf_format
.name
= g_quark_from_static_string("ctf");
1307 ret
= bt_register_format(&ctf_format
);
1311 /* TODO: finalize */