2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
25 #include <uuid/uuid.h>
29 #include <sys/types.h>
37 #include "metadata/ctf-scanner.h"
38 #include "metadata/ctf-parser.h"
39 #include "metadata/ctf-ast.h"
42 * We currently simply map a page to read the packet header and packet
43 * context to get the packet length and content length. (in bits)
45 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
46 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
47 #define UUID_LEN 16 /* uuid by value len */
50 #define min(a, b) (((a) < (b)) ? (a) : (b))
56 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
58 void ctf_close_trace(struct trace_descriptor
*descriptor
);
61 rw_dispatch read_dispatch_table
[] = {
62 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
63 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
64 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
65 [ CTF_TYPE_STRING
] = ctf_string_read
,
66 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
67 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
68 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
69 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
73 rw_dispatch write_dispatch_table
[] = {
74 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
75 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
76 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
77 [ CTF_TYPE_STRING
] = ctf_string_write
,
78 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
79 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
80 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
81 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
85 struct format ctf_format
= {
86 .open_trace
= ctf_open_trace
,
87 .close_trace
= ctf_close_trace
,
91 void ctf_update_timestamp(struct ctf_stream
*stream
,
92 struct definition_integer
*integer_definition
)
94 struct declaration_integer
*integer_declaration
=
95 integer_definition
->declaration
;
96 uint64_t oldval
, newval
, updateval
;
98 if (integer_declaration
->len
== 64) {
99 stream
->timestamp
= integer_definition
->value
._unsigned
;
103 oldval
= stream
->timestamp
;
104 oldval
&= (1ULL << integer_declaration
->len
) - 1;
105 newval
= integer_definition
->value
._unsigned
;
106 /* Test for overflow by comparing low bits */
108 newval
+= 1ULL << integer_declaration
->len
;
109 /* updateval contains old high bits, and new low bits (sum) */
110 updateval
= stream
->timestamp
;
111 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
113 stream
->timestamp
= updateval
;
117 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
119 struct ctf_stream_pos
*pos
=
120 container_of(ppos
, struct ctf_stream_pos
, parent
);
121 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
122 struct ctf_event
*event_class
;
126 if (pos
->offset
== EOF
)
129 /* Read event header */
130 if (stream_class
->event_header
) {
131 struct definition_integer
*integer_definition
;
132 struct definition
*variant
;
134 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
137 /* lookup event id */
138 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
139 if (integer_definition
) {
140 id
= integer_definition
->value
._unsigned
;
142 struct definition_enum
*enum_definition
;
144 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
145 if (enum_definition
) {
146 id
= enum_definition
->integer
->value
._unsigned
;
150 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
152 integer_definition
= lookup_integer(variant
, "id", FALSE
);
153 if (integer_definition
) {
154 id
= integer_definition
->value
._unsigned
;
158 /* lookup timestamp */
159 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "timestamp", FALSE
);
160 if (integer_definition
) {
161 ctf_update_timestamp(stream
, integer_definition
);
164 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
165 if (integer_definition
) {
166 ctf_update_timestamp(stream
, integer_definition
);
172 /* Read stream-declared event context */
173 if (stream_class
->event_context
) {
174 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
179 if (id
>= stream_class
->events_by_id
->len
) {
180 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
183 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
185 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
189 /* Read event-declared event context */
190 if (event_class
->context
) {
191 ret
= generic_rw(ppos
, &event_class
->context
->p
);
196 /* Read event payload */
197 if (event_class
->fields
) {
198 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
206 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
211 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
213 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
214 struct ctf_event
*event_class
;
218 /* print event header */
219 if (stream_class
->event_header
) {
220 struct definition_integer
*integer_definition
;
221 struct definition
*variant
;
223 /* lookup event id */
224 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
225 if (integer_definition
) {
226 id
= integer_definition
->value
._unsigned
;
228 struct definition_enum
*enum_definition
;
230 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
231 if (enum_definition
) {
232 id
= enum_definition
->integer
->value
._unsigned
;
236 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
238 integer_definition
= lookup_integer(variant
, "id", FALSE
);
239 if (integer_definition
) {
240 id
= integer_definition
->value
._unsigned
;
244 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
249 /* print stream-declared event context */
250 if (stream_class
->event_context
) {
251 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
256 if (id
>= stream_class
->events_by_id
->len
) {
257 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
260 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
262 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
266 /* print event-declared event context */
267 if (event_class
->context
) {
268 ret
= generic_rw(pos
, &event_class
->context
->p
);
273 /* Read and print event payload */
274 if (event_class
->fields
) {
275 ret
= generic_rw(pos
, &event_class
->fields
->p
);
283 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
287 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
290 pos
->mmap_offset
= 0;
291 pos
->packet_size
= 0;
292 pos
->content_size
= 0;
293 pos
->content_size_loc
= NULL
;
299 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
300 sizeof(struct packet_index
));
302 pos
->packet_index
= NULL
;
303 switch (open_flags
& O_ACCMODE
) {
305 pos
->prot
= PROT_READ
;
306 pos
->flags
= MAP_PRIVATE
;
307 pos
->parent
.rw_table
= read_dispatch_table
;
308 pos
->parent
.event_cb
= ctf_read_event
;
311 pos
->prot
= PROT_WRITE
; /* Write has priority */
312 pos
->flags
= MAP_SHARED
;
313 pos
->parent
.rw_table
= write_dispatch_table
;
314 pos
->parent
.event_cb
= ctf_write_event
;
316 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
323 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
327 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
328 *pos
->content_size_loc
= pos
->offset
;
331 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
333 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
338 (void) g_array_free(pos
->packet_index
, TRUE
);
341 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
345 struct packet_index
*index
;
347 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
348 *pos
->content_size_loc
= pos
->offset
;
352 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
354 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
362 * The caller should never ask for ctf_move_pos across packets,
363 * except to get exactly at the beginning of the next packet.
365 if (pos
->prot
== PROT_WRITE
) {
368 /* The writer will add padding */
369 assert(pos
->offset
+ offset
== pos
->packet_size
);
370 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
373 assert(offset
== 0); /* only seek supported for now */
379 pos
->content_size
= -1U; /* Unknown at this point */
380 pos
->packet_size
= WRITE_PACKET_LEN
;
381 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
382 pos
->packet_size
/ CHAR_BIT
);
388 /* The reader will expect us to skip padding */
389 assert(pos
->offset
+ offset
== pos
->content_size
);
393 assert(offset
== 0); /* only seek supported for now */
399 if (pos
->cur_index
>= pos
->packet_index
->len
) {
403 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
405 pos
->mmap_offset
= index
->offset
;
407 /* Lookup context/packet size in index */
408 pos
->content_size
= index
->content_size
;
409 pos
->packet_size
= index
->packet_size
;
410 if (index
->data_offset
< index
->content_size
)
411 pos
->offset
= index
->data_offset
;
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",
428 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
434 len
= fread(&magic
, sizeof(magic
), 1, fp
);
438 if (magic
== TSDL_MAGIC
) {
440 td
->byte_order
= BYTE_ORDER
;
441 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
443 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
444 LITTLE_ENDIAN
: BIG_ENDIAN
;
446 CTF_TRACE_SET_FIELD(td
, byte_order
);
453 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
456 struct metadata_packet_header header
;
457 size_t readlen
, writelen
, toread
;
461 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
465 if (td
->byte_order
!= BYTE_ORDER
) {
466 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
467 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
468 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
469 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
472 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
473 if (header
.compression_scheme
) {
474 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
475 header
.compression_scheme
);
478 if (header
.encryption_scheme
) {
479 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
480 header
.encryption_scheme
);
483 if (header
.checksum_scheme
) {
484 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
485 header
.checksum_scheme
);
488 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
489 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
490 CTF_TRACE_SET_FIELD(td
, uuid
);
492 if (uuid_compare(header
.uuid
, td
->uuid
))
496 toread
= header
.content_size
/ CHAR_BIT
;
499 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
504 if (babeltrace_debug
) {
505 fprintf(stdout
, "[debug] metadata packet read: %s\n",
509 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
510 if (writelen
< readlen
) {
520 ret
= 0; /* continue reading next packet */
528 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
537 * Using strlen on *buf instead of size of open_memstream
538 * because its size includes garbage at the end (after final
539 * \0). This is the allocated size, not the actual string size.
541 out
= open_memstream(buf
, &size
);
546 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
555 fclose(out
); /* flush the buffer */
557 /* open for reading */
558 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
563 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
565 struct ctf_scanner
*scanner
;
570 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
571 if (td
->metadata
.pos
.fd
< 0) {
572 fprintf(stdout
, "Unable to open metadata.\n");
573 return td
->metadata
.pos
.fd
;
576 if (babeltrace_debug
)
579 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
581 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
586 if (packet_metadata(td
, fp
)) {
587 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
589 goto end_packet_read
;
592 scanner
= ctf_scanner_alloc(fp
);
594 fprintf(stdout
, "[error] Error allocating scanner\n");
596 goto end_scanner_alloc
;
598 ret
= ctf_scanner_append_ast(scanner
);
600 fprintf(stdout
, "[error] Error creating AST\n");
604 if (babeltrace_debug
) {
605 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
607 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
612 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
614 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
617 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
620 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
624 ctf_scanner_free(scanner
);
630 close(td
->metadata
.pos
.fd
);
636 int create_stream_packet_index(struct ctf_trace
*td
,
637 struct ctf_file_stream
*file_stream
)
639 struct ctf_stream_class
*stream
;
641 struct ctf_stream_pos
*pos
;
642 struct stat filestats
;
643 struct packet_index packet_index
;
644 int first_packet
= 1;
647 pos
= &file_stream
->pos
;
649 ret
= fstat(pos
->fd
, &filestats
);
653 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
654 uint64_t stream_id
= 0;
658 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
660 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
666 /* map new base. Need mapping length from header. */
667 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
668 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
669 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
670 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
671 pos
->offset
= 0; /* Position of the packet header */
673 packet_index
.offset
= pos
->mmap_offset
;
674 packet_index
.content_size
= 0;
675 packet_index
.packet_size
= 0;
677 /* read and check header, set stream id (and check) */
678 if (td
->packet_header
) {
679 /* Read packet header */
680 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
683 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
684 if (len_index
>= 0) {
685 struct definition_integer
*defint
;
686 struct definition
*field
;
688 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
689 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
690 defint
= container_of(field
, struct definition_integer
, p
);
691 assert(defint
->declaration
->signedness
== FALSE
);
692 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
693 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
694 defint
->value
._unsigned
,
695 file_stream
->pos
.packet_index
->len
,
696 (ssize_t
) pos
->mmap_offset
);
702 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
703 if (len_index
>= 0) {
704 struct definition_array
*defarray
;
705 struct definition
*field
;
707 uint8_t uuidval
[UUID_LEN
];
709 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
710 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
711 defarray
= container_of(field
, struct definition_array
, p
);
712 assert(array_len(defarray
) == UUID_LEN
);
713 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
715 for (i
= 0; i
< UUID_LEN
; i
++) {
716 struct definition
*elem
;
717 struct definition_integer
*defint
;
719 elem
= array_index(defarray
, i
);
721 defint
= container_of(elem
, struct definition_integer
, p
);
722 uuidval
[i
] = defint
->value
._unsigned
;
724 ret
= uuid_compare(td
->uuid
, uuidval
);
726 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
732 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
733 if (len_index
>= 0) {
734 struct definition_integer
*defint
;
735 struct definition
*field
;
737 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
738 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
739 defint
= container_of(field
, struct definition_integer
, p
);
740 assert(defint
->declaration
->signedness
== FALSE
);
741 stream_id
= defint
->value
._unsigned
;
745 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
746 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
750 file_stream
->stream_id
= stream_id
;
751 if (stream_id
>= td
->streams
->len
) {
752 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
755 stream
= g_ptr_array_index(td
->streams
, stream_id
);
757 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
760 file_stream
->stream
.stream_class
= stream
;
764 if (stream
->packet_context
) {
765 /* Read packet context */
766 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
769 /* read content size from header */
770 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
771 if (len_index
>= 0) {
772 struct definition_integer
*defint
;
773 struct definition
*field
;
775 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
776 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
777 defint
= container_of(field
, struct definition_integer
, p
);
778 assert(defint
->declaration
->signedness
== FALSE
);
779 packet_index
.content_size
= defint
->value
._unsigned
;
781 /* Use file size for packet size */
782 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
785 /* read packet size from header */
786 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
787 if (len_index
>= 0) {
788 struct definition_integer
*defint
;
789 struct definition
*field
;
791 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
792 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
793 defint
= container_of(field
, struct definition_integer
, p
);
794 assert(defint
->declaration
->signedness
== FALSE
);
795 packet_index
.packet_size
= defint
->value
._unsigned
;
797 /* Use content size if non-zero, else file size */
798 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
801 /* Use file size for packet size */
802 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
803 /* Use content size if non-zero, else file size */
804 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
807 /* Validate content size and packet size values */
808 if (packet_index
.content_size
> packet_index
.packet_size
) {
809 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
810 packet_index
.content_size
, packet_index
.packet_size
);
814 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
815 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
816 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
820 /* Save position after header and context */
821 packet_index
.data_offset
= pos
->offset
;
823 /* add index to packet array */
824 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
826 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
829 /* Move pos back to beginning of file */
830 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
836 * Note: many file streams can inherit from the same stream class
837 * description (metadata).
840 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
843 struct ctf_file_stream
*file_stream
;
845 ret
= openat(td
->dirfd
, path
, flags
);
848 file_stream
= g_new0(struct ctf_file_stream
, 1);
849 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
850 ret
= create_stream_packet_index(td
, file_stream
);
853 /* Add stream file to stream class */
854 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
858 ctf_fini_pos(&file_stream
->pos
);
859 close(file_stream
->pos
.fd
);
866 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
869 struct dirent
*dirent
;
870 struct dirent
*diriter
;
875 /* Open trace directory */
876 td
->dir
= opendir(path
);
878 fprintf(stdout
, "[error] Unable to open trace directory.\n");
883 td
->dirfd
= open(path
, 0);
885 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
891 * Keep the metadata file separate.
894 ret
= ctf_open_trace_metadata_read(td
);
900 * Open each stream: for each file, try to open, check magic
901 * number, and get the stream ID to add to the right location in
905 dirent_len
= offsetof(struct dirent
, d_name
) +
906 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
908 dirent
= malloc(dirent_len
);
911 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
913 fprintf(stdout
, "[error] Readdir error.\n");
918 /* Ignore hidden files, ., .. and metadata. */
919 if (!strncmp(diriter
->d_name
, ".", 1)
920 || !strcmp(diriter
->d_name
, "..")
921 || !strcmp(diriter
->d_name
, "metadata"))
923 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
925 fprintf(stdout
, "[error] Open file stream error.\n");
944 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
946 struct ctf_trace
*td
;
949 td
= g_new0(struct ctf_trace
, 1);
951 switch (flags
& O_ACCMODE
) {
954 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
957 ret
= ctf_open_trace_read(td
, path
, flags
);
962 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
965 fprintf(stdout
, "[error] Incorrect open flags.\n");
976 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
978 ctf_fini_pos(&file_stream
->pos
);
979 close(file_stream
->pos
.fd
);
983 void ctf_close_trace(struct trace_descriptor
*tdp
)
985 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
989 for (i
= 0; i
< td
->streams
->len
; i
++) {
990 struct ctf_stream_class
*stream
;
993 stream
= g_ptr_array_index(td
->streams
, i
);
996 for (j
= 0; j
< stream
->files
->len
; j
++) {
997 struct ctf_file_stream
*file_stream
;
998 file_stream
= g_ptr_array_index(stream
->files
, j
);
999 ctf_close_file_stream(file_stream
);
1003 g_ptr_array_free(td
->streams
, TRUE
);
1009 void __attribute__((constructor
)) ctf_init(void)
1013 ctf_format
.name
= g_quark_from_static_string("ctf");
1014 ret
= bt_register_format(&ctf_format
);
1018 /* TODO: finalize */