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 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
93 struct ctf_stream_pos
*pos
=
94 container_of(ppos
, struct ctf_stream_pos
, parent
);
95 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
96 struct ctf_event
*event_class
;
100 if (pos
->offset
== EOF
)
103 /* Read event header */
104 if (stream_class
->event_header
) {
105 struct definition_integer
*integer_definition
;
106 struct definition
*variant
;
108 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
111 /* lookup event id */
112 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
113 if (integer_definition
) {
114 id
= integer_definition
->value
._unsigned
;
116 struct definition_enum
*enum_definition
;
118 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
119 if (enum_definition
) {
120 id
= enum_definition
->integer
->value
._unsigned
;
124 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
126 integer_definition
= lookup_integer(variant
, "id", FALSE
);
127 if (integer_definition
) {
128 id
= integer_definition
->value
._unsigned
;
132 /* lookup timestamp */
133 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "timestamp", FALSE
);
134 if (integer_definition
) {
135 stream
->timestamp
= integer_definition
->value
._unsigned
;
138 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
139 if (integer_definition
) {
140 stream
->timestamp
= integer_definition
->value
._unsigned
;
146 /* Read stream-declared event context */
147 if (stream_class
->event_context
) {
148 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
153 if (id
>= stream_class
->events_by_id
->len
) {
154 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
157 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
159 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
163 /* Read event-declared event context */
164 if (event_class
->context
) {
165 ret
= generic_rw(ppos
, &event_class
->context
->p
);
170 /* Read event payload */
171 if (event_class
->fields
) {
172 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
180 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
185 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
187 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
188 struct ctf_event
*event_class
;
192 /* print event header */
193 if (stream_class
->event_header
) {
194 struct definition_integer
*integer_definition
;
195 struct definition
*variant
;
197 /* lookup event id */
198 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
199 if (integer_definition
) {
200 id
= integer_definition
->value
._unsigned
;
202 struct definition_enum
*enum_definition
;
204 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
205 if (enum_definition
) {
206 id
= enum_definition
->integer
->value
._unsigned
;
210 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
212 integer_definition
= lookup_integer(variant
, "id", FALSE
);
213 if (integer_definition
) {
214 id
= integer_definition
->value
._unsigned
;
218 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
223 /* print stream-declared event context */
224 if (stream_class
->event_context
) {
225 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
230 if (id
>= stream_class
->events_by_id
->len
) {
231 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
234 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
236 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
240 /* print event-declared event context */
241 if (event_class
->context
) {
242 ret
= generic_rw(pos
, &event_class
->context
->p
);
247 /* Read and print event payload */
248 if (event_class
->fields
) {
249 ret
= generic_rw(pos
, &event_class
->fields
->p
);
257 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
261 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
264 pos
->mmap_offset
= 0;
265 pos
->packet_size
= 0;
266 pos
->content_size
= 0;
267 pos
->content_size_loc
= NULL
;
273 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
274 sizeof(struct packet_index
));
276 pos
->packet_index
= NULL
;
277 switch (open_flags
& O_ACCMODE
) {
279 pos
->prot
= PROT_READ
;
280 pos
->flags
= MAP_PRIVATE
;
281 pos
->parent
.rw_table
= read_dispatch_table
;
282 pos
->parent
.event_cb
= ctf_read_event
;
285 pos
->prot
= PROT_WRITE
; /* Write has priority */
286 pos
->flags
= MAP_SHARED
;
287 pos
->parent
.rw_table
= write_dispatch_table
;
288 pos
->parent
.event_cb
= ctf_write_event
;
290 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
297 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
301 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
302 *pos
->content_size_loc
= pos
->offset
;
305 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
307 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
312 (void) g_array_free(pos
->packet_index
, TRUE
);
315 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
319 struct packet_index
*index
;
321 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
322 *pos
->content_size_loc
= pos
->offset
;
326 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
328 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
336 * The caller should never ask for ctf_move_pos across packets,
337 * except to get exactly at the beginning of the next packet.
339 if (pos
->prot
== PROT_WRITE
) {
342 /* The writer will add padding */
343 assert(pos
->offset
+ offset
== pos
->packet_size
);
344 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
347 assert(offset
== 0); /* only seek supported for now */
353 pos
->content_size
= -1U; /* Unknown at this point */
354 pos
->packet_size
= WRITE_PACKET_LEN
;
355 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
356 pos
->packet_size
/ CHAR_BIT
);
362 /* The reader will expect us to skip padding */
363 assert(pos
->offset
+ offset
== pos
->content_size
);
367 assert(offset
== 0); /* only seek supported for now */
373 if (pos
->cur_index
>= pos
->packet_index
->len
) {
377 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
379 pos
->mmap_offset
= index
->offset
;
381 /* Lookup context/packet size in index */
382 pos
->content_size
= index
->content_size
;
383 pos
->packet_size
= index
->packet_size
;
384 pos
->offset
= index
->data_offset
;
386 /* map new base. Need mapping length from header. */
387 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
388 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
389 if (pos
->base
== MAP_FAILED
) {
390 fprintf(stdout
, "[error] mmap error %s.\n",
397 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
403 len
= fread(&magic
, sizeof(magic
), 1, fp
);
407 if (magic
== TSDL_MAGIC
) {
409 td
->byte_order
= BYTE_ORDER
;
410 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
412 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
413 LITTLE_ENDIAN
: BIG_ENDIAN
;
415 CTF_TRACE_SET_FIELD(td
, byte_order
);
422 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
425 struct metadata_packet_header header
;
426 size_t readlen
, writelen
, toread
;
430 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
434 if (td
->byte_order
!= BYTE_ORDER
) {
435 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
436 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
437 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
438 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
441 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
442 if (header
.compression_scheme
) {
443 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
444 header
.compression_scheme
);
447 if (header
.encryption_scheme
) {
448 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
449 header
.encryption_scheme
);
452 if (header
.checksum_scheme
) {
453 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
454 header
.checksum_scheme
);
457 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
458 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
459 CTF_TRACE_SET_FIELD(td
, uuid
);
461 if (uuid_compare(header
.uuid
, td
->uuid
))
465 toread
= header
.content_size
/ CHAR_BIT
;
468 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
473 if (babeltrace_debug
) {
474 fprintf(stdout
, "[debug] metadata packet read: %s\n",
478 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
479 if (writelen
< readlen
) {
489 ret
= 0; /* continue reading next packet */
497 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
506 * Using strlen on *buf instead of size of open_memstream
507 * because its size includes garbage at the end (after final
508 * \0). This is the allocated size, not the actual string size.
510 out
= open_memstream(buf
, &size
);
515 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
524 fclose(out
); /* flush the buffer */
526 /* open for reading */
527 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
532 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
534 struct ctf_scanner
*scanner
;
539 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
540 if (td
->metadata
.pos
.fd
< 0) {
541 fprintf(stdout
, "Unable to open metadata.\n");
542 return td
->metadata
.pos
.fd
;
545 if (babeltrace_debug
)
548 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
550 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
555 if (packet_metadata(td
, fp
)) {
556 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
558 goto end_packet_read
;
561 scanner
= ctf_scanner_alloc(fp
);
563 fprintf(stdout
, "[error] Error allocating scanner\n");
565 goto end_scanner_alloc
;
567 ret
= ctf_scanner_append_ast(scanner
);
569 fprintf(stdout
, "[error] Error creating AST\n");
573 if (babeltrace_debug
) {
574 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
576 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
581 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
583 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
586 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
589 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
593 ctf_scanner_free(scanner
);
599 close(td
->metadata
.pos
.fd
);
605 int create_stream_packet_index(struct ctf_trace
*td
,
606 struct ctf_file_stream
*file_stream
)
608 struct ctf_stream_class
*stream
;
610 struct ctf_stream_pos
*pos
;
611 struct stat filestats
;
612 struct packet_index packet_index
;
613 int first_packet
= 1;
616 pos
= &file_stream
->pos
;
618 ret
= fstat(pos
->fd
, &filestats
);
622 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
623 uint64_t stream_id
= 0;
627 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
629 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
635 /* map new base. Need mapping length from header. */
636 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
637 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
638 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
639 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
640 pos
->offset
= 0; /* Position of the packet header */
642 packet_index
.offset
= pos
->mmap_offset
;
643 packet_index
.content_size
= 0;
644 packet_index
.packet_size
= 0;
646 /* read and check header, set stream id (and check) */
647 if (td
->packet_header
) {
648 /* Read packet header */
649 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
652 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
653 if (len_index
>= 0) {
654 struct definition_integer
*defint
;
655 struct definition
*field
;
657 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
658 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
659 defint
= container_of(field
, struct definition_integer
, p
);
660 assert(defint
->declaration
->signedness
== FALSE
);
661 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
662 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
663 defint
->value
._unsigned
,
664 file_stream
->pos
.packet_index
->len
,
665 (ssize_t
) pos
->mmap_offset
);
671 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
672 if (len_index
>= 0) {
673 struct definition_array
*defarray
;
674 struct definition
*field
;
676 uint8_t uuidval
[UUID_LEN
];
678 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
679 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
680 defarray
= container_of(field
, struct definition_array
, p
);
681 assert(array_len(defarray
) == UUID_LEN
);
682 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
684 for (i
= 0; i
< UUID_LEN
; i
++) {
685 struct definition
*elem
;
686 struct definition_integer
*defint
;
688 elem
= array_index(defarray
, i
);
690 defint
= container_of(elem
, struct definition_integer
, p
);
691 uuidval
[i
] = defint
->value
._unsigned
;
693 ret
= uuid_compare(td
->uuid
, uuidval
);
695 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
701 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
702 if (len_index
>= 0) {
703 struct definition_integer
*defint
;
704 struct definition
*field
;
706 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
707 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
708 defint
= container_of(field
, struct definition_integer
, p
);
709 assert(defint
->declaration
->signedness
== FALSE
);
710 stream_id
= defint
->value
._unsigned
;
714 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
715 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
719 file_stream
->stream_id
= stream_id
;
720 if (stream_id
>= td
->streams
->len
) {
721 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
724 stream
= g_ptr_array_index(td
->streams
, stream_id
);
726 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
729 file_stream
->stream
.stream_class
= stream
;
733 if (stream
->packet_context
) {
734 /* Read packet context */
735 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
738 /* read content size from header */
739 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
740 if (len_index
>= 0) {
741 struct definition_integer
*defint
;
742 struct definition
*field
;
744 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
745 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
746 defint
= container_of(field
, struct definition_integer
, p
);
747 assert(defint
->declaration
->signedness
== FALSE
);
748 packet_index
.content_size
= defint
->value
._unsigned
;
750 /* Use file size for packet size */
751 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
754 /* read packet size from header */
755 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
756 if (len_index
>= 0) {
757 struct definition_integer
*defint
;
758 struct definition
*field
;
760 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
761 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
762 defint
= container_of(field
, struct definition_integer
, p
);
763 assert(defint
->declaration
->signedness
== FALSE
);
764 packet_index
.packet_size
= defint
->value
._unsigned
;
766 /* Use content size if non-zero, else file size */
767 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
770 /* Use file size for packet size */
771 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
772 /* Use content size if non-zero, else file size */
773 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
776 /* Validate content size and packet size values */
777 if (packet_index
.content_size
> packet_index
.packet_size
) {
778 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
779 packet_index
.content_size
, packet_index
.packet_size
);
783 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
784 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
785 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
789 /* Save position after header and context */
790 packet_index
.data_offset
= pos
->offset
;
792 /* add index to packet array */
793 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
795 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
798 /* Move pos back to beginning of file */
799 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
805 * Note: many file streams can inherit from the same stream class
806 * description (metadata).
809 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
812 struct ctf_file_stream
*file_stream
;
814 ret
= openat(td
->dirfd
, path
, flags
);
817 file_stream
= g_new0(struct ctf_file_stream
, 1);
818 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
819 ret
= create_stream_packet_index(td
, file_stream
);
822 /* Add stream file to stream class */
823 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
827 ctf_fini_pos(&file_stream
->pos
);
828 close(file_stream
->pos
.fd
);
835 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
838 struct dirent
*dirent
;
839 struct dirent
*diriter
;
844 /* Open trace directory */
845 td
->dir
= opendir(path
);
847 fprintf(stdout
, "[error] Unable to open trace directory.\n");
852 td
->dirfd
= open(path
, 0);
854 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
860 * Keep the metadata file separate.
863 ret
= ctf_open_trace_metadata_read(td
);
869 * Open each stream: for each file, try to open, check magic
870 * number, and get the stream ID to add to the right location in
874 dirent_len
= offsetof(struct dirent
, d_name
) +
875 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
877 dirent
= malloc(dirent_len
);
880 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
882 fprintf(stdout
, "[error] Readdir error.\n");
887 /* Ignore hidden files, ., .. and metadata. */
888 if (!strncmp(diriter
->d_name
, ".", 1)
889 || !strcmp(diriter
->d_name
, "..")
890 || !strcmp(diriter
->d_name
, "metadata"))
892 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
894 fprintf(stdout
, "[error] Open file stream error.\n");
913 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
915 struct ctf_trace
*td
;
918 td
= g_new0(struct ctf_trace
, 1);
920 switch (flags
& O_ACCMODE
) {
923 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
926 ret
= ctf_open_trace_read(td
, path
, flags
);
931 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
934 fprintf(stdout
, "[error] Incorrect open flags.\n");
945 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
947 ctf_fini_pos(&file_stream
->pos
);
948 close(file_stream
->pos
.fd
);
952 void ctf_close_trace(struct trace_descriptor
*tdp
)
954 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
958 for (i
= 0; i
< td
->streams
->len
; i
++) {
959 struct ctf_stream_class
*stream
;
962 stream
= g_ptr_array_index(td
->streams
, i
);
965 for (j
= 0; j
< stream
->files
->len
; j
++) {
966 struct ctf_file_stream
*file_stream
;
967 file_stream
= g_ptr_array_index(stream
->files
, j
);
968 ctf_close_file_stream(file_stream
);
972 g_ptr_array_free(td
->streams
, TRUE
);
978 void __attribute__((constructor
)) ctf_init(void)
982 ctf_format
.name
= g_quark_from_static_string("ctf");
983 ret
= bt_register_format(&ctf_format
);