2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/context-internal.h>
28 #include <babeltrace/uuid.h>
29 #include <babeltrace/endian.h>
34 #include <sys/types.h>
42 #include "metadata/ctf-scanner.h"
43 #include "metadata/ctf-parser.h"
44 #include "metadata/ctf-ast.h"
45 #include "events-private.h"
46 #include "memstream.h"
49 * We currently simply map a page to read the packet header and packet
50 * context to get the packet length and content length. (in bits)
52 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
53 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
56 #define min(a, b) (((a) < (b)) ? (a) : (b))
59 #define NSEC_PER_SEC 1000000000ULL
66 uint64_t opt_clock_offset
;
71 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
72 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
76 struct trace_descriptor
*ctf_open_mmap_trace(
77 struct mmap_stream_list
*mmap_list
,
78 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
82 void ctf_set_context(struct trace_descriptor
*descriptor
,
83 struct bt_context
*ctx
);
85 void ctf_set_handle(struct trace_descriptor
*descriptor
,
86 struct bt_trace_handle
*handle
);
89 void ctf_close_trace(struct trace_descriptor
*descriptor
);
91 uint64_t ctf_timestamp_begin(struct trace_descriptor
*descriptor
,
92 struct bt_trace_handle
*handle
);
94 uint64_t ctf_timestamp_end(struct trace_descriptor
*descriptor
,
95 struct bt_trace_handle
*handle
);
98 rw_dispatch read_dispatch_table
[] = {
99 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
100 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
101 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
102 [ CTF_TYPE_STRING
] = ctf_string_read
,
103 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
104 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
105 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
106 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
110 rw_dispatch write_dispatch_table
[] = {
111 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
112 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
113 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
114 [ CTF_TYPE_STRING
] = ctf_string_write
,
115 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
116 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
117 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
118 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
122 struct format ctf_format
= {
123 .open_trace
= ctf_open_trace
,
124 .open_mmap_trace
= ctf_open_mmap_trace
,
125 .close_trace
= ctf_close_trace
,
126 .set_context
= ctf_set_context
,
127 .set_handle
= ctf_set_handle
,
128 .timestamp_begin
= ctf_timestamp_begin
,
129 .timestamp_end
= ctf_timestamp_end
,
133 uint64_t ctf_timestamp_begin(struct trace_descriptor
*descriptor
,
134 struct bt_trace_handle
*handle
)
136 struct ctf_trace
*tin
;
137 uint64_t begin
= ULLONG_MAX
;
140 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
145 /* for each stream_class */
146 for (i
= 0; i
< tin
->streams
->len
; i
++) {
147 struct ctf_stream_declaration
*stream_class
;
149 stream_class
= g_ptr_array_index(tin
->streams
, i
);
152 /* for each file_stream */
153 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
154 struct ctf_stream_definition
*stream
;
155 struct ctf_file_stream
*cfs
;
156 struct ctf_stream_pos
*stream_pos
;
157 struct packet_index
*index
;
159 stream
= g_ptr_array_index(stream_class
->streams
, j
);
160 cfs
= container_of(stream
, struct ctf_file_stream
,
162 stream_pos
= &cfs
->pos
;
164 index
= &g_array_index(stream_pos
->packet_index
,
165 struct packet_index
, 0);
166 if (index
->timestamp_begin
< begin
)
167 begin
= index
->timestamp_begin
;
178 uint64_t ctf_timestamp_end(struct trace_descriptor
*descriptor
,
179 struct bt_trace_handle
*handle
)
181 struct ctf_trace
*tin
;
185 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
190 /* for each stream_class */
191 for (i
= 0; i
< tin
->streams
->len
; i
++) {
192 struct ctf_stream_declaration
*stream_class
;
194 stream_class
= g_ptr_array_index(tin
->streams
, i
);
197 /* for each file_stream */
198 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
199 struct ctf_stream_definition
*stream
;
200 struct ctf_file_stream
*cfs
;
201 struct ctf_stream_pos
*stream_pos
;
202 struct packet_index
*index
;
204 stream
= g_ptr_array_index(stream_class
->streams
, j
);
205 cfs
= container_of(stream
, struct ctf_file_stream
,
207 stream_pos
= &cfs
->pos
;
209 index
= &g_array_index(stream_pos
->packet_index
,
211 stream_pos
->packet_index
->len
- 1);
212 if (index
->timestamp_end
> end
)
213 end
= index
->timestamp_end
;
224 * Update stream current timestamp, keep at clock frequency.
227 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
228 struct definition_integer
*integer_definition
)
230 struct declaration_integer
*integer_declaration
=
231 integer_definition
->declaration
;
232 uint64_t oldval
, newval
, updateval
;
234 if (unlikely(integer_declaration
->len
== 64)) {
235 stream
->timestamp
= integer_definition
->value
._unsigned
;
239 oldval
= stream
->timestamp
;
240 oldval
&= (1ULL << integer_declaration
->len
) - 1;
241 newval
= integer_definition
->value
._unsigned
;
242 /* Test for overflow by comparing low bits */
244 newval
+= 1ULL << integer_declaration
->len
;
245 /* updateval contains old high bits, and new low bits (sum) */
246 updateval
= stream
->timestamp
;
247 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
249 stream
->prev_timestamp
= stream
->timestamp
;
250 stream
->timestamp
= updateval
;
254 * Print timestamp, rescaling clock frequency to nanoseconds and
255 * applying offsets as needed (unix time).
257 void ctf_print_timestamp(FILE *fp
,
258 struct ctf_stream_definition
*stream
,
261 uint64_t ts_sec
= 0, ts_nsec
;
264 ts_nsec
= ctf_get_timestamp_raw(stream
, timestamp
);
266 ts_nsec
= ctf_get_timestamp(stream
, timestamp
);
269 /* Add command-line offset */
270 ts_sec
+= opt_clock_offset
;
272 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
273 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
275 if (!opt_clock_seconds
) {
277 time_t time_s
= (time_t) ts_sec
;
279 if (!opt_clock_gmt
) {
282 res
= localtime_r(&time_s
, &tm
);
284 fprintf(stderr
, "[warning] Unable to get localtime.\n");
290 res
= gmtime_r(&time_s
, &tm
);
292 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
296 if (opt_clock_date
) {
300 /* Print date and time */
301 res
= strftime(timestr
, sizeof(timestr
),
304 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
307 fprintf(fp
, "%s", timestr
);
309 /* Print time in HH:MM:SS.ns */
310 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
311 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
315 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
323 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
325 struct ctf_stream_pos
*pos
=
326 container_of(ppos
, struct ctf_stream_pos
, parent
);
327 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
328 struct ctf_event_definition
*event
;
332 /* We need to check for EOF here for empty files. */
333 if (unlikely(pos
->offset
== EOF
))
336 ctf_pos_get_event(pos
);
338 /* save the current position as a restore point */
339 pos
->last_offset
= pos
->offset
;
342 * This is the EOF check after we've advanced the position in
345 if (unlikely(pos
->offset
== EOF
))
347 assert(pos
->offset
< pos
->content_size
);
349 /* Read event header */
350 if (likely(stream
->stream_event_header
)) {
351 struct definition_integer
*integer_definition
;
352 struct definition
*variant
;
354 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
357 /* lookup event id */
358 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
359 if (integer_definition
) {
360 id
= integer_definition
->value
._unsigned
;
362 struct definition_enum
*enum_definition
;
364 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
365 if (enum_definition
) {
366 id
= enum_definition
->integer
->value
._unsigned
;
370 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
372 integer_definition
= lookup_integer(variant
, "id", FALSE
);
373 if (integer_definition
) {
374 id
= integer_definition
->value
._unsigned
;
377 stream
->event_id
= id
;
379 /* lookup timestamp */
380 stream
->has_timestamp
= 0;
381 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
382 if (integer_definition
) {
383 ctf_update_timestamp(stream
, integer_definition
);
384 stream
->has_timestamp
= 1;
387 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
388 if (integer_definition
) {
389 ctf_update_timestamp(stream
, integer_definition
);
390 stream
->has_timestamp
= 1;
396 /* Read stream-declared event context */
397 if (stream
->stream_event_context
) {
398 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
403 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
404 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
407 event
= g_ptr_array_index(stream
->events_by_id
, id
);
408 if (unlikely(!event
)) {
409 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
413 /* Read event-declared event context */
414 if (event
->event_context
) {
415 ret
= generic_rw(ppos
, &event
->event_context
->p
);
420 /* Read event payload */
421 if (likely(event
->event_fields
)) {
422 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
430 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
435 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream_definition
*stream
)
437 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
438 struct ctf_event_definition
*event
;
442 id
= stream
->event_id
;
444 /* print event header */
445 if (likely(stream
->stream_event_header
)) {
446 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
451 /* print stream-declared event context */
452 if (stream
->stream_event_context
) {
453 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
458 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
459 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
462 event
= g_ptr_array_index(stream
->events_by_id
, id
);
463 if (unlikely(!event
)) {
464 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
468 /* print event-declared event context */
469 if (event
->event_context
) {
470 ret
= generic_rw(pos
, &event
->event_context
->p
);
475 /* Read and print event payload */
476 if (likely(event
->event_fields
)) {
477 ret
= generic_rw(pos
, &event
->event_fields
->p
);
485 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
489 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
492 pos
->mmap_offset
= 0;
493 pos
->packet_size
= 0;
494 pos
->content_size
= 0;
495 pos
->content_size_loc
= NULL
;
496 pos
->base_mma
= NULL
;
501 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
502 sizeof(struct packet_index
));
504 pos
->packet_index
= NULL
;
505 switch (open_flags
& O_ACCMODE
) {
507 pos
->prot
= PROT_READ
;
508 pos
->flags
= MAP_PRIVATE
;
509 pos
->parent
.rw_table
= read_dispatch_table
;
510 pos
->parent
.event_cb
= ctf_read_event
;
513 pos
->prot
= PROT_WRITE
; /* Write has priority */
514 pos
->flags
= MAP_SHARED
;
515 pos
->parent
.rw_table
= write_dispatch_table
;
516 pos
->parent
.event_cb
= ctf_write_event
;
518 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
525 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
529 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
530 *pos
->content_size_loc
= pos
->offset
;
533 ret
= munmap_align(pos
->base_mma
);
535 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
540 (void) g_array_free(pos
->packet_index
, TRUE
);
544 * for SEEK_CUR: go to next packet.
545 * for SEEK_POS: go to packet numer (index).
547 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
549 struct ctf_stream_pos
*pos
=
550 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
551 struct ctf_file_stream
*file_stream
=
552 container_of(pos
, struct ctf_file_stream
, pos
);
555 struct packet_index
*packet_index
;
557 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
558 *pos
->content_size_loc
= pos
->offset
;
562 ret
= munmap_align(pos
->base_mma
);
564 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
568 pos
->base_mma
= NULL
;
572 * The caller should never ask for ctf_move_pos across packets,
573 * except to get exactly at the beginning of the next packet.
575 if (pos
->prot
== PROT_WRITE
) {
578 /* The writer will add padding */
579 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
582 assert(index
== 0); /* only seek supported for now */
588 pos
->content_size
= -1U; /* Unknown at this point */
589 pos
->packet_size
= WRITE_PACKET_LEN
;
590 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
591 pos
->packet_size
/ CHAR_BIT
);
599 uint64_t events_discarded_diff
;
601 if (pos
->offset
== EOF
) {
604 /* For printing discarded event count */
605 packet_index
= &g_array_index(pos
->packet_index
,
606 struct packet_index
, pos
->cur_index
);
607 events_discarded_diff
= packet_index
->events_discarded
;
608 file_stream
->parent
.prev_timestamp_end
=
609 packet_index
->timestamp_end
;
610 if (pos
->cur_index
> 0) {
611 packet_index
= &g_array_index(pos
->packet_index
,
614 events_discarded_diff
-= packet_index
->events_discarded
;
616 * Deal with 32-bit wrap-around if the
617 * tracer provided a 32-bit field.
619 if (packet_index
->events_discarded_len
== 32) {
620 events_discarded_diff
= (uint32_t) events_discarded_diff
;
623 file_stream
->parent
.events_discarded
= events_discarded_diff
;
624 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
625 /* The reader will expect us to skip padding */
630 pos
->cur_index
= index
;
631 file_stream
->parent
.prev_timestamp
= 0;
632 file_stream
->parent
.prev_timestamp_end
= 0;
637 if (pos
->cur_index
>= pos
->packet_index
->len
) {
639 * When a stream reaches the end of the
640 * file, we need to show the number of
641 * events discarded ourselves, because
642 * there is no next event scheduled to
643 * be printed in the output.
645 if (file_stream
->parent
.events_discarded
) {
647 * We need to check if we are in trace
648 * read or called from packet indexing.
649 * In this last case, the collection is
650 * not there, so we cannot print the
653 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
655 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events at end of stream between [",
656 file_stream
->parent
.events_discarded
);
657 ctf_print_timestamp(stderr
, &file_stream
->parent
,
658 file_stream
->parent
.prev_timestamp
);
659 fprintf(stderr
, "] and [");
660 ctf_print_timestamp(stderr
, &file_stream
->parent
,
661 file_stream
->parent
.prev_timestamp_end
);
662 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
665 file_stream
->parent
.events_discarded
= 0;
670 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
672 pos
->mmap_offset
= packet_index
->offset
;
674 /* Lookup context/packet size in index */
675 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
676 pos
->content_size
= packet_index
->content_size
;
677 pos
->packet_size
= packet_index
->packet_size
;
678 if (packet_index
->data_offset
< packet_index
->content_size
) {
679 pos
->offset
= 0; /* will read headers */
680 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
682 pos
->offset
= packet_index
->data_offset
;
684 goto read_next_packet
;
690 /* map new base. Need mapping length from header. */
691 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
692 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
693 if (pos
->base_mma
== MAP_FAILED
) {
694 fprintf(stderr
, "[error] mmap error %s.\n",
699 /* update trace_packet_header and stream_packet_context */
700 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
701 /* Read packet header */
702 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
705 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
706 /* Read packet context */
707 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
713 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
719 len
= fread(&magic
, sizeof(magic
), 1, fp
);
723 if (magic
== TSDL_MAGIC
) {
725 td
->byte_order
= BYTE_ORDER
;
726 CTF_TRACE_SET_FIELD(td
, byte_order
);
727 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
729 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
730 LITTLE_ENDIAN
: BIG_ENDIAN
;
731 CTF_TRACE_SET_FIELD(td
, byte_order
);
739 * Returns 0 on success, -1 on error.
742 int check_version(unsigned int major
, unsigned int minor
)
757 /* eventually return an error instead of warning */
759 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
765 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
768 struct metadata_packet_header header
;
769 size_t readlen
, writelen
, toread
;
770 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
773 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
777 if (td
->byte_order
!= BYTE_ORDER
) {
778 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
779 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
780 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
781 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
784 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
785 if (header
.compression_scheme
) {
786 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
787 header
.compression_scheme
);
790 if (header
.encryption_scheme
) {
791 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
792 header
.encryption_scheme
);
795 if (header
.checksum_scheme
) {
796 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
797 header
.checksum_scheme
);
800 if (check_version(header
.major
, header
.minor
) < 0)
802 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
803 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
804 CTF_TRACE_SET_FIELD(td
, uuid
);
806 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
810 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
813 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
818 if (babeltrace_debug
) {
820 fprintf(stderr
, "[debug] metadata packet read: %s\n",
824 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
825 if (writelen
< readlen
) {
835 ret
= 0; /* continue reading next packet */
842 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
843 ret
= fseek(in
, toread
, SEEK_CUR
);
845 fprintf(stderr
, "[warning] Missing padding at end of file\n");
852 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
861 * Using strlen on *buf instead of size of open_memstream
862 * because its size includes garbage at the end (after final
863 * \0). This is the allocated size, not the actual string size.
865 out
= babeltrace_open_memstream(buf
, &size
);
867 perror("Metadata open_memstream");
871 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
880 /* close to flush the buffer */
881 ret
= babeltrace_close_memstream(buf
, &size
, out
);
883 perror("babeltrace_flush_memstream");
888 /* open for reading */
889 *fp
= babeltrace_fmemopen(*buf
, strlen(*buf
), "rb");
891 perror("Metadata fmemopen");
898 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
899 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
900 int whence
), FILE *metadata_fp
)
902 struct ctf_scanner
*scanner
;
903 struct ctf_file_stream
*metadata_stream
;
908 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
909 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
912 metadata_stream
->pos
.packet_seek
= packet_seek
;
914 fprintf(stderr
, "[error] packet_seek function undefined.\n");
922 td
->metadata
= &metadata_stream
->parent
;
923 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
924 if (metadata_stream
->pos
.fd
< 0) {
925 fprintf(stderr
, "Unable to open metadata.\n");
926 return metadata_stream
->pos
.fd
;
929 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
931 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
932 perror("Metadata stream open");
937 if (babeltrace_debug
)
940 if (packet_metadata(td
, fp
)) {
941 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
943 goto end_packet_read
;
945 unsigned int major
, minor
;
948 td
->byte_order
= BYTE_ORDER
;
950 /* Check text-only metadata header and version */
951 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
953 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
954 if (check_version(major
, minor
) < 0) {
956 goto end_packet_read
;
961 scanner
= ctf_scanner_alloc(fp
);
963 fprintf(stderr
, "[error] Error allocating scanner\n");
965 goto end_scanner_alloc
;
967 ret
= ctf_scanner_append_ast(scanner
);
969 fprintf(stderr
, "[error] Error creating AST\n");
973 if (babeltrace_debug
) {
974 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
976 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
981 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
983 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
986 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
989 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
993 ctf_scanner_free(scanner
);
1000 close(metadata_stream
->pos
.fd
);
1002 g_free(metadata_stream
);
1007 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1008 struct ctf_stream_definition
*stream
,
1009 struct ctf_event_declaration
*event
)
1011 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1013 if (event
->context_decl
) {
1014 struct definition
*definition
=
1015 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1016 stream
->parent_def_scope
, 0, 0, "event.context");
1020 stream_event
->event_context
= container_of(definition
,
1021 struct definition_struct
, p
);
1022 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1024 if (event
->fields_decl
) {
1025 struct definition
*definition
=
1026 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1027 stream
->parent_def_scope
, 0, 0, "event.fields");
1031 stream_event
->event_fields
= container_of(definition
,
1032 struct definition_struct
, p
);
1033 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1035 stream_event
->stream
= stream
;
1036 return stream_event
;
1039 if (stream_event
->event_fields
)
1040 definition_unref(&stream_event
->event_fields
->p
);
1041 if (stream_event
->event_context
)
1042 definition_unref(&stream_event
->event_context
->p
);
1047 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1049 struct ctf_stream_declaration
*stream_class
;
1053 if (stream
->stream_definitions_created
)
1056 stream_class
= stream
->stream_class
;
1058 if (stream_class
->packet_context_decl
) {
1059 struct definition
*definition
=
1060 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1061 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1066 stream
->stream_packet_context
= container_of(definition
,
1067 struct definition_struct
, p
);
1068 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1070 if (stream_class
->event_header_decl
) {
1071 struct definition
*definition
=
1072 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1073 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1078 stream
->stream_event_header
=
1079 container_of(definition
, struct definition_struct
, p
);
1080 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1082 if (stream_class
->event_context_decl
) {
1083 struct definition
*definition
=
1084 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1085 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1090 stream
->stream_event_context
=
1091 container_of(definition
, struct definition_struct
, p
);
1092 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1094 stream
->events_by_id
= g_ptr_array_new();
1095 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1096 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1097 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1098 struct ctf_event_definition
*stream_event
;
1102 stream_event
= create_event_definitions(td
, stream
, event
);
1105 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1110 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1111 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1113 g_free(stream_event
);
1115 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1117 if (stream
->stream_event_context
)
1118 definition_unref(&stream
->stream_event_context
->p
);
1119 if (stream
->stream_event_header
)
1120 definition_unref(&stream
->stream_event_header
->p
);
1121 if (stream
->stream_packet_context
)
1122 definition_unref(&stream
->stream_packet_context
->p
);
1128 int create_stream_packet_index(struct ctf_trace
*td
,
1129 struct ctf_file_stream
*file_stream
)
1131 struct ctf_stream_declaration
*stream
;
1133 struct ctf_stream_pos
*pos
;
1134 struct stat filestats
;
1135 struct packet_index packet_index
;
1136 int first_packet
= 1;
1139 pos
= &file_stream
->pos
;
1141 ret
= fstat(pos
->fd
, &filestats
);
1145 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1148 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1149 uint64_t stream_id
= 0;
1151 if (pos
->base_mma
) {
1152 /* unmap old base */
1153 ret
= munmap_align(pos
->base_mma
);
1155 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1159 pos
->base_mma
= NULL
;
1161 /* map new base. Need mapping length from header. */
1162 pos
->base_mma
= mmap_align(MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1163 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1164 assert(pos
->base_mma
!= MAP_FAILED
);
1165 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1166 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1167 pos
->offset
= 0; /* Position of the packet header */
1169 packet_index
.offset
= pos
->mmap_offset
;
1170 packet_index
.content_size
= 0;
1171 packet_index
.packet_size
= 0;
1172 packet_index
.timestamp_begin
= 0;
1173 packet_index
.timestamp_end
= 0;
1174 packet_index
.events_discarded
= 0;
1176 /* read and check header, set stream id (and check) */
1177 if (file_stream
->parent
.trace_packet_header
) {
1178 /* Read packet header */
1179 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1182 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1183 if (len_index
>= 0) {
1184 struct definition
*field
;
1187 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1188 magic
= get_unsigned_int(field
);
1189 if (magic
!= CTF_MAGIC
) {
1190 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1192 file_stream
->pos
.packet_index
->len
,
1193 (ssize_t
) pos
->mmap_offset
);
1199 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1200 if (len_index
>= 0) {
1201 struct definition_array
*defarray
;
1202 struct definition
*field
;
1204 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1206 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1207 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1208 defarray
= container_of(field
, struct definition_array
, p
);
1209 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1211 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1212 struct definition
*elem
;
1214 elem
= array_index(defarray
, i
);
1215 uuidval
[i
] = get_unsigned_int(elem
);
1217 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1219 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1225 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1226 if (len_index
>= 0) {
1227 struct definition
*field
;
1229 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1230 stream_id
= get_unsigned_int(field
);
1234 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1235 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1239 file_stream
->parent
.stream_id
= stream_id
;
1240 if (stream_id
>= td
->streams
->len
) {
1241 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1244 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1246 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1249 file_stream
->parent
.stream_class
= stream
;
1250 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1256 if (file_stream
->parent
.stream_packet_context
) {
1257 /* Read packet context */
1258 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1261 /* read content size from header */
1262 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1263 if (len_index
>= 0) {
1264 struct definition
*field
;
1266 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1267 packet_index
.content_size
= get_unsigned_int(field
);
1269 /* Use file size for packet size */
1270 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1273 /* read packet size from header */
1274 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1275 if (len_index
>= 0) {
1276 struct definition
*field
;
1278 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1279 packet_index
.packet_size
= get_unsigned_int(field
);
1281 /* Use content size if non-zero, else file size */
1282 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1285 /* read timestamp begin from header */
1286 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1287 if (len_index
>= 0) {
1288 struct definition
*field
;
1290 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1291 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1294 /* read timestamp end from header */
1295 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1296 if (len_index
>= 0) {
1297 struct definition
*field
;
1299 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1300 packet_index
.timestamp_end
= get_unsigned_int(field
);
1303 /* read events discarded from header */
1304 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1305 if (len_index
>= 0) {
1306 struct definition
*field
;
1308 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1309 packet_index
.events_discarded
= get_unsigned_int(field
);
1310 packet_index
.events_discarded_len
= get_int_len(field
);
1313 /* Use file size for packet size */
1314 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1315 /* Use content size if non-zero, else file size */
1316 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1319 /* Validate content size and packet size values */
1320 if (packet_index
.content_size
> packet_index
.packet_size
) {
1321 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1322 packet_index
.content_size
, packet_index
.packet_size
);
1326 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1327 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1328 packet_index
.packet_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1332 /* Save position after header and context */
1333 packet_index
.data_offset
= pos
->offset
;
1335 /* add index to packet array */
1336 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1338 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1341 /* Move pos back to beginning of file */
1342 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1348 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1352 if (td
->packet_header_decl
) {
1353 struct definition
*definition
=
1354 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1355 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1360 stream
->trace_packet_header
=
1361 container_of(definition
, struct definition_struct
, p
);
1362 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1372 * Note: many file streams can inherit from the same stream class
1373 * description (metadata).
1376 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1377 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1381 struct ctf_file_stream
*file_stream
;
1382 struct stat statbuf
;
1384 fd
= openat(td
->dirfd
, path
, flags
);
1386 perror("File stream openat()");
1391 /* Don't try to mmap subdirectories. Skip them, return success. */
1392 ret
= fstat(fd
, &statbuf
);
1394 perror("File stream fstat()");
1397 if (S_ISDIR(statbuf
.st_mode
)) {
1398 fprintf(stderr
, "[warning] Skipping directory '%s' found in trace\n", path
);
1403 file_stream
= g_new0(struct ctf_file_stream
, 1);
1404 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1407 file_stream
->pos
.packet_seek
= packet_seek
;
1409 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1414 ctf_init_pos(&file_stream
->pos
, fd
, flags
);
1415 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1419 * For now, only a single slock is supported.
1421 file_stream
->parent
.current_clock
= td
->single_clock
;
1422 ret
= create_stream_packet_index(td
, file_stream
);
1425 /* Add stream file to stream class */
1426 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1427 &file_stream
->parent
);
1431 if (file_stream
->parent
.trace_packet_header
)
1432 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1434 ctf_fini_pos(&file_stream
->pos
);
1435 g_free(file_stream
);
1444 int ctf_open_trace_read(struct ctf_trace
*td
,
1445 const char *path
, int flags
,
1446 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1447 int whence
), FILE *metadata_fp
)
1450 struct dirent
*dirent
;
1451 struct dirent
*diriter
;
1456 /* Open trace directory */
1457 td
->dir
= opendir(path
);
1459 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1464 td
->dirfd
= open(path
, 0);
1465 if (td
->dirfd
< 0) {
1466 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1467 perror("Trace directory open");
1471 strncpy(td
->path
, path
, sizeof(td
->path
));
1472 td
->path
[sizeof(td
->path
) - 1] = '\0';
1475 * Keep the metadata file separate.
1478 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1480 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1481 goto error_metadata
;
1485 * Open each stream: for each file, try to open, check magic
1486 * number, and get the stream ID to add to the right location in
1490 dirent_len
= offsetof(struct dirent
, d_name
) +
1491 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1493 dirent
= malloc(dirent_len
);
1496 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1498 fprintf(stderr
, "[error] Readdir error.\n");
1503 /* Ignore hidden files, ., .. and metadata. */
1504 if (!strncmp(diriter
->d_name
, ".", 1)
1505 || !strcmp(diriter
->d_name
, "..")
1506 || !strcmp(diriter
->d_name
, "metadata"))
1508 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1509 flags
, packet_seek
);
1511 fprintf(stderr
, "[error] Open file stream error.\n");
1530 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1531 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1532 int whence
), FILE *metadata_fp
)
1534 struct ctf_trace
*td
;
1538 * If packet_seek is NULL, we provide our default version.
1541 packet_seek
= ctf_packet_seek
;
1543 td
= g_new0(struct ctf_trace
, 1);
1545 switch (flags
& O_ACCMODE
) {
1548 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1551 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1556 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1559 fprintf(stderr
, "[error] Incorrect open flags.\n");
1570 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1571 struct mmap_stream
*mmap_info
)
1573 pos
->mmap_offset
= 0;
1574 pos
->packet_size
= 0;
1575 pos
->content_size
= 0;
1576 pos
->content_size_loc
= NULL
;
1577 pos
->fd
= mmap_info
->fd
;
1578 pos
->base_mma
= NULL
;
1582 pos
->packet_index
= NULL
;
1583 pos
->prot
= PROT_READ
;
1584 pos
->flags
= MAP_PRIVATE
;
1585 pos
->parent
.rw_table
= read_dispatch_table
;
1586 pos
->parent
.event_cb
= ctf_read_event
;
1590 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1591 struct ctf_file_stream
*file_stream
)
1593 struct ctf_stream_declaration
*stream
;
1594 uint64_t stream_id
= 0;
1597 file_stream
->parent
.stream_id
= stream_id
;
1598 if (stream_id
>= td
->streams
->len
) {
1599 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1600 "in metadata.\n", stream_id
);
1604 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1606 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1607 "in metadata.\n", stream_id
);
1611 file_stream
->parent
.stream_class
= stream
;
1612 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1618 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1619 struct mmap_stream
*mmap_info
,
1620 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1624 struct ctf_file_stream
*file_stream
;
1626 file_stream
= g_new0(struct ctf_file_stream
, 1);
1627 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1628 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1630 file_stream
->pos
.packet_seek
= packet_seek
;
1632 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1637 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1641 /* Add stream file to stream class */
1642 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1643 &file_stream
->parent
);
1647 if (file_stream
->parent
.trace_packet_header
)
1648 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1650 g_free(file_stream
);
1654 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1655 struct mmap_stream_list
*mmap_list
,
1656 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1661 struct mmap_stream
*mmap_info
;
1663 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1669 * for each stream, try to open, check magic number, and get the
1670 * stream ID to add to the right location in the stream array.
1672 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1673 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1675 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1687 struct trace_descriptor
*ctf_open_mmap_trace(
1688 struct mmap_stream_list
*mmap_list
,
1689 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1693 struct ctf_trace
*td
;
1697 fprintf(stderr
, "[error] No metadata file pointer associated, "
1698 "required for mmap parsing\n");
1702 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1705 td
= g_new0(struct ctf_trace
, 1);
1706 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1719 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1721 ctf_fini_pos(&file_stream
->pos
);
1722 close(file_stream
->pos
.fd
);
1726 void ctf_close_trace(struct trace_descriptor
*tdp
)
1728 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1732 for (i
= 0; i
< td
->streams
->len
; i
++) {
1733 struct ctf_stream_declaration
*stream
;
1736 stream
= g_ptr_array_index(td
->streams
, i
);
1739 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1740 struct ctf_file_stream
*file_stream
;
1741 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1742 ctf_close_file_stream(file_stream
);
1746 g_ptr_array_free(td
->streams
, TRUE
);
1749 if (td
->event_declarations
) {
1750 for (i
= 0; i
< td
->event_declarations
->len
; i
++) {
1751 struct bt_ctf_event_decl
*event
;
1753 event
= g_ptr_array_index(td
->event_declarations
, i
);
1754 if (event
->context_decl
)
1755 g_ptr_array_free(event
->context_decl
, TRUE
);
1756 if (event
->fields_decl
)
1757 g_ptr_array_free(event
->fields_decl
, TRUE
);
1758 if (event
->packet_header_decl
)
1759 g_ptr_array_free(event
->packet_header_decl
, TRUE
);
1760 if (event
->event_context_decl
)
1761 g_ptr_array_free(event
->event_context_decl
, TRUE
);
1762 if (event
->event_header_decl
)
1763 g_ptr_array_free(event
->event_header_decl
, TRUE
);
1764 if (event
->packet_context_decl
)
1765 g_ptr_array_free(event
->packet_context_decl
, TRUE
);
1768 g_ptr_array_free(td
->event_declarations
, TRUE
);
1775 void ctf_set_context(struct trace_descriptor
*descriptor
,
1776 struct bt_context
*ctx
)
1778 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1785 void ctf_set_handle(struct trace_descriptor
*descriptor
,
1786 struct bt_trace_handle
*handle
)
1788 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1791 td
->handle
= handle
;
1794 void __attribute__((constructor
)) ctf_init(void)
1798 ctf_format
.name
= g_quark_from_static_string("ctf");
1799 ret
= bt_register_format(&ctf_format
);
1803 /* TODO: finalize */