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
;
104 struct definition
*variant
;
106 ret
= generic_rw(ppos
, &stream_class
->event_header
->p
);
109 /* lookup event id */
110 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
111 if (integer_definition
) {
112 id
= integer_definition
->value
._unsigned
;
114 struct definition_enum
*enum_definition
;
116 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
117 if (enum_definition
) {
118 id
= enum_definition
->integer
->value
._unsigned
;
122 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
124 integer_definition
= lookup_integer(variant
, "id", FALSE
);
125 if (integer_definition
) {
126 id
= integer_definition
->value
._unsigned
;
130 /* lookup timestamp */
131 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "timestamp", FALSE
);
132 if (integer_definition
) {
133 stream
->timestamp
= integer_definition
->value
._unsigned
;
136 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
137 if (integer_definition
) {
138 stream
->timestamp
= integer_definition
->value
._unsigned
;
144 /* Read stream-declared event context */
145 if (stream_class
->event_context
) {
146 ret
= generic_rw(ppos
, &stream_class
->event_context
->p
);
151 if (id
>= stream_class
->events_by_id
->len
) {
152 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
155 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
157 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
161 /* Read event-declared event context */
162 if (event_class
->context
) {
163 ret
= generic_rw(ppos
, &event_class
->context
->p
);
168 /* Read event payload */
169 if (event_class
->fields
) {
170 ret
= generic_rw(ppos
, &event_class
->fields
->p
);
178 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
183 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
185 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
186 struct ctf_event
*event_class
;
190 /* print event header */
191 if (stream_class
->event_header
) {
192 struct definition_integer
*integer_definition
;
193 struct definition
*variant
;
195 /* lookup event id */
196 integer_definition
= lookup_integer(&stream_class
->event_header
->p
, "id", FALSE
);
197 if (integer_definition
) {
198 id
= integer_definition
->value
._unsigned
;
200 struct definition_enum
*enum_definition
;
202 enum_definition
= lookup_enum(&stream_class
->event_header
->p
, "id", FALSE
);
203 if (enum_definition
) {
204 id
= enum_definition
->integer
->value
._unsigned
;
208 variant
= lookup_variant(&stream_class
->event_header
->p
, "v");
210 integer_definition
= lookup_integer(variant
, "id", FALSE
);
211 if (integer_definition
) {
212 id
= integer_definition
->value
._unsigned
;
216 ret
= generic_rw(pos
, &stream_class
->event_header
->p
);
221 /* print stream-declared event context */
222 if (stream_class
->event_context
) {
223 ret
= generic_rw(pos
, &stream_class
->event_context
->p
);
228 if (id
>= stream_class
->events_by_id
->len
) {
229 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
232 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
234 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
238 /* print event-declared event context */
239 if (event_class
->context
) {
240 ret
= generic_rw(pos
, &event_class
->context
->p
);
245 /* Read and print event payload */
246 if (event_class
->fields
) {
247 ret
= generic_rw(pos
, &event_class
->fields
->p
);
255 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
259 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
262 pos
->mmap_offset
= 0;
263 pos
->packet_size
= 0;
264 pos
->content_size
= 0;
265 pos
->content_size_loc
= NULL
;
271 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
272 sizeof(struct packet_index
));
274 pos
->packet_index
= NULL
;
275 switch (open_flags
& O_ACCMODE
) {
277 pos
->prot
= PROT_READ
;
278 pos
->flags
= MAP_PRIVATE
;
279 pos
->parent
.rw_table
= read_dispatch_table
;
280 pos
->parent
.event_cb
= ctf_read_event
;
283 pos
->prot
= PROT_WRITE
; /* Write has priority */
284 pos
->flags
= MAP_SHARED
;
285 pos
->parent
.rw_table
= write_dispatch_table
;
286 pos
->parent
.event_cb
= ctf_write_event
;
288 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
295 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
299 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
300 *pos
->content_size_loc
= pos
->offset
;
303 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
305 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
310 (void) g_array_free(pos
->packet_index
, TRUE
);
313 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
317 struct packet_index
*index
;
319 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
320 *pos
->content_size_loc
= pos
->offset
;
324 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
326 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
334 * The caller should never ask for ctf_move_pos across packets,
335 * except to get exactly at the beginning of the next packet.
337 if (pos
->prot
== PROT_WRITE
) {
340 /* The writer will add padding */
341 assert(pos
->offset
+ offset
== pos
->packet_size
);
342 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
345 assert(offset
== 0); /* only seek supported for now */
351 pos
->content_size
= -1U; /* Unknown at this point */
352 pos
->packet_size
= WRITE_PACKET_LEN
;
353 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
354 pos
->packet_size
/ CHAR_BIT
);
360 /* The reader will expect us to skip padding */
361 assert(pos
->offset
+ offset
== pos
->content_size
);
365 assert(offset
== 0); /* only seek supported for now */
371 if (pos
->cur_index
>= pos
->packet_index
->len
) {
375 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
377 pos
->mmap_offset
= index
->offset
;
379 /* Lookup context/packet size in index */
380 pos
->content_size
= index
->content_size
;
381 pos
->packet_size
= index
->packet_size
;
382 pos
->offset
= index
->data_offset
;
384 /* map new base. Need mapping length from header. */
385 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
386 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
387 if (pos
->base
== MAP_FAILED
) {
388 fprintf(stdout
, "[error] mmap error %s.\n",
395 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
401 len
= fread(&magic
, sizeof(magic
), 1, fp
);
405 if (magic
== TSDL_MAGIC
) {
407 td
->byte_order
= BYTE_ORDER
;
408 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
410 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
411 LITTLE_ENDIAN
: BIG_ENDIAN
;
413 CTF_TRACE_SET_FIELD(td
, byte_order
);
420 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
423 struct metadata_packet_header header
;
424 size_t readlen
, writelen
, toread
;
428 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
432 if (td
->byte_order
!= BYTE_ORDER
) {
433 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
434 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
435 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
436 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
439 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
440 if (header
.compression_scheme
) {
441 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
442 header
.compression_scheme
);
445 if (header
.encryption_scheme
) {
446 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
447 header
.encryption_scheme
);
450 if (header
.checksum_scheme
) {
451 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
452 header
.checksum_scheme
);
455 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
456 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
457 CTF_TRACE_SET_FIELD(td
, uuid
);
459 if (uuid_compare(header
.uuid
, td
->uuid
))
463 toread
= header
.content_size
/ CHAR_BIT
;
466 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
), toread
), in
);
471 if (babeltrace_debug
) {
472 fprintf(stdout
, "[debug] metadata packet read: %s\n",
476 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
477 if (writelen
< readlen
) {
487 ret
= 0; /* continue reading next packet */
495 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
504 * Using strlen on *buf instead of size of open_memstream
505 * because its size includes garbage at the end (after final
506 * \0). This is the allocated size, not the actual string size.
508 out
= open_memstream(buf
, &size
);
513 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
522 fclose(out
); /* flush the buffer */
524 /* open for reading */
525 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
530 int ctf_open_trace_metadata_read(struct ctf_trace
*td
)
532 struct ctf_scanner
*scanner
;
537 td
->metadata
.pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
538 if (td
->metadata
.pos
.fd
< 0) {
539 fprintf(stdout
, "Unable to open metadata.\n");
540 return td
->metadata
.pos
.fd
;
543 if (babeltrace_debug
)
546 fp
= fdopen(td
->metadata
.pos
.fd
, "r");
548 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
553 if (packet_metadata(td
, fp
)) {
554 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
556 goto end_packet_read
;
559 scanner
= ctf_scanner_alloc(fp
);
561 fprintf(stdout
, "[error] Error allocating scanner\n");
563 goto end_scanner_alloc
;
565 ret
= ctf_scanner_append_ast(scanner
);
567 fprintf(stdout
, "[error] Error creating AST\n");
571 if (babeltrace_debug
) {
572 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
574 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
579 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
581 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
584 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
587 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
591 ctf_scanner_free(scanner
);
597 close(td
->metadata
.pos
.fd
);
603 int create_stream_packet_index(struct ctf_trace
*td
,
604 struct ctf_file_stream
*file_stream
)
606 struct ctf_stream_class
*stream
;
608 struct ctf_stream_pos
*pos
;
609 struct stat filestats
;
610 struct packet_index packet_index
;
611 int first_packet
= 1;
614 pos
= &file_stream
->pos
;
616 ret
= fstat(pos
->fd
, &filestats
);
620 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
621 uint64_t stream_id
= 0;
625 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
627 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
633 /* map new base. Need mapping length from header. */
634 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
635 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
636 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
637 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
638 pos
->offset
= 0; /* Position of the packet header */
640 packet_index
.offset
= pos
->mmap_offset
;
641 packet_index
.content_size
= 0;
642 packet_index
.packet_size
= 0;
644 /* read and check header, set stream id (and check) */
645 if (td
->packet_header
) {
646 /* Read packet header */
647 ret
= generic_rw(&pos
->parent
, &td
->packet_header
->p
);
650 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("magic"));
651 if (len_index
>= 0) {
652 struct definition_integer
*defint
;
653 struct definition
*field
;
655 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
656 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
657 defint
= container_of(field
, struct definition_integer
, p
);
658 assert(defint
->declaration
->signedness
== FALSE
);
659 if (defint
->value
._unsigned
!= CTF_MAGIC
) {
660 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
661 defint
->value
._unsigned
,
662 file_stream
->pos
.packet_index
->len
,
663 (ssize_t
) pos
->mmap_offset
);
669 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("uuid"));
670 if (len_index
>= 0) {
671 struct definition_array
*defarray
;
672 struct definition
*field
;
674 uint8_t uuidval
[UUID_LEN
];
676 field
= struct_definition_get_field_from_index(td
->packet_header
, len_index
);
677 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
678 defarray
= container_of(field
, struct definition_array
, p
);
679 assert(array_len(defarray
) == UUID_LEN
);
680 assert(defarray
->declaration
->elem
->id
== CTF_TYPE_INTEGER
);
682 for (i
= 0; i
< UUID_LEN
; i
++) {
683 struct definition
*elem
;
684 struct definition_integer
*defint
;
686 elem
= array_index(defarray
, i
);
688 defint
= container_of(elem
, struct definition_integer
, p
);
689 uuidval
[i
] = defint
->value
._unsigned
;
691 ret
= uuid_compare(td
->uuid
, uuidval
);
693 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
699 len_index
= struct_declaration_lookup_field_index(td
->packet_header
->declaration
, g_quark_from_static_string("stream_id"));
700 if (len_index
>= 0) {
701 struct definition_integer
*defint
;
702 struct definition
*field
;
704 field
= struct_definition_get_field_from_index(td
->packet_header
, 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 stream_id
= defint
->value
._unsigned
;
712 if (!first_packet
&& file_stream
->stream_id
!= stream_id
) {
713 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
717 file_stream
->stream_id
= stream_id
;
718 if (stream_id
>= td
->streams
->len
) {
719 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
722 stream
= g_ptr_array_index(td
->streams
, stream_id
);
724 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
727 file_stream
->stream
.stream_class
= stream
;
731 if (stream
->packet_context
) {
732 /* Read packet context */
733 ret
= generic_rw(&pos
->parent
, &stream
->packet_context
->p
);
736 /* read content size from header */
737 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("content_size"));
738 if (len_index
>= 0) {
739 struct definition_integer
*defint
;
740 struct definition
*field
;
742 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
743 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
744 defint
= container_of(field
, struct definition_integer
, p
);
745 assert(defint
->declaration
->signedness
== FALSE
);
746 packet_index
.content_size
= defint
->value
._unsigned
;
748 /* Use file size for packet size */
749 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
752 /* read packet size from header */
753 len_index
= struct_declaration_lookup_field_index(stream
->packet_context
->declaration
, g_quark_from_static_string("packet_size"));
754 if (len_index
>= 0) {
755 struct definition_integer
*defint
;
756 struct definition
*field
;
758 field
= struct_definition_get_field_from_index(stream
->packet_context
, len_index
);
759 assert(field
->declaration
->id
== CTF_TYPE_INTEGER
);
760 defint
= container_of(field
, struct definition_integer
, p
);
761 assert(defint
->declaration
->signedness
== FALSE
);
762 packet_index
.packet_size
= defint
->value
._unsigned
;
764 /* Use content size if non-zero, else file size */
765 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
768 /* Use file size for packet size */
769 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
770 /* Use content size if non-zero, else file size */
771 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
774 /* Validate content size and packet size values */
775 if (packet_index
.content_size
> packet_index
.packet_size
) {
776 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
777 packet_index
.content_size
, packet_index
.packet_size
);
781 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
782 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
783 packet_index
.content_size
, (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
787 /* Save position after header and context */
788 packet_index
.data_offset
= pos
->offset
;
790 /* add index to packet array */
791 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
793 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
796 /* Move pos back to beginning of file */
797 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
803 * Note: many file streams can inherit from the same stream class
804 * description (metadata).
807 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
)
810 struct ctf_file_stream
*file_stream
;
812 ret
= openat(td
->dirfd
, path
, flags
);
815 file_stream
= g_new0(struct ctf_file_stream
, 1);
816 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
817 ret
= create_stream_packet_index(td
, file_stream
);
820 /* Add stream file to stream class */
821 g_ptr_array_add(file_stream
->stream
.stream_class
->files
, file_stream
);
825 ctf_fini_pos(&file_stream
->pos
);
826 close(file_stream
->pos
.fd
);
833 int ctf_open_trace_read(struct ctf_trace
*td
, const char *path
, int flags
)
836 struct dirent
*dirent
;
837 struct dirent
*diriter
;
842 /* Open trace directory */
843 td
->dir
= opendir(path
);
845 fprintf(stdout
, "[error] Unable to open trace directory.\n");
850 td
->dirfd
= open(path
, 0);
852 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
858 * Keep the metadata file separate.
861 ret
= ctf_open_trace_metadata_read(td
);
867 * Open each stream: for each file, try to open, check magic
868 * number, and get the stream ID to add to the right location in
872 dirent_len
= offsetof(struct dirent
, d_name
) +
873 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
875 dirent
= malloc(dirent_len
);
878 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
880 fprintf(stdout
, "[error] Readdir error.\n");
885 /* Ignore hidden files, ., .. and metadata. */
886 if (!strncmp(diriter
->d_name
, ".", 1)
887 || !strcmp(diriter
->d_name
, "..")
888 || !strcmp(diriter
->d_name
, "metadata"))
890 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
);
892 fprintf(stdout
, "[error] Open file stream error.\n");
910 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
)
912 struct ctf_trace
*td
;
915 td
= g_new0(struct ctf_trace
, 1);
917 switch (flags
& O_ACCMODE
) {
920 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
923 ret
= ctf_open_trace_read(td
, path
, flags
);
928 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
931 fprintf(stdout
, "[error] Incorrect open flags.\n");
942 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
944 ctf_fini_pos(&file_stream
->pos
);
945 close(file_stream
->pos
.fd
);
948 void ctf_close_trace(struct trace_descriptor
*tdp
)
950 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
954 for (i
= 0; i
< td
->streams
->len
; i
++) {
955 struct ctf_stream_class
*stream
;
957 stream
= g_ptr_array_index(td
->streams
, i
);
958 for (j
= 0; j
< stream
->files
->len
; j
++) {
959 struct ctf_file_stream
*file_stream
;
960 file_stream
= g_ptr_array_index(stream
->files
, j
);
961 ctf_close_file_stream(file_stream
);
965 g_ptr_array_free(td
->streams
, TRUE
);
971 void __attribute__((constructor
)) ctf_init(void)
975 ctf_format
.name
= g_quark_from_static_string("ctf");
976 ret
= bt_register_format(&ctf_format
);