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))
55 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
56 void ctf_close_trace(struct trace_descriptor
*descriptor
);
59 rw_dispatch read_dispatch_table
[] = {
60 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
61 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
62 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
63 [ CTF_TYPE_STRING
] = ctf_string_read
,
64 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
65 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
66 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
67 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
71 rw_dispatch write_dispatch_table
[] = {
72 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
73 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
74 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
75 [ CTF_TYPE_STRING
] = ctf_string_write
,
76 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
77 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
78 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
79 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
83 struct format ctf_format
= {
84 .open_trace
= ctf_open_trace
,
85 .close_trace
= ctf_close_trace
,
89 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
91 struct ctf_stream_pos
*pos
=
92 container_of(ppos
, struct ctf_stream_pos
, parent
);
93 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
94 struct ctf_event
*event_class
;
99 if (pos
->offset
== EOF
)
102 /* Read event header */
103 if (stream_class
->event_header
) {
104 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
107 /* lookup event id */
108 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
109 g_quark_from_static_string("id"));
110 if (len_index
>= 0) {
111 struct definition_integer
*defint
;
112 struct definition
*field
;
114 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
115 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
116 defint
= container_of(field
, struct definition_integer
, p
);
117 assert(defint
->declaration
->signedness
== FALSE
);
118 id
= defint
->value
._unsigned
; /* set id */
121 /* lookup timestamp */
122 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
123 g_quark_from_static_string("timestamp"));
124 if (len_index
>= 0) {
125 struct definition_integer
*defint
;
126 struct definition
*field
;
128 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
129 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
130 defint
= container_of(field
, struct definition_integer
, p
);
131 assert(defint
->declaration
->signedness
== FALSE
);
132 /* update timestamp */
133 stream
->timestamp
= defint
->value
._unsigned
;
138 /* Read stream-declared event context */
139 if (stream_class
->event_context
) {
140 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
145 if (id
>= stream_class
->events_by_id
->len
) {
146 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
149 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
151 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
155 /* Read event-declared event context */
156 if (event_class
->context
) {
157 ret
= generic_rw(ppos
, &event_class
->context
->p
);
162 /* Read event payload */
163 if (event_class
->fields
) {
164 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
172 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
177 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
179 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
180 struct ctf_event
*event_class
;
185 /* print event header */
186 if (stream_class
->event_header
) {
187 /* lookup event id */
188 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
189 g_quark_from_static_string("id"));
190 if (len_index
>= 0) {
191 struct definition_integer
*defint
;
192 struct definition
*field
;
194 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
195 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
196 defint
= container_of(field
, struct definition_integer
, p
);
197 assert(defint
->declaration
->signedness
== FALSE
);
198 id
= defint
->value
._unsigned
; /* set id */
201 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
206 /* print stream-declared event context */
207 if (stream_class
->event_context
) {
208 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
213 if (id
>= stream_class
->events_by_id
->len
) {
214 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
217 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
219 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
223 /* print event-declared event context */
224 if (event_class
->context
) {
225 ret
= generic_rw(pos
, &event_class
->context
->p
);
230 /* Read and print event payload */
231 if (event_class
->fields
) {
232 ret
= generic_rw(pos
, &event_class
->fields
->p
);
240 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
244 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
247 pos
->mmap_offset
= 0;
248 pos
->packet_size
= 0;
249 pos
->content_size
= 0;
250 pos
->content_size_loc
= NULL
;
256 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
257 sizeof(struct packet_index
));
259 pos
->packet_index
= NULL
;
260 switch (open_flags
& O_ACCMODE
) {
262 pos
->prot
= PROT_READ
;
263 pos
->flags
= MAP_PRIVATE
;
264 pos
->parent
.rw_table
= read_dispatch_table
;
265 pos
->parent
.event_cb
= ctf_read_event
;
268 pos
->prot
= PROT_WRITE
; /* Write has priority */
269 pos
->flags
= MAP_SHARED
;
270 pos
->parent
.rw_table
= write_dispatch_table
;
271 pos
->parent
.event_cb
= ctf_write_event
;
273 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
280 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
284 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
285 *pos
->content_size_loc
= pos
->offset
;
288 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
290 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
295 (void) g_array_free(pos
->packet_index
, TRUE
);
298 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
302 struct packet_index
*index
;
304 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
305 *pos
->content_size_loc
= pos
->offset
;
309 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
311 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
319 * The caller should never ask for ctf_move_pos across packets,
320 * except to get exactly at the beginning of the next packet.
322 if (pos
->prot
== PROT_WRITE
) {
325 /* The writer will add padding */
326 assert(pos
->offset
+ offset
== pos
->packet_size
);
327 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
330 assert(offset
== 0); /* only seek supported for now */
336 pos
->content_size
= -1U; /* Unknown at this point */
337 pos
->packet_size
= WRITE_PACKET_LEN
;
338 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
339 pos
->packet_size
/ CHAR_BIT
);
345 /* The reader will expect us to skip padding */
346 assert(pos
->offset
+ offset
== pos
->content_size
);
350 assert(offset
== 0); /* only seek supported for now */
356 if (pos
->cur_index
>= pos
->packet_index
->len
) {
360 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
362 pos
->mmap_offset
= index
->offset
;
364 /* Lookup context/packet size in index */
365 pos
->content_size
= index
->content_size
;
366 pos
->packet_size
= index
->packet_size
;
367 pos
->offset
= index
->data_offset
;
369 /* map new base. Need mapping length from header. */
370 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
371 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
372 if (pos
->base
== MAP_FAILED
) {
373 fprintf(stdout
, "[error] mmap error %s.\n",
380 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
386 len
= fread(&magic
, sizeof(magic
), 1, fp
);
390 if (magic
== TSDL_MAGIC
) {
392 td
->byte_order
= BYTE_ORDER
;
393 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
395 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
396 LITTLE_ENDIAN
: BIG_ENDIAN
;
398 CTF_TRACE_SET_FIELD(td
, byte_order
);
405 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
408 struct metadata_packet_header header
;
409 size_t readlen
, writelen
, toread
;
413 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
417 if (td
->byte_order
!= BYTE_ORDER
) {
418 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
419 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
420 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
421 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
424 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
425 if (header
.compression_scheme
) {
426 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
427 header
.compression_scheme
);
430 if (header
.encryption_scheme
) {
431 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
432 header
.encryption_scheme
);
435 if (header
.checksum_scheme
) {
436 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
437 header
.checksum_scheme
);
440 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
441 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
442 CTF_TRACE_SET_FIELD(td
, uuid
);
444 if (uuid_compare(header
.uuid
, td
->uuid
))
448 toread
= header
.content_size
/ CHAR_BIT
;
451 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
456 printf("read %s\n", buf
);
457 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
458 if (writelen
< readlen
) {
468 ret
= 0; /* continue reading next packet */
476 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
484 out
= open_memstream(buf
, &size
);
489 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
498 fclose(out
); /* flush the buffer */
500 /* open for reading */
501 *fp
= fmemopen(*buf
, size
, "rb");
506 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
508 struct ctf_scanner
*scanner
;
513 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
514 if (td
->metadata
.pos
.fd
< 0) {
515 fprintf(stdout
, "Unable to open metadata.\n");
516 return td
->metadata
.pos
.fd
;
519 if (babeltrace_debug
)
522 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
524 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
529 if (packet_metadata(td
, fp
)) {
530 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
532 goto end_packet_read
;
535 scanner
= ctf_scanner_alloc(fp
);
537 fprintf(stdout
, "[error] Error allocating scanner\n");
539 goto end_scanner_alloc
;
541 ret
= ctf_scanner_append_ast(scanner
);
543 fprintf(stdout
, "[error] Error creating AST\n");
547 if (babeltrace_debug
) {
548 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
550 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
555 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
557 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
560 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
563 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
567 ctf_scanner_free(scanner
);
573 close(td
->metadata
.pos
.fd
);
579 int create_stream_packet_index(struct ctf_trace
*td
,
580 struct ctf_file_stream
*file_stream
)
582 struct ctf_stream_class
*stream
;
584 struct ctf_stream_pos
*pos
;
585 struct stat filestats
;
586 struct packet_index packet_index
;
587 int first_packet
= 1;
590 pos
= &file_stream
->pos
;
592 ret
= fstat(pos
->fd
, &filestats
);
596 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
597 uint64_t stream_id
= 0;
601 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
603 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
609 /* map new base. Need mapping length from header. */
610 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
611 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
612 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
613 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
614 pos
->offset
= 0; /* Position of the packet header */
616 packet_index
.offset
= pos
->mmap_offset
;
617 packet_index
.content_size
= 0;
618 packet_index
.packet_size
= 0;
620 /* read and check header, set stream id (and check) */
621 if (td
->packet_header
) {
622 /* Read packet header */
623 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
626 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
627 if (len_index
>= 0) {
628 struct definition_integer
*defint
;
629 struct definition
*field
;
631 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
632 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
633 defint
= container_of(field
, struct definition_integer
, p
);
634 assert(defint
->declaration
->signedness
== FALSE
);
635 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
636 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
637 defint
->value
._unsigned
,
638 file_stream
->pos
.packet_index
->len
,
639 (ssize_t
) pos
->mmap_offset
);
645 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
646 if (len_index
>= 0) {
647 struct definition_array
*defarray
;
648 struct definition
*field
;
650 uint8_t uuidval
[UUID_LEN
];
652 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
653 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
654 defarray
= container_of(field
, struct definition_array
, p
);
655 assert(array_len(defarray
) == UUID_LEN
);
656 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
658 for (i
= 0; i
< UUID_LEN
; i
++) {
659 struct definition
*elem
;
660 struct definition_integer
*defint
;
662 elem
= array_index(defarray
, i
);
664 defint
= container_of(elem
, struct definition_integer
, p
);
665 uuidval
[i
] = defint
->value
._unsigned
;
667 ret
= uuid_compare(td
->uuid
, uuidval
);
669 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
675 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
676 if (len_index
>= 0) {
677 struct definition_integer
*defint
;
678 struct definition
*field
;
680 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
681 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
682 defint
= container_of(field
, struct definition_integer
, p
);
683 assert(defint
->declaration
->signedness
== FALSE
);
684 stream_id
= defint
->value
._unsigned
;
688 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
689 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
693 file_stream
->stream_id
= stream_id
;
694 if (stream_id
>= td
->streams
->len
) {
695 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
698 stream
= g_ptr_array_index(td
->streams
, stream_id
);
700 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
703 file_stream
->stream
.stream_class
= stream
;
707 if (stream
->packet_context
) {
708 /* Read packet context */
709 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
712 /* read content size from header */
713 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
714 if (len_index
>= 0) {
715 struct definition_integer
*defint
;
716 struct definition
*field
;
718 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
719 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
720 defint
= container_of(field
, struct definition_integer
, p
);
721 assert(defint
->declaration
->signedness
== FALSE
);
722 packet_index
.content_size
= defint
->value
._unsigned
;
724 /* Use file size for packet size */
725 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
728 /* read packet size from header */
729 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
730 if (len_index
>= 0) {
731 struct definition_integer
*defint
;
732 struct definition
*field
;
734 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
735 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
736 defint
= container_of(field
, struct definition_integer
, p
);
737 assert(defint
->declaration
->signedness
== FALSE
);
738 packet_index
.packet_size
= defint
->value
._unsigned
;
740 /* Use content size if non-zero, else file size */
741 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
744 /* Use file size for packet size */
745 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
746 /* Use content size if non-zero, else file size */
747 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
750 /* Validate content size and packet size values */
751 if (packet_index
.content_size
> packet_index
.packet_size
) {
752 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
753 packet_index
.content_size
, packet_index
.packet_size
);
757 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
758 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
759 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
763 /* Save position after header and context */
764 packet_index
.data_offset
= pos
->offset
;
766 /* add index to packet array */
767 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
769 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
772 /* Move pos back to beginning of file */
773 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
779 * Note: many file streams can inherit from the same stream class
780 * description (metadata).
783 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
786 struct ctf_file_stream
*file_stream
;
788 ret
= openat(td
->dirfd
, path
, flags
);
791 file_stream
= g_new0(struct ctf_file_stream
, 1);
792 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
793 ret
= create_stream_packet_index(td
, file_stream
);
796 /* Add stream file to stream class */
797 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
801 ctf_fini_pos(&file_stream
->pos
);
802 close(file_stream
->pos
.fd
);
809 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
812 struct dirent
*dirent
;
813 struct dirent
*diriter
;
818 /* Open trace directory */
819 td
->dir
= opendir(path
);
821 fprintf(stdout
, "[error] Unable to open trace directory.\n");
826 td
->dirfd
= open(path
, 0);
828 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
834 * Keep the metadata file separate.
837 ret
= ctf_open_trace_metadata_read(td
);
843 * Open each stream: for each file, try to open, check magic
844 * number, and get the stream ID to add to the right location in
848 dirent_len
= offsetof(struct dirent
, d_name
) +
849 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
851 dirent
= malloc(dirent_len
);
854 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
856 fprintf(stdout
, "[error] Readdir error.\n");
861 /* Ignore hidden files, ., .. and metadata. */
862 if (!strncmp(diriter
->d_name
, ".", 1)
863 || !strcmp(diriter
->d_name
, "..")
864 || !strcmp(diriter
->d_name
, "metadata"))
866 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
868 fprintf(stdout
, "[error] Open file stream error.\n");
886 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
888 struct ctf_trace
*td
;
891 td
= g_new0(struct ctf_trace
, 1);
893 switch (flags
& O_ACCMODE
) {
896 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
899 ret
= ctf_open_trace_read(td
, path
, flags
);
904 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
907 fprintf(stdout
, "[error] Incorrect open flags.\n");
918 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
920 ctf_fini_pos(&file_stream
->pos
);
921 close(file_stream
->pos
.fd
);
924 void ctf_close_trace(struct trace_descriptor
*tdp
)
926 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
930 for (i
= 0; i
< td
->streams
->len
; i
++) {
931 struct ctf_stream_class
*stream
;
933 stream
= g_ptr_array_index(td
->streams
, i
);
934 for (j
= 0; j
< stream
->files
->len
; j
++) {
935 struct ctf_file_stream
*file_stream
;
936 file_stream
= g_ptr_array_index(stream
->files
, j
);
937 ctf_close_file_stream(file_stream
);
941 g_ptr_array_free(td
->streams
, TRUE
);
947 void __attribute__((constructor
)) ctf_init(void)
951 ctf_format
.name
= g_quark_from_static_string("ctf");
952 ret
= bt_register_format(&ctf_format
);