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
;
340 /* we just read the event, it is consumed when used by the caller */
341 stream
->consumed
= 0;
344 * This is the EOF check after we've advanced the position in
347 if (unlikely(pos
->offset
== EOF
))
349 assert(pos
->offset
< pos
->content_size
);
351 /* Read event header */
352 if (likely(stream
->stream_event_header
)) {
353 struct definition_integer
*integer_definition
;
354 struct definition
*variant
;
356 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
359 /* lookup event id */
360 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
361 if (integer_definition
) {
362 id
= integer_definition
->value
._unsigned
;
364 struct definition_enum
*enum_definition
;
366 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
367 if (enum_definition
) {
368 id
= enum_definition
->integer
->value
._unsigned
;
372 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
374 integer_definition
= lookup_integer(variant
, "id", FALSE
);
375 if (integer_definition
) {
376 id
= integer_definition
->value
._unsigned
;
379 stream
->event_id
= id
;
381 /* lookup timestamp */
382 stream
->has_timestamp
= 0;
383 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
384 if (integer_definition
) {
385 ctf_update_timestamp(stream
, integer_definition
);
386 stream
->has_timestamp
= 1;
389 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
390 if (integer_definition
) {
391 ctf_update_timestamp(stream
, integer_definition
);
392 stream
->has_timestamp
= 1;
398 /* Read stream-declared event context */
399 if (stream
->stream_event_context
) {
400 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
405 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
406 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
409 event
= g_ptr_array_index(stream
->events_by_id
, id
);
410 if (unlikely(!event
)) {
411 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
415 /* Read event-declared event context */
416 if (event
->event_context
) {
417 ret
= generic_rw(ppos
, &event
->event_context
->p
);
422 /* Read event payload */
423 if (likely(event
->event_fields
)) {
424 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
432 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
437 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream_definition
*stream
)
439 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
440 struct ctf_event_definition
*event
;
444 id
= stream
->event_id
;
446 /* print event header */
447 if (likely(stream
->stream_event_header
)) {
448 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
453 /* print stream-declared event context */
454 if (stream
->stream_event_context
) {
455 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
460 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
461 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
464 event
= g_ptr_array_index(stream
->events_by_id
, id
);
465 if (unlikely(!event
)) {
466 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
470 /* print event-declared event context */
471 if (event
->event_context
) {
472 ret
= generic_rw(pos
, &event
->event_context
->p
);
477 /* Read and print event payload */
478 if (likely(event
->event_fields
)) {
479 ret
= generic_rw(pos
, &event
->event_fields
->p
);
487 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
491 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
494 pos
->mmap_offset
= 0;
495 pos
->packet_size
= 0;
496 pos
->content_size
= 0;
497 pos
->content_size_loc
= NULL
;
498 pos
->base_mma
= NULL
;
503 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
504 sizeof(struct packet_index
));
506 pos
->packet_index
= NULL
;
507 switch (open_flags
& O_ACCMODE
) {
509 pos
->prot
= PROT_READ
;
510 pos
->flags
= MAP_PRIVATE
;
511 pos
->parent
.rw_table
= read_dispatch_table
;
512 pos
->parent
.event_cb
= ctf_read_event
;
515 pos
->prot
= PROT_WRITE
; /* Write has priority */
516 pos
->flags
= MAP_SHARED
;
517 pos
->parent
.rw_table
= write_dispatch_table
;
518 pos
->parent
.event_cb
= ctf_write_event
;
520 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
527 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
531 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
532 *pos
->content_size_loc
= pos
->offset
;
535 ret
= munmap_align(pos
->base_mma
);
537 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
542 (void) g_array_free(pos
->packet_index
, TRUE
);
546 * for SEEK_CUR: go to next packet.
547 * for SEEK_POS: go to packet numer (index).
549 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
551 struct ctf_stream_pos
*pos
=
552 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
553 struct ctf_file_stream
*file_stream
=
554 container_of(pos
, struct ctf_file_stream
, pos
);
557 struct packet_index
*packet_index
;
559 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
560 *pos
->content_size_loc
= pos
->offset
;
564 ret
= munmap_align(pos
->base_mma
);
566 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
570 pos
->base_mma
= NULL
;
574 * The caller should never ask for ctf_move_pos across packets,
575 * except to get exactly at the beginning of the next packet.
577 if (pos
->prot
== PROT_WRITE
) {
580 /* The writer will add padding */
581 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
584 assert(index
== 0); /* only seek supported for now */
590 pos
->content_size
= -1U; /* Unknown at this point */
591 pos
->packet_size
= WRITE_PACKET_LEN
;
592 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
593 pos
->packet_size
/ CHAR_BIT
);
601 uint64_t events_discarded_diff
;
603 if (pos
->offset
== EOF
) {
606 /* For printing discarded event count */
607 packet_index
= &g_array_index(pos
->packet_index
,
608 struct packet_index
, pos
->cur_index
);
609 events_discarded_diff
= packet_index
->events_discarded
;
610 file_stream
->parent
.prev_timestamp_end
=
611 packet_index
->timestamp_end
;
612 if (pos
->cur_index
> 0) {
613 packet_index
= &g_array_index(pos
->packet_index
,
616 events_discarded_diff
-= packet_index
->events_discarded
;
618 * Deal with 32-bit wrap-around if the
619 * tracer provided a 32-bit field.
621 if (packet_index
->events_discarded_len
== 32) {
622 events_discarded_diff
= (uint32_t) events_discarded_diff
;
625 file_stream
->parent
.events_discarded
= events_discarded_diff
;
626 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
627 /* The reader will expect us to skip padding */
632 pos
->cur_index
= index
;
633 file_stream
->parent
.prev_timestamp
= 0;
634 file_stream
->parent
.prev_timestamp_end
= 0;
639 if (pos
->cur_index
>= pos
->packet_index
->len
) {
641 * When a stream reaches the end of the
642 * file, we need to show the number of
643 * events discarded ourselves, because
644 * there is no next event scheduled to
645 * be printed in the output.
647 if (file_stream
->parent
.events_discarded
) {
649 * We need to check if we are in trace
650 * read or called from packet indexing.
651 * In this last case, the collection is
652 * not there, so we cannot print the
655 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
657 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events at end of stream between [",
658 file_stream
->parent
.events_discarded
);
659 ctf_print_timestamp(stderr
, &file_stream
->parent
,
660 file_stream
->parent
.prev_timestamp
);
661 fprintf(stderr
, "] and [");
662 ctf_print_timestamp(stderr
, &file_stream
->parent
,
663 file_stream
->parent
.prev_timestamp_end
);
664 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
667 file_stream
->parent
.events_discarded
= 0;
672 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
674 pos
->mmap_offset
= packet_index
->offset
;
676 /* Lookup context/packet size in index */
677 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
678 pos
->content_size
= packet_index
->content_size
;
679 pos
->packet_size
= packet_index
->packet_size
;
680 if (packet_index
->data_offset
< packet_index
->content_size
) {
681 pos
->offset
= 0; /* will read headers */
682 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
684 pos
->offset
= packet_index
->data_offset
;
686 goto read_next_packet
;
692 /* map new base. Need mapping length from header. */
693 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
694 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
695 if (pos
->base_mma
== MAP_FAILED
) {
696 fprintf(stderr
, "[error] mmap error %s.\n",
701 /* update trace_packet_header and stream_packet_context */
702 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
703 /* Read packet header */
704 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
707 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
708 /* Read packet context */
709 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
715 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
721 len
= fread(&magic
, sizeof(magic
), 1, fp
);
725 if (magic
== TSDL_MAGIC
) {
727 td
->byte_order
= BYTE_ORDER
;
728 CTF_TRACE_SET_FIELD(td
, byte_order
);
729 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
731 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
732 LITTLE_ENDIAN
: BIG_ENDIAN
;
733 CTF_TRACE_SET_FIELD(td
, byte_order
);
741 * Returns 0 on success, -1 on error.
744 int check_version(unsigned int major
, unsigned int minor
)
759 /* eventually return an error instead of warning */
761 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
767 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
770 struct metadata_packet_header header
;
771 size_t readlen
, writelen
, toread
;
772 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
775 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
779 if (td
->byte_order
!= BYTE_ORDER
) {
780 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
781 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
782 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
783 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
786 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
787 if (header
.compression_scheme
) {
788 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
789 header
.compression_scheme
);
792 if (header
.encryption_scheme
) {
793 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
794 header
.encryption_scheme
);
797 if (header
.checksum_scheme
) {
798 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
799 header
.checksum_scheme
);
802 if (check_version(header
.major
, header
.minor
) < 0)
804 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
805 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
806 CTF_TRACE_SET_FIELD(td
, uuid
);
808 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
812 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
815 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
820 if (babeltrace_debug
) {
822 fprintf(stderr
, "[debug] metadata packet read: %s\n",
826 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
827 if (writelen
< readlen
) {
837 ret
= 0; /* continue reading next packet */
844 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
845 ret
= fseek(in
, toread
, SEEK_CUR
);
847 fprintf(stderr
, "[warning] Missing padding at end of file\n");
854 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
863 * Using strlen on *buf instead of size of open_memstream
864 * because its size includes garbage at the end (after final
865 * \0). This is the allocated size, not the actual string size.
867 out
= babeltrace_open_memstream(buf
, &size
);
869 perror("Metadata open_memstream");
873 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
882 /* close to flush the buffer */
883 ret
= babeltrace_close_memstream(buf
, &size
, out
);
885 perror("babeltrace_flush_memstream");
890 /* open for reading */
891 *fp
= babeltrace_fmemopen(*buf
, strlen(*buf
), "rb");
893 perror("Metadata fmemopen");
900 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
901 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
902 int whence
), FILE *metadata_fp
)
904 struct ctf_scanner
*scanner
;
905 struct ctf_file_stream
*metadata_stream
;
910 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
913 metadata_stream
->pos
.packet_seek
= packet_seek
;
915 fprintf(stderr
, "[error] packet_seek function undefined.\n");
923 td
->metadata
= &metadata_stream
->parent
;
924 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
925 if (metadata_stream
->pos
.fd
< 0) {
926 fprintf(stderr
, "Unable to open metadata.\n");
927 return metadata_stream
->pos
.fd
;
930 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
932 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
933 perror("Metadata stream open");
938 if (babeltrace_debug
)
941 if (packet_metadata(td
, fp
)) {
942 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
944 goto end_packet_read
;
946 unsigned int major
, minor
;
949 td
->byte_order
= BYTE_ORDER
;
951 /* Check text-only metadata header and version */
952 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
954 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
955 if (check_version(major
, minor
) < 0) {
957 goto end_packet_read
;
962 scanner
= ctf_scanner_alloc(fp
);
964 fprintf(stderr
, "[error] Error allocating scanner\n");
966 goto end_scanner_alloc
;
968 ret
= ctf_scanner_append_ast(scanner
);
970 fprintf(stderr
, "[error] Error creating AST\n");
974 if (babeltrace_debug
) {
975 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
977 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
982 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
984 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
987 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
990 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
994 ctf_scanner_free(scanner
);
1001 close(metadata_stream
->pos
.fd
);
1003 g_free(metadata_stream
);
1008 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1009 struct ctf_stream_definition
*stream
,
1010 struct ctf_event_declaration
*event
)
1012 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1014 if (event
->context_decl
) {
1015 struct definition
*definition
=
1016 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1017 stream
->parent_def_scope
, 0, 0, "event.context");
1021 stream_event
->event_context
= container_of(definition
,
1022 struct definition_struct
, p
);
1023 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1025 if (event
->fields_decl
) {
1026 struct definition
*definition
=
1027 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1028 stream
->parent_def_scope
, 0, 0, "event.fields");
1032 stream_event
->event_fields
= container_of(definition
,
1033 struct definition_struct
, p
);
1034 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1036 stream_event
->stream
= stream
;
1037 return stream_event
;
1040 if (stream_event
->event_fields
)
1041 definition_unref(&stream_event
->event_fields
->p
);
1042 if (stream_event
->event_context
)
1043 definition_unref(&stream_event
->event_context
->p
);
1048 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1050 struct ctf_stream_declaration
*stream_class
;
1054 if (stream
->stream_definitions_created
)
1057 stream_class
= stream
->stream_class
;
1059 if (stream_class
->packet_context_decl
) {
1060 struct definition
*definition
=
1061 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1062 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1067 stream
->stream_packet_context
= container_of(definition
,
1068 struct definition_struct
, p
);
1069 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1071 if (stream_class
->event_header_decl
) {
1072 struct definition
*definition
=
1073 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1074 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1079 stream
->stream_event_header
=
1080 container_of(definition
, struct definition_struct
, p
);
1081 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1083 if (stream_class
->event_context_decl
) {
1084 struct definition
*definition
=
1085 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1086 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1091 stream
->stream_event_context
=
1092 container_of(definition
, struct definition_struct
, p
);
1093 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1095 stream
->events_by_id
= g_ptr_array_new();
1096 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1097 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1098 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1099 struct ctf_event_definition
*stream_event
;
1103 stream_event
= create_event_definitions(td
, stream
, event
);
1106 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1111 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1112 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1114 g_free(stream_event
);
1116 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1118 if (stream
->stream_event_context
)
1119 definition_unref(&stream
->stream_event_context
->p
);
1120 if (stream
->stream_event_header
)
1121 definition_unref(&stream
->stream_event_header
->p
);
1122 if (stream
->stream_packet_context
)
1123 definition_unref(&stream
->stream_packet_context
->p
);
1129 int create_stream_packet_index(struct ctf_trace
*td
,
1130 struct ctf_file_stream
*file_stream
)
1132 struct ctf_stream_declaration
*stream
;
1134 struct ctf_stream_pos
*pos
;
1135 struct stat filestats
;
1136 struct packet_index packet_index
;
1137 int first_packet
= 1;
1140 pos
= &file_stream
->pos
;
1142 ret
= fstat(pos
->fd
, &filestats
);
1146 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1149 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1150 uint64_t stream_id
= 0;
1152 if (pos
->base_mma
) {
1153 /* unmap old base */
1154 ret
= munmap_align(pos
->base_mma
);
1156 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1160 pos
->base_mma
= NULL
;
1162 /* map new base. Need mapping length from header. */
1163 pos
->base_mma
= mmap_align(MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1164 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1165 assert(pos
->base_mma
!= MAP_FAILED
);
1166 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1167 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1168 pos
->offset
= 0; /* Position of the packet header */
1170 packet_index
.offset
= pos
->mmap_offset
;
1171 packet_index
.content_size
= 0;
1172 packet_index
.packet_size
= 0;
1173 packet_index
.timestamp_begin
= 0;
1174 packet_index
.timestamp_end
= 0;
1175 packet_index
.events_discarded
= 0;
1177 /* read and check header, set stream id (and check) */
1178 if (file_stream
->parent
.trace_packet_header
) {
1179 /* Read packet header */
1180 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1183 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1184 if (len_index
>= 0) {
1185 struct definition
*field
;
1188 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1189 magic
= get_unsigned_int(field
);
1190 if (magic
!= CTF_MAGIC
) {
1191 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1193 file_stream
->pos
.packet_index
->len
,
1194 (ssize_t
) pos
->mmap_offset
);
1200 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1201 if (len_index
>= 0) {
1202 struct definition_array
*defarray
;
1203 struct definition
*field
;
1205 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1207 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1208 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1209 defarray
= container_of(field
, struct definition_array
, p
);
1210 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1212 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1213 struct definition
*elem
;
1215 elem
= array_index(defarray
, i
);
1216 uuidval
[i
] = get_unsigned_int(elem
);
1218 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1220 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1226 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1227 if (len_index
>= 0) {
1228 struct definition
*field
;
1230 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1231 stream_id
= get_unsigned_int(field
);
1235 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1236 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1240 file_stream
->parent
.stream_id
= stream_id
;
1241 if (stream_id
>= td
->streams
->len
) {
1242 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1245 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1247 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1250 file_stream
->parent
.stream_class
= stream
;
1251 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1257 if (file_stream
->parent
.stream_packet_context
) {
1258 /* Read packet context */
1259 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1262 /* read content size from header */
1263 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1264 if (len_index
>= 0) {
1265 struct definition
*field
;
1267 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1268 packet_index
.content_size
= get_unsigned_int(field
);
1270 /* Use file size for packet size */
1271 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1274 /* read packet size from header */
1275 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1276 if (len_index
>= 0) {
1277 struct definition
*field
;
1279 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1280 packet_index
.packet_size
= get_unsigned_int(field
);
1282 /* Use content size if non-zero, else file size */
1283 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1286 /* read timestamp begin from header */
1287 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1288 if (len_index
>= 0) {
1289 struct definition
*field
;
1291 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1292 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1295 /* read timestamp end from header */
1296 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1297 if (len_index
>= 0) {
1298 struct definition
*field
;
1300 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1301 packet_index
.timestamp_end
= get_unsigned_int(field
);
1304 /* read events discarded from header */
1305 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1306 if (len_index
>= 0) {
1307 struct definition
*field
;
1309 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1310 packet_index
.events_discarded
= get_unsigned_int(field
);
1311 packet_index
.events_discarded_len
= get_int_len(field
);
1314 /* Use file size for packet size */
1315 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1316 /* Use content size if non-zero, else file size */
1317 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1320 /* Validate content size and packet size values */
1321 if (packet_index
.content_size
> packet_index
.packet_size
) {
1322 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1323 packet_index
.content_size
, packet_index
.packet_size
);
1327 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1328 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1329 packet_index
.packet_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1333 /* Save position after header and context */
1334 packet_index
.data_offset
= pos
->offset
;
1336 /* add index to packet array */
1337 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1339 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1342 /* Move pos back to beginning of file */
1343 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1349 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1353 if (td
->packet_header_decl
) {
1354 struct definition
*definition
=
1355 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1356 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1361 stream
->trace_packet_header
=
1362 container_of(definition
, struct definition_struct
, p
);
1363 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1373 * Note: many file streams can inherit from the same stream class
1374 * description (metadata).
1377 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1378 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1382 struct ctf_file_stream
*file_stream
;
1384 ret
= openat(td
->dirfd
, path
, flags
);
1386 perror("File stream openat()");
1389 file_stream
= g_new0(struct ctf_file_stream
, 1);
1392 file_stream
->pos
.packet_seek
= packet_seek
;
1394 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1399 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1400 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1404 * For now, only a single slock is supported.
1406 file_stream
->parent
.current_clock
= td
->single_clock
;
1407 ret
= create_stream_packet_index(td
, file_stream
);
1410 /* Add stream file to stream class */
1411 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1412 &file_stream
->parent
);
1416 if (file_stream
->parent
.trace_packet_header
)
1417 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1419 ctf_fini_pos(&file_stream
->pos
);
1420 close(file_stream
->pos
.fd
);
1421 g_free(file_stream
);
1427 int ctf_open_trace_read(struct ctf_trace
*td
,
1428 const char *path
, int flags
,
1429 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1430 int whence
), FILE *metadata_fp
)
1433 struct dirent
*dirent
;
1434 struct dirent
*diriter
;
1439 /* Open trace directory */
1440 td
->dir
= opendir(path
);
1442 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1447 td
->dirfd
= open(path
, 0);
1448 if (td
->dirfd
< 0) {
1449 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1450 perror("Trace directory open");
1454 strncpy(td
->path
, path
, sizeof(td
->path
));
1455 td
->path
[sizeof(td
->path
) - 1] = '\0';
1458 * Keep the metadata file separate.
1461 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1463 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1464 goto error_metadata
;
1468 * Open each stream: for each file, try to open, check magic
1469 * number, and get the stream ID to add to the right location in
1473 dirent_len
= offsetof(struct dirent
, d_name
) +
1474 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1476 dirent
= malloc(dirent_len
);
1479 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1481 fprintf(stderr
, "[error] Readdir error.\n");
1486 /* Ignore hidden files, ., .. and metadata. */
1487 if (!strncmp(diriter
->d_name
, ".", 1)
1488 || !strcmp(diriter
->d_name
, "..")
1489 || !strcmp(diriter
->d_name
, "metadata"))
1491 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1492 flags
, packet_seek
);
1494 fprintf(stderr
, "[error] Open file stream error.\n");
1513 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1514 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1515 int whence
), FILE *metadata_fp
)
1517 struct ctf_trace
*td
;
1521 * If packet_seek is NULL, we provide our default version.
1524 packet_seek
= ctf_packet_seek
;
1526 td
= g_new0(struct ctf_trace
, 1);
1528 switch (flags
& O_ACCMODE
) {
1531 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1534 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1539 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1542 fprintf(stderr
, "[error] Incorrect open flags.\n");
1553 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1554 struct mmap_stream
*mmap_info
)
1556 pos
->mmap_offset
= 0;
1557 pos
->packet_size
= 0;
1558 pos
->content_size
= 0;
1559 pos
->content_size_loc
= NULL
;
1560 pos
->fd
= mmap_info
->fd
;
1561 pos
->base_mma
= NULL
;
1565 pos
->packet_index
= NULL
;
1566 pos
->prot
= PROT_READ
;
1567 pos
->flags
= MAP_PRIVATE
;
1568 pos
->parent
.rw_table
= read_dispatch_table
;
1569 pos
->parent
.event_cb
= ctf_read_event
;
1573 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1574 struct ctf_file_stream
*file_stream
)
1576 struct ctf_stream_declaration
*stream
;
1577 uint64_t stream_id
= 0;
1580 file_stream
->parent
.stream_id
= stream_id
;
1581 if (stream_id
>= td
->streams
->len
) {
1582 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1583 "in metadata.\n", stream_id
);
1587 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1589 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1590 "in metadata.\n", stream_id
);
1594 file_stream
->parent
.stream_class
= stream
;
1595 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1601 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1602 struct mmap_stream
*mmap_info
,
1603 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1607 struct ctf_file_stream
*file_stream
;
1609 file_stream
= g_new0(struct ctf_file_stream
, 1);
1610 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1612 file_stream
->pos
.packet_seek
= packet_seek
;
1614 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1619 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1623 /* Add stream file to stream class */
1624 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1625 &file_stream
->parent
);
1629 if (file_stream
->parent
.trace_packet_header
)
1630 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1632 g_free(file_stream
);
1636 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1637 struct mmap_stream_list
*mmap_list
,
1638 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1643 struct mmap_stream
*mmap_info
;
1645 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1651 * for each stream, try to open, check magic number, and get the
1652 * stream ID to add to the right location in the stream array.
1654 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1655 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1657 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1669 struct trace_descriptor
*ctf_open_mmap_trace(
1670 struct mmap_stream_list
*mmap_list
,
1671 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1675 struct ctf_trace
*td
;
1679 fprintf(stderr
, "[error] No metadata file pointer associated, "
1680 "required for mmap parsing\n");
1684 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1687 td
= g_new0(struct ctf_trace
, 1);
1688 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1701 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1703 ctf_fini_pos(&file_stream
->pos
);
1704 close(file_stream
->pos
.fd
);
1708 void ctf_close_trace(struct trace_descriptor
*tdp
)
1710 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1714 for (i
= 0; i
< td
->streams
->len
; i
++) {
1715 struct ctf_stream_declaration
*stream
;
1718 stream
= g_ptr_array_index(td
->streams
, i
);
1721 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1722 struct ctf_file_stream
*file_stream
;
1723 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1724 ctf_close_file_stream(file_stream
);
1728 g_ptr_array_free(td
->streams
, TRUE
);
1731 if (td
->event_declarations
) {
1732 for (i
= 0; i
< td
->event_declarations
->len
; i
++) {
1733 struct bt_ctf_event_decl
*event
;
1735 event
= g_ptr_array_index(td
->event_declarations
, i
);
1736 if (event
->context_decl
)
1737 g_ptr_array_free(event
->context_decl
, TRUE
);
1738 if (event
->fields_decl
)
1739 g_ptr_array_free(event
->fields_decl
, TRUE
);
1740 if (event
->packet_header_decl
)
1741 g_ptr_array_free(event
->packet_header_decl
, TRUE
);
1742 if (event
->event_context_decl
)
1743 g_ptr_array_free(event
->event_context_decl
, TRUE
);
1744 if (event
->event_header_decl
)
1745 g_ptr_array_free(event
->event_header_decl
, TRUE
);
1746 if (event
->packet_context_decl
)
1747 g_ptr_array_free(event
->packet_context_decl
, TRUE
);
1750 g_ptr_array_free(td
->event_declarations
, TRUE
);
1757 void ctf_set_context(struct trace_descriptor
*descriptor
,
1758 struct bt_context
*ctx
)
1760 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1767 void ctf_set_handle(struct trace_descriptor
*descriptor
,
1768 struct bt_trace_handle
*handle
)
1770 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1773 td
->handle
= handle
;
1776 void __attribute__((constructor
)) ctf_init(void)
1780 ctf_format
.name
= g_quark_from_static_string("ctf");
1781 ret
= bt_register_format(&ctf_format
);
1785 /* TODO: finalize */