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 */
51 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
);
52 void ctf_close_trace(struct trace_descriptor
*descriptor
);
55 rw_dispatch read_dispatch_table
[] = {
56 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
57 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
58 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
59 [ CTF_TYPE_STRING
] = ctf_string_read
,
60 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
61 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
62 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
63 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
67 rw_dispatch write_dispatch_table
[] = {
68 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
69 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
70 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
71 [ CTF_TYPE_STRING
] = ctf_string_write
,
72 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
73 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
74 [ CTF_TYPE_ARRAY
] = ctf_array_rw
,
75 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_rw
,
79 struct format ctf_format
= {
80 .open_trace
= ctf_open_trace
,
81 .close_trace
= ctf_close_trace
,
85 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
87 struct ctf_stream_pos
*pos
=
88 container_of(ppos
, struct ctf_stream_pos
, parent
);
89 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
90 struct ctf_event
*event_class
;
95 if (pos
->offset
== EOF
)
98 /* Read event header */
99 if (stream_class
->event_header
) {
100 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
103 /* lookup event id */
104 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
105 g_quark_from_static_string("id"));
106 if (len_index
>= 0) {
107 struct definition_integer
*defint
;
108 struct definition
*field
;
110 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
111 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
112 defint
= container_of(field
, struct definition_integer
, p
);
113 assert(defint
->declaration
->signedness
== FALSE
);
114 id
= defint
->value
._unsigned
; /* set id */
117 /* lookup timestamp */
118 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
119 g_quark_from_static_string("timestamp"));
120 if (len_index
>= 0) {
121 struct definition_integer
*defint
;
122 struct definition
*field
;
124 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
125 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
126 defint
= container_of(field
, struct definition_integer
, p
);
127 assert(defint
->declaration
->signedness
== FALSE
);
128 /* update timestamp */
129 stream
->timestamp
= defint
->value
._unsigned
;
134 /* Read stream-declared event context */
135 if (stream_class
->event_context
) {
136 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
141 if (id
>= stream_class
->events_by_id
->len
) {
142 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
145 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
147 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
151 /* Read event-declared event context */
152 if (event_class
->context
) {
153 ret
= generic_rw(ppos
, &event_class
->context
->p
);
158 /* Read event payload */
159 if (event_class
->fields
) {
160 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
168 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
173 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
175 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
176 struct ctf_event
*event_class
;
181 /* print event header */
182 if (stream_class
->event_header
) {
183 /* lookup event id */
184 len_index
= struct_declaration_lookup_field_index(stream_class
->event_header_decl
,
185 g_quark_from_static_string("id"));
186 if (len_index
>= 0) {
187 struct definition_integer
*defint
;
188 struct definition
*field
;
190 field
= struct_definition_get_field_from_index(stream_class
->event_header
, len_index
);
191 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
192 defint
= container_of(field
, struct definition_integer
, p
);
193 assert(defint
->declaration
->signedness
== FALSE
);
194 id
= defint
->value
._unsigned
; /* set id */
197 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
202 /* print stream-declared event context */
203 if (stream_class
->event_context
) {
204 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
209 if (id
>= stream_class
->events_by_id
->len
) {
210 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
213 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
215 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
219 /* print event-declared event context */
220 if (event_class
->context
) {
221 ret
= generic_rw(pos
, &event_class
->context
->p
);
226 /* Read and print event payload */
227 if (event_class
->fields
) {
228 ret
= generic_rw(pos
, &event_class
->fields
->p
);
236 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
240 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
243 pos
->mmap_offset
= 0;
244 pos
->packet_size
= 0;
245 pos
->content_size
= 0;
246 pos
->content_size_loc
= NULL
;
252 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
253 sizeof(struct packet_index
));
255 pos
->packet_index
= NULL
;
256 switch (open_flags
& O_ACCMODE
) {
258 pos
->prot
= PROT_READ
;
259 pos
->flags
= MAP_PRIVATE
;
260 pos
->parent
.rw_table
= read_dispatch_table
;
261 pos
->parent
.event_cb
= ctf_read_event
;
264 pos
->prot
= PROT_WRITE
; /* Write has priority */
265 pos
->flags
= MAP_SHARED
;
266 pos
->parent
.rw_table
= write_dispatch_table
;
267 pos
->parent
.event_cb
= ctf_write_event
;
269 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
276 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
280 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
281 *pos
->content_size_loc
= pos
->offset
;
284 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
286 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
291 (void) g_array_free(pos
->packet_index
, TRUE
);
294 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
298 struct packet_index
*index
;
300 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
301 *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",
315 * The caller should never ask for ctf_move_pos across packets,
316 * except to get exactly at the beginning of the next packet.
318 if (pos
->prot
== PROT_WRITE
) {
321 /* The writer will add padding */
322 assert(pos
->offset
+ offset
== pos
->packet_size
);
323 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
326 assert(offset
== 0); /* only seek supported for now */
332 pos
->content_size
= -1U; /* Unknown at this point */
333 pos
->packet_size
= WRITE_PACKET_LEN
;
334 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
335 pos
->packet_size
/ CHAR_BIT
);
341 /* The reader will expect us to skip padding */
342 assert(pos
->offset
+ offset
== pos
->content_size
);
346 assert(offset
== 0); /* only seek supported for now */
352 if (pos
->cur_index
>= pos
->packet_index
->len
) {
356 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
358 pos
->mmap_offset
= index
->offset
;
360 /* Lookup context/packet size in index */
361 pos
->content_size
= index
->content_size
;
362 pos
->packet_size
= index
->packet_size
;
363 pos
->offset
= index
->data_offset
;
365 /* map new base. Need mapping length from header. */
366 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
367 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
368 if (pos
->base
== MAP_FAILED
) {
369 fprintf(stdout
, "[error] mmap error %s.\n",
376 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
382 len
= fread(&magic
, sizeof(magic
), 1, fp
);
383 if (len
!= sizeof(magic
)) {
386 if (magic
== TSDL_MAGIC
) {
388 td
->byte_order
= BYTE_ORDER
;
389 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
391 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
392 LITTLE_ENDIAN
: BIG_ENDIAN
;
400 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
403 struct metadata_packet_header header
;
404 size_t readlen
, writelen
;
408 readlen
= fread(&header
, sizeof(header
), 1, in
);
409 if (readlen
< sizeof(header
))
412 if (td
->byte_order
!= BYTE_ORDER
) {
413 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
414 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
415 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
416 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
419 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
420 if (header
.compression_scheme
) {
421 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
422 header
.compression_scheme
);
425 if (header
.encryption_scheme
) {
426 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
427 header
.encryption_scheme
);
430 if (header
.checksum_scheme
) {
431 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
432 header
.checksum_scheme
);
435 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
436 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
437 CTF_TRACE_SET_FIELD(td
, uuid
);
439 if (uuid_compare(header
.uuid
, td
->uuid
))
444 readlen
= fread(buf
, sizeof(char), sizeof(buf
), in
);
449 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
450 if (writelen
< readlen
) {
463 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
471 out
= open_memstream(buf
, &size
);
476 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
484 fclose(out
); /* flush the buffer */
486 /* open for reading */
487 *fp
= fmemopen(*buf
, size
, "rb");
492 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
494 struct ctf_scanner
*scanner
;
499 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
500 if (td
->metadata
.pos
.fd
< 0) {
501 fprintf(stdout
, "Unable to open metadata.\n");
502 return td
->metadata
.pos
.fd
;
505 if (babeltrace_debug
)
508 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
510 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
515 if (packet_metadata(td
, fp
)) {
516 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
518 goto end_packet_read
;
521 scanner
= ctf_scanner_alloc(fp
);
523 fprintf(stdout
, "[error] Error allocating scanner\n");
525 goto end_scanner_alloc
;
527 ret
= ctf_scanner_append_ast(scanner
);
529 fprintf(stdout
, "[error] Error creating AST\n");
533 if (babeltrace_debug
) {
534 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
536 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
541 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
543 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
546 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
549 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
553 ctf_scanner_free(scanner
);
559 close(td
->metadata
.pos
.fd
);
565 int create_stream_packet_index(struct ctf_trace
*td
,
566 struct ctf_file_stream
*file_stream
)
568 struct ctf_stream_class
*stream
;
570 struct ctf_stream_pos
*pos
;
571 struct stat filestats
;
572 struct packet_index packet_index
;
573 int first_packet
= 1;
576 pos
= &file_stream
->pos
;
578 ret
= fstat(pos
->fd
, &filestats
);
582 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
583 uint64_t stream_id
= 0;
587 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
589 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
595 /* map new base. Need mapping length from header. */
596 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
597 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
598 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
599 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
600 pos
->offset
= 0; /* Position of the packet header */
602 packet_index
.offset
= pos
->mmap_offset
;
603 packet_index
.content_size
= 0;
604 packet_index
.packet_size
= 0;
606 /* read and check header, set stream id (and check) */
607 if (td
->packet_header
) {
608 /* Read packet header */
609 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
612 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
613 if (len_index
>= 0) {
614 struct definition_integer
*defint
;
615 struct definition
*field
;
617 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
618 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
619 defint
= container_of(field
, struct definition_integer
, p
);
620 assert(defint
->declaration
->signedness
== FALSE
);
621 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
622 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
623 defint
->value
._unsigned
,
624 file_stream
->pos
.packet_index
->len
,
625 (ssize_t
) pos
->mmap_offset
);
631 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
632 if (len_index
>= 0) {
633 struct definition_array
*defarray
;
634 struct definition
*field
;
636 uint8_t uuidval
[UUID_LEN
];
638 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
639 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
640 defarray
= container_of(field
, struct definition_array
, p
);
641 assert(array_len(defarray
) == UUID_LEN
);
642 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
644 for (i
= 0; i
< UUID_LEN
; i
++) {
645 struct definition
*elem
;
646 struct definition_integer
*defint
;
648 elem
= array_index(defarray
, i
);
650 defint
= container_of(elem
, struct definition_integer
, p
);
651 uuidval
[i
] = defint
->value
._unsigned
;
653 ret
= uuid_compare(td
->uuid
, uuidval
);
655 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
661 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
662 if (len_index
>= 0) {
663 struct definition_integer
*defint
;
664 struct definition
*field
;
666 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
667 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
668 defint
= container_of(field
, struct definition_integer
, p
);
669 assert(defint
->declaration
->signedness
== FALSE
);
670 stream_id
= defint
->value
._unsigned
;
674 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
675 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
679 file_stream
->stream_id
= stream_id
;
680 if (stream_id
>= td
->streams
->len
) {
681 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
684 stream
= g_ptr_array_index(td
->streams
, stream_id
);
686 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
689 file_stream
->stream
.stream_class
= stream
;
693 if (stream
->packet_context
) {
694 /* Read packet context */
695 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
698 /* read content size from header */
699 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
700 if (len_index
>= 0) {
701 struct definition_integer
*defint
;
702 struct definition
*field
;
704 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
705 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
706 defint
= container_of(field
, struct definition_integer
, p
);
707 assert(defint
->declaration
->signedness
== FALSE
);
708 packet_index
.content_size
= defint
->value
._unsigned
;
710 /* Use file size for packet size */
711 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
714 /* read packet size from header */
715 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
716 if (len_index
>= 0) {
717 struct definition_integer
*defint
;
718 struct definition
*field
;
720 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
721 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
722 defint
= container_of(field
, struct definition_integer
, p
);
723 assert(defint
->declaration
->signedness
== FALSE
);
724 packet_index
.packet_size
= defint
->value
._unsigned
;
726 /* Use content size if non-zero, else file size */
727 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
730 /* Use file size for packet size */
731 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
732 /* Use content size if non-zero, else file size */
733 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
736 /* Validate content size and packet size values */
737 if (packet_index
.content_size
> packet_index
.packet_size
) {
738 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
739 packet_index
.content_size
, packet_index
.packet_size
);
743 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
744 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
745 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
749 /* Save position after header and context */
750 packet_index
.data_offset
= pos
->offset
;
752 /* add index to packet array */
753 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
755 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
758 /* Move pos back to beginning of file */
759 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
765 * Note: many file streams can inherit from the same stream class
766 * description (metadata).
769 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
772 struct ctf_file_stream
*file_stream
;
774 ret
= openat(td
->dirfd
, path
, flags
);
777 file_stream
= g_new0(struct ctf_file_stream
, 1);
778 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
779 ret
= create_stream_packet_index(td
, file_stream
);
782 /* Add stream file to stream class */
783 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
787 ctf_fini_pos(&file_stream
->pos
);
788 close(file_stream
->pos
.fd
);
795 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
798 struct dirent
*dirent
;
799 struct dirent
*diriter
;
804 /* Open trace directory */
805 td
->dir
= opendir(path
);
807 fprintf(stdout
, "[error] Unable to open trace directory.\n");
812 td
->dirfd
= open(path
, 0);
814 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
820 * Keep the metadata file separate.
823 ret
= ctf_open_trace_metadata_read(td
);
829 * Open each stream: for each file, try to open, check magic
830 * number, and get the stream ID to add to the right location in
834 dirent_len
= offsetof(struct dirent
, d_name
) +
835 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
837 dirent
= malloc(dirent_len
);
840 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
842 fprintf(stdout
, "[error] Readdir error.\n");
847 /* Ignore hidden files, ., .. and metadata. */
848 if (!strncmp(diriter
->d_name
, ".", 1)
849 || !strcmp(diriter
->d_name
, "..")
850 || !strcmp(diriter
->d_name
, "metadata"))
852 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
854 fprintf(stdout
, "[error] Open file stream error.\n");
865 g_ptr_array_free(td
->streams
, TRUE
);
873 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
875 struct ctf_trace
*td
;
878 td
= g_new0(struct ctf_trace
, 1);
880 switch (flags
& O_ACCMODE
) {
883 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
886 ret
= ctf_open_trace_read(td
, path
, flags
);
891 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
894 fprintf(stdout
, "[error] Incorrect open flags.\n");
905 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
907 ctf_fini_pos(&file_stream
->pos
);
908 close(file_stream
->pos
.fd
);
911 void ctf_close_trace(struct trace_descriptor
*tdp
)
913 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
917 for (i
= 0; i
< td
->streams
->len
; i
++) {
918 struct ctf_stream_class
*stream
;
920 stream
= g_ptr_array_index(td
->streams
, i
);
921 for (j
= 0; j
< stream
->files
->len
; j
++) {
922 struct ctf_file_stream
*file_stream
;
923 file_stream
= g_ptr_array_index(stream
->files
, j
);
924 ctf_close_file_stream(file_stream
);
928 g_ptr_array_free(td
->streams
, TRUE
);
934 void __attribute__((constructor
)) ctf_init(void)
938 ctf_format
.name
= g_quark_from_static_string("ctf");
939 ret
= bt_register_format(&ctf_format
);