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_read
,
67 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
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_write
,
79 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
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
;
98 if (pos
->offset
== EOF
)
101 /* Read event header */
102 if (stream_class
->event_header
) {
103 struct definition_integer
*integer_definition
;
105 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
108 /* lookup event id */
109 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
110 if (integer_definition
) {
111 id
= integer_definition
->value
._unsigned
;
113 struct definition_enum
*enum_definition
;
115 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
116 if (enum_definition
) {
117 id
= enum_definition
->integer
->value
._unsigned
;
121 /* lookup timestamp */
122 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "timestamp", FALSE
);
123 if (integer_definition
) {
124 stream
->timestamp
= integer_definition
->value
._unsigned
;
126 struct definition
*definition
;
128 definition
= lookup_variant(&stream_class
->event_header
->p
, "v");
130 integer_definition
= lookup_integer(definition
, "id", FALSE
);
131 if (integer_definition
) {
132 id
= integer_definition
->value
._unsigned
;
134 integer_definition
= lookup_integer(definition
, "timestamp", FALSE
);
135 if (integer_definition
) {
136 stream
->timestamp
= integer_definition
->value
._unsigned
;
142 /* Read stream-declared event context */
143 if (stream_class
->event_context
) {
144 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
149 if (id
>= stream_class
->events_by_id
->len
) {
150 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
153 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
155 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
159 /* Read event-declared event context */
160 if (event_class
->context
) {
161 ret
= generic_rw(ppos
, &event_class
->context
->p
);
166 /* Read event payload */
167 if (event_class
->fields
) {
168 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
176 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
181 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
183 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
184 struct ctf_event
*event_class
;
189 /* print event header */
190 if (stream_class
->event_header
) {
191 /* lookup event id */
192 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
193 g_quark_from_static_string("id"));
194 if (len_index
>= 0) {
195 struct definition_integer
*defint
;
196 struct definition
*field
;
198 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
199 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
200 defint
= container_of(field
, struct definition_integer
, p
);
201 assert(defint
->declaration
->signedness
== FALSE
);
202 id
= defint
->value
._unsigned
; /* set id */
205 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
210 /* print stream-declared event context */
211 if (stream_class
->event_context
) {
212 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
217 if (id
>= stream_class
->events_by_id
->len
) {
218 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
221 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
223 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
227 /* print event-declared event context */
228 if (event_class
->context
) {
229 ret
= generic_rw(pos
, &event_class
->context
->p
);
234 /* Read and print event payload */
235 if (event_class
->fields
) {
236 ret
= generic_rw(pos
, &event_class
->fields
->p
);
244 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
248 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
251 pos
->mmap_offset
= 0;
252 pos
->packet_size
= 0;
253 pos
->content_size
= 0;
254 pos
->content_size_loc
= NULL
;
260 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
261 sizeof(struct packet_index
));
263 pos
->packet_index
= NULL
;
264 switch (open_flags
& O_ACCMODE
) {
266 pos
->prot
= PROT_READ
;
267 pos
->flags
= MAP_PRIVATE
;
268 pos
->parent
.rw_table
= read_dispatch_table
;
269 pos
->parent
.event_cb
= ctf_read_event
;
272 pos
->prot
= PROT_WRITE
; /* Write has priority */
273 pos
->flags
= MAP_SHARED
;
274 pos
->parent
.rw_table
= write_dispatch_table
;
275 pos
->parent
.event_cb
= ctf_write_event
;
277 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
284 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
288 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
289 *pos
->content_size_loc
= pos
->offset
;
292 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
294 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
299 (void) g_array_free(pos
->packet_index
, TRUE
);
302 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
306 struct packet_index
*index
;
308 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
309 *pos
->content_size_loc
= pos
->offset
;
313 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
315 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
323 * The caller should never ask for ctf_move_pos across packets,
324 * except to get exactly at the beginning of the next packet.
326 if (pos
->prot
== PROT_WRITE
) {
329 /* The writer will add padding */
330 assert(pos
->offset
+ offset
== pos
->packet_size
);
331 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
334 assert(offset
== 0); /* only seek supported for now */
340 pos
->content_size
= -1U; /* Unknown at this point */
341 pos
->packet_size
= WRITE_PACKET_LEN
;
342 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
343 pos
->packet_size
/ CHAR_BIT
);
349 /* The reader will expect us to skip padding */
350 assert(pos
->offset
+ offset
== pos
->content_size
);
354 assert(offset
== 0); /* only seek supported for now */
360 if (pos
->cur_index
>= pos
->packet_index
->len
) {
364 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
366 pos
->mmap_offset
= index
->offset
;
368 /* Lookup context/packet size in index */
369 pos
->content_size
= index
->content_size
;
370 pos
->packet_size
= index
->packet_size
;
371 pos
->offset
= index
->data_offset
;
373 /* map new base. Need mapping length from header. */
374 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
375 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
376 if (pos
->base
== MAP_FAILED
) {
377 fprintf(stdout
, "[error] mmap error %s.\n",
384 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
390 len
= fread(&magic
, sizeof(magic
), 1, fp
);
394 if (magic
== TSDL_MAGIC
) {
396 td
->byte_order
= BYTE_ORDER
;
397 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
399 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
400 LITTLE_ENDIAN
: BIG_ENDIAN
;
402 CTF_TRACE_SET_FIELD(td
, byte_order
);
409 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
412 struct metadata_packet_header header
;
413 size_t readlen
, writelen
, toread
;
417 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
421 if (td
->byte_order
!= BYTE_ORDER
) {
422 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
423 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
424 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
425 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
428 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
429 if (header
.compression_scheme
) {
430 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
431 header
.compression_scheme
);
434 if (header
.encryption_scheme
) {
435 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
436 header
.encryption_scheme
);
439 if (header
.checksum_scheme
) {
440 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
441 header
.checksum_scheme
);
444 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
445 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
446 CTF_TRACE_SET_FIELD(td
, uuid
);
448 if (uuid_compare(header
.uuid
, td
->uuid
))
452 toread
= header
.content_size
/ CHAR_BIT
;
455 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
460 printf("read %s\n", buf
);
461 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
462 if (writelen
< readlen
) {
472 ret
= 0; /* continue reading next packet */
480 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
488 out
= open_memstream(buf
, &size
);
493 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
502 fclose(out
); /* flush the buffer */
504 /* open for reading */
505 *fp
= fmemopen(*buf
, size
, "rb");
510 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
512 struct ctf_scanner
*scanner
;
517 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
518 if (td
->metadata
.pos
.fd
< 0) {
519 fprintf(stdout
, "Unable to open metadata.\n");
520 return td
->metadata
.pos
.fd
;
523 if (babeltrace_debug
)
526 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
528 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
533 if (packet_metadata(td
, fp
)) {
534 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
536 goto end_packet_read
;
539 scanner
= ctf_scanner_alloc(fp
);
541 fprintf(stdout
, "[error] Error allocating scanner\n");
543 goto end_scanner_alloc
;
545 ret
= ctf_scanner_append_ast(scanner
);
547 fprintf(stdout
, "[error] Error creating AST\n");
551 if (babeltrace_debug
) {
552 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
554 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
559 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
561 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
564 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
567 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
571 ctf_scanner_free(scanner
);
577 close(td
->metadata
.pos
.fd
);
583 int create_stream_packet_index(struct ctf_trace
*td
,
584 struct ctf_file_stream
*file_stream
)
586 struct ctf_stream_class
*stream
;
588 struct ctf_stream_pos
*pos
;
589 struct stat filestats
;
590 struct packet_index packet_index
;
591 int first_packet
= 1;
594 pos
= &file_stream
->pos
;
596 ret
= fstat(pos
->fd
, &filestats
);
600 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
601 uint64_t stream_id
= 0;
605 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
607 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
613 /* map new base. Need mapping length from header. */
614 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
615 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
616 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
617 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
618 pos
->offset
= 0; /* Position of the packet header */
620 packet_index
.offset
= pos
->mmap_offset
;
621 packet_index
.content_size
= 0;
622 packet_index
.packet_size
= 0;
624 /* read and check header, set stream id (and check) */
625 if (td
->packet_header
) {
626 /* Read packet header */
627 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
630 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
631 if (len_index
>= 0) {
632 struct definition_integer
*defint
;
633 struct definition
*field
;
635 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
636 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
637 defint
= container_of(field
, struct definition_integer
, p
);
638 assert(defint
->declaration
->signedness
== FALSE
);
639 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
640 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
641 defint
->value
._unsigned
,
642 file_stream
->pos
.packet_index
->len
,
643 (ssize_t
) pos
->mmap_offset
);
649 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
650 if (len_index
>= 0) {
651 struct definition_array
*defarray
;
652 struct definition
*field
;
654 uint8_t uuidval
[UUID_LEN
];
656 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
657 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
658 defarray
= container_of(field
, struct definition_array
, p
);
659 assert(array_len(defarray
) == UUID_LEN
);
660 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
662 for (i
= 0; i
< UUID_LEN
; i
++) {
663 struct definition
*elem
;
664 struct definition_integer
*defint
;
666 elem
= array_index(defarray
, i
);
668 defint
= container_of(elem
, struct definition_integer
, p
);
669 uuidval
[i
] = defint
->value
._unsigned
;
671 ret
= uuid_compare(td
->uuid
, uuidval
);
673 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
679 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
680 if (len_index
>= 0) {
681 struct definition_integer
*defint
;
682 struct definition
*field
;
684 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
685 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
686 defint
= container_of(field
, struct definition_integer
, p
);
687 assert(defint
->declaration
->signedness
== FALSE
);
688 stream_id
= defint
->value
._unsigned
;
692 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
693 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
697 file_stream
->stream_id
= stream_id
;
698 if (stream_id
>= td
->streams
->len
) {
699 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
702 stream
= g_ptr_array_index(td
->streams
, stream_id
);
704 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
707 file_stream
->stream
.stream_class
= stream
;
711 if (stream
->packet_context
) {
712 /* Read packet context */
713 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
716 /* read content size from header */
717 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
718 if (len_index
>= 0) {
719 struct definition_integer
*defint
;
720 struct definition
*field
;
722 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
723 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
724 defint
= container_of(field
, struct definition_integer
, p
);
725 assert(defint
->declaration
->signedness
== FALSE
);
726 packet_index
.content_size
= defint
->value
._unsigned
;
728 /* Use file size for packet size */
729 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
732 /* read packet size from header */
733 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
734 if (len_index
>= 0) {
735 struct definition_integer
*defint
;
736 struct definition
*field
;
738 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
739 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
740 defint
= container_of(field
, struct definition_integer
, p
);
741 assert(defint
->declaration
->signedness
== FALSE
);
742 packet_index
.packet_size
= defint
->value
._unsigned
;
744 /* Use content size if non-zero, else file size */
745 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
748 /* Use file size for packet size */
749 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
750 /* Use content size if non-zero, else file size */
751 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
754 /* Validate content size and packet size values */
755 if (packet_index
.content_size
> packet_index
.packet_size
) {
756 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
757 packet_index
.content_size
, packet_index
.packet_size
);
761 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
762 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
763 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
767 /* Save position after header and context */
768 packet_index
.data_offset
= pos
->offset
;
770 /* add index to packet array */
771 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
773 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
776 /* Move pos back to beginning of file */
777 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
783 * Note: many file streams can inherit from the same stream class
784 * description (metadata).
787 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
790 struct ctf_file_stream
*file_stream
;
792 ret
= openat(td
->dirfd
, path
, flags
);
795 file_stream
= g_new0(struct ctf_file_stream
, 1);
796 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
797 ret
= create_stream_packet_index(td
, file_stream
);
800 /* Add stream file to stream class */
801 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
805 ctf_fini_pos(&file_stream
->pos
);
806 close(file_stream
->pos
.fd
);
813 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
816 struct dirent
*dirent
;
817 struct dirent
*diriter
;
822 /* Open trace directory */
823 td
->dir
= opendir(path
);
825 fprintf(stdout
, "[error] Unable to open trace directory.\n");
830 td
->dirfd
= open(path
, 0);
832 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
838 * Keep the metadata file separate.
841 ret
= ctf_open_trace_metadata_read(td
);
847 * Open each stream: for each file, try to open, check magic
848 * number, and get the stream ID to add to the right location in
852 dirent_len
= offsetof(struct dirent
, d_name
) +
853 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
855 dirent
= malloc(dirent_len
);
858 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
860 fprintf(stdout
, "[error] Readdir error.\n");
865 /* Ignore hidden files, ., .. and metadata. */
866 if (!strncmp(diriter
->d_name
, ".", 1)
867 || !strcmp(diriter
->d_name
, "..")
868 || !strcmp(diriter
->d_name
, "metadata"))
870 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
872 fprintf(stdout
, "[error] Open file stream error.\n");
890 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
892 struct ctf_trace
*td
;
895 td
= g_new0(struct ctf_trace
, 1);
897 switch (flags
& O_ACCMODE
) {
900 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
903 ret
= ctf_open_trace_read(td
, path
, flags
);
908 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
911 fprintf(stdout
, "[error] Incorrect open flags.\n");
922 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
924 ctf_fini_pos(&file_stream
->pos
);
925 close(file_stream
->pos
.fd
);
928 void ctf_close_trace(struct trace_descriptor
*tdp
)
930 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
934 for (i
= 0; i
< td
->streams
->len
; i
++) {
935 struct ctf_stream_class
*stream
;
937 stream
= g_ptr_array_index(td
->streams
, i
);
938 for (j
= 0; j
< stream
->files
->len
; j
++) {
939 struct ctf_file_stream
*file_stream
;
940 file_stream
= g_ptr_array_index(stream
->files
, j
);
941 ctf_close_file_stream(file_stream
);
945 g_ptr_array_free(td
->streams
, TRUE
);
951 void __attribute__((constructor
)) ctf_init(void)
955 ctf_format
.name
= g_quark_from_static_string("ctf");
956 ret
= bt_register_format(&ctf_format
);