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>
27 #include <uuid/uuid.h>
31 #include <sys/types.h>
39 #include "metadata/ctf-scanner.h"
40 #include "metadata/ctf-parser.h"
41 #include "metadata/ctf-ast.h"
44 * We currently simply map a page to read the packet header and packet
45 * context to get the packet length and content length. (in bits)
47 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
49 #define UUID_LEN 16 /* uuid by value len */
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
55 #define NSEC_PER_SEC 1000000000ULL
62 uint64_t opt_clock_offset
;
67 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
68 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
69 int whence
), FILE *metadata_fp
);
71 struct trace_descriptor
*ctf_open_mmap_trace(
72 struct mmap_stream_list
*mmap_list
,
73 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
77 void ctf_close_trace(struct trace_descriptor
*descriptor
);
80 rw_dispatch read_dispatch_table
[] = {
81 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
82 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
83 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
84 [ CTF_TYPE_STRING
] = ctf_string_read
,
85 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
86 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
87 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
88 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
92 rw_dispatch write_dispatch_table
[] = {
93 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
94 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
95 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
96 [ CTF_TYPE_STRING
] = ctf_string_write
,
97 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
98 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
99 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
100 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
104 struct format ctf_format
= {
105 .open_trace
= ctf_open_trace
,
106 .open_mmap_trace
= ctf_open_mmap_trace
,
107 .close_trace
= ctf_close_trace
,
111 void ctf_update_timestamp(struct ctf_stream
*stream
,
112 struct definition_integer
*integer_definition
)
114 struct declaration_integer
*integer_declaration
=
115 integer_definition
->declaration
;
116 uint64_t oldval
, newval
, updateval
;
118 if (unlikely(integer_declaration
->len
== 64)) {
119 stream
->timestamp
= integer_definition
->value
._unsigned
;
123 oldval
= stream
->timestamp
;
124 oldval
&= (1ULL << integer_declaration
->len
) - 1;
125 newval
= integer_definition
->value
._unsigned
;
126 /* Test for overflow by comparing low bits */
128 newval
+= 1ULL << integer_declaration
->len
;
129 /* updateval contains old high bits, and new low bits (sum) */
130 updateval
= stream
->timestamp
;
131 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
133 stream
->prev_timestamp
= stream
->timestamp
;
134 stream
->timestamp
= updateval
;
137 void ctf_print_timestamp(FILE *fp
,
138 struct ctf_stream
*stream
,
141 uint64_t ts_sec
= 0, ts_nsec
;
142 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
143 struct trace_collection
*tc
= trace
->collection
;
144 uint64_t tc_offset
= tc
->single_clock_offset_avg
;
149 if (!opt_clock_raw
) {
150 ts_nsec
+= tc_offset
;
152 ts_sec
+= opt_clock_offset
;
154 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
155 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
157 if (!opt_clock_seconds
) {
159 time_t time_s
= (time_t) ts_sec
;
161 if (!opt_clock_gmt
) {
164 res
= localtime_r(&time_s
, &tm
);
166 fprintf(stderr
, "[warning] Unable to get localtime.\n");
172 res
= gmtime_r(&time_s
, &tm
);
174 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
178 if (opt_clock_date
) {
182 /* Print date and time */
183 res
= strftime(timestr
, sizeof(timestr
),
186 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
189 fprintf(fp
, "%s", timestr
);
191 /* Print time in HH:MM:SS.ns */
192 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
193 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
197 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
205 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
207 struct ctf_stream_pos
*pos
=
208 container_of(ppos
, struct ctf_stream_pos
, parent
);
209 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
210 struct ctf_stream_event
*event
;
214 /* We need to check for EOF here for empty files. */
215 if (unlikely(pos
->offset
== EOF
))
218 ctf_pos_get_event(pos
);
221 * This is the EOF check after we've advanced the position in
224 if (unlikely(pos
->offset
== EOF
))
226 assert(pos
->offset
< pos
->content_size
);
228 /* Read event header */
229 if (likely(stream
->stream_event_header
)) {
230 struct definition_integer
*integer_definition
;
231 struct definition
*variant
;
233 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
236 /* lookup event id */
237 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
238 if (integer_definition
) {
239 id
= integer_definition
->value
._unsigned
;
241 struct definition_enum
*enum_definition
;
243 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
244 if (enum_definition
) {
245 id
= enum_definition
->integer
->value
._unsigned
;
249 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
251 integer_definition
= lookup_integer(variant
, "id", FALSE
);
252 if (integer_definition
) {
253 id
= integer_definition
->value
._unsigned
;
256 stream
->event_id
= id
;
258 /* lookup timestamp */
259 stream
->has_timestamp
= 0;
260 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
261 if (integer_definition
) {
262 ctf_update_timestamp(stream
, integer_definition
);
263 stream
->has_timestamp
= 1;
266 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
267 if (integer_definition
) {
268 ctf_update_timestamp(stream
, integer_definition
);
269 stream
->has_timestamp
= 1;
275 /* Read stream-declared event context */
276 if (stream
->stream_event_context
) {
277 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
282 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
283 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
286 event
= g_ptr_array_index(stream
->events_by_id
, id
);
287 if (unlikely(!event
)) {
288 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
292 /* Read event-declared event context */
293 if (event
->event_context
) {
294 ret
= generic_rw(ppos
, &event
->event_context
->p
);
299 /* Read event payload */
300 if (likely(event
->event_fields
)) {
301 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
309 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
314 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
316 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
317 struct ctf_stream_event
*event
;
321 id
= stream
->event_id
;
323 /* print event header */
324 if (likely(stream
->stream_event_header
)) {
325 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
330 /* print stream-declared event context */
331 if (stream
->stream_event_context
) {
332 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
337 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
338 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
341 event
= g_ptr_array_index(stream
->events_by_id
, id
);
342 if (unlikely(!event
)) {
343 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
347 /* print event-declared event context */
348 if (event
->event_context
) {
349 ret
= generic_rw(pos
, &event
->event_context
->p
);
354 /* Read and print event payload */
355 if (likely(event
->event_fields
)) {
356 ret
= generic_rw(pos
, &event
->event_fields
->p
);
364 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
368 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
371 pos
->mmap_offset
= 0;
372 pos
->packet_size
= 0;
373 pos
->content_size
= 0;
374 pos
->content_size_loc
= NULL
;
380 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
381 sizeof(struct packet_index
));
383 pos
->packet_index
= NULL
;
384 switch (open_flags
& O_ACCMODE
) {
386 pos
->prot
= PROT_READ
;
387 pos
->flags
= MAP_PRIVATE
;
388 pos
->parent
.rw_table
= read_dispatch_table
;
389 pos
->parent
.event_cb
= ctf_read_event
;
392 pos
->prot
= PROT_WRITE
; /* Write has priority */
393 pos
->flags
= MAP_SHARED
;
394 pos
->parent
.rw_table
= write_dispatch_table
;
395 pos
->parent
.event_cb
= ctf_write_event
;
397 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
404 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
408 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
409 *pos
->content_size_loc
= pos
->offset
;
412 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
414 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
419 (void) g_array_free(pos
->packet_index
, TRUE
);
422 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
424 struct ctf_file_stream
*file_stream
=
425 container_of(pos
, struct ctf_file_stream
, pos
);
428 struct packet_index
*index
;
430 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
431 *pos
->content_size_loc
= pos
->offset
;
435 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
437 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
445 * The caller should never ask for ctf_move_pos across packets,
446 * except to get exactly at the beginning of the next packet.
448 if (pos
->prot
== PROT_WRITE
) {
451 /* The writer will add padding */
452 assert(pos
->offset
+ offset
== pos
->packet_size
);
453 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
456 assert(offset
== 0); /* only seek supported for now */
462 pos
->content_size
= -1U; /* Unknown at this point */
463 pos
->packet_size
= WRITE_PACKET_LEN
;
464 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
465 pos
->packet_size
/ CHAR_BIT
);
473 uint32_t events_discarded_diff
;
475 if (pos
->offset
== EOF
) {
478 /* For printing discarded event count */
479 index
= &g_array_index(pos
->packet_index
,
480 struct packet_index
, pos
->cur_index
);
481 events_discarded_diff
= index
->events_discarded
;
482 file_stream
->parent
.prev_timestamp_end
=
483 index
->timestamp_end
;
484 if (pos
->cur_index
> 0) {
485 index
= &g_array_index(pos
->packet_index
,
488 events_discarded_diff
-= index
->events_discarded
;
490 file_stream
->parent
.events_discarded
= events_discarded_diff
;
491 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
492 /* The reader will expect us to skip padding */
493 assert(pos
->offset
+ offset
== pos
->content_size
);
498 assert(offset
== 0); /* only seek supported for now */
500 file_stream
->parent
.prev_timestamp
= 0;
501 file_stream
->parent
.prev_timestamp_end
= 0;
506 if (pos
->cur_index
>= pos
->packet_index
->len
) {
508 * When a stream reaches the end of the
509 * file, we need to show the number of
510 * events discarded ourselves, because
511 * there is no next event scheduled to
512 * be printed in the output.
514 if (file_stream
->parent
.events_discarded
) {
516 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
517 file_stream
->parent
.events_discarded
);
518 ctf_print_timestamp(stderr
, &file_stream
->parent
,
519 file_stream
->parent
.prev_timestamp
);
520 fprintf(stderr
, "] and [");
521 ctf_print_timestamp(stderr
, &file_stream
->parent
,
522 file_stream
->parent
.prev_timestamp_end
);
523 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
525 file_stream
->parent
.events_discarded
= 0;
530 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
532 pos
->mmap_offset
= index
->offset
;
534 /* Lookup context/packet size in index */
535 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
536 pos
->content_size
= index
->content_size
;
537 pos
->packet_size
= index
->packet_size
;
538 if (index
->data_offset
< index
->content_size
) {
539 pos
->offset
= 0; /* will read headers */
540 } else if (index
->data_offset
== index
->content_size
) {
542 pos
->offset
= index
->data_offset
;
545 goto read_next_packet
;
551 /* map new base. Need mapping length from header. */
552 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
553 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
554 if (pos
->base
== MAP_FAILED
) {
555 fprintf(stderr
, "[error] mmap error %s.\n",
560 /* update trace_packet_header and stream_packet_context */
561 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
562 /* Read packet header */
563 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
566 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
567 /* Read packet context */
568 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
574 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
580 len
= fread(&magic
, sizeof(magic
), 1, fp
);
584 if (magic
== TSDL_MAGIC
) {
586 td
->byte_order
= BYTE_ORDER
;
587 CTF_TRACE_SET_FIELD(td
, byte_order
);
588 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
590 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
591 LITTLE_ENDIAN
: BIG_ENDIAN
;
592 CTF_TRACE_SET_FIELD(td
, byte_order
);
600 * Returns 0 on success, -1 on error.
603 int check_version(unsigned int major
, unsigned int minor
)
618 /* eventually return an error instead of warning */
620 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
626 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
629 struct metadata_packet_header header
;
630 size_t readlen
, writelen
, toread
;
631 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
634 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
638 if (td
->byte_order
!= BYTE_ORDER
) {
639 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
640 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
641 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
642 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
645 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
646 if (header
.compression_scheme
) {
647 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
648 header
.compression_scheme
);
651 if (header
.encryption_scheme
) {
652 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
653 header
.encryption_scheme
);
656 if (header
.checksum_scheme
) {
657 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
658 header
.checksum_scheme
);
661 if (check_version(header
.major
, header
.minor
) < 0)
663 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
664 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
665 CTF_TRACE_SET_FIELD(td
, uuid
);
667 if (uuid_compare(header
.uuid
, td
->uuid
))
671 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
674 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
679 if (babeltrace_debug
) {
681 fprintf(stderr
, "[debug] metadata packet read: %s\n",
685 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
686 if (writelen
< readlen
) {
696 ret
= 0; /* continue reading next packet */
703 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
704 ret
= fseek(in
, toread
, SEEK_CUR
);
706 fprintf(stderr
, "[warning] Missing padding at end of file\n");
713 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
722 * Using strlen on *buf instead of size of open_memstream
723 * because its size includes garbage at the end (after final
724 * \0). This is the allocated size, not the actual string size.
726 out
= open_memstream(buf
, &size
);
728 perror("Metadata open_memstream");
732 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
741 fclose(out
); /* flush the buffer */
743 /* open for reading */
744 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
746 perror("Metadata fmemopen");
753 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
754 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
755 int whence
), FILE *metadata_fp
)
757 struct ctf_scanner
*scanner
;
758 struct ctf_file_stream
*metadata_stream
;
763 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
766 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
768 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
776 td
->metadata
= &metadata_stream
->parent
;
777 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
778 if (metadata_stream
->pos
.fd
< 0) {
779 fprintf(stderr
, "Unable to open metadata.\n");
780 return metadata_stream
->pos
.fd
;
783 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
785 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
786 perror("Metadata stream open");
791 if (babeltrace_debug
)
794 if (packet_metadata(td
, fp
)) {
795 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
797 goto end_packet_read
;
799 unsigned int major
, minor
;
802 td
->byte_order
= BYTE_ORDER
;
804 /* Check text-only metadata header and version */
805 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
807 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
808 if (check_version(major
, minor
) < 0) {
810 goto end_packet_read
;
815 scanner
= ctf_scanner_alloc(fp
);
817 fprintf(stderr
, "[error] Error allocating scanner\n");
819 goto end_scanner_alloc
;
821 ret
= ctf_scanner_append_ast(scanner
);
823 fprintf(stderr
, "[error] Error creating AST\n");
827 if (babeltrace_debug
) {
828 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
830 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
835 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
837 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
840 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
843 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
847 ctf_scanner_free(scanner
);
853 close(metadata_stream
->pos
.fd
);
855 g_free(metadata_stream
);
860 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
861 struct ctf_stream
*stream
,
862 struct ctf_event
*event
)
864 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
866 if (event
->context_decl
) {
867 struct definition
*definition
=
868 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
869 stream
->parent_def_scope
, 0, 0, "event.context");
873 stream_event
->event_context
= container_of(definition
,
874 struct definition_struct
, p
);
875 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
877 if (event
->fields_decl
) {
878 struct definition
*definition
=
879 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
880 stream
->parent_def_scope
, 0, 0, "event.fields");
884 stream_event
->event_fields
= container_of(definition
,
885 struct definition_struct
, p
);
886 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
891 if (stream_event
->event_fields
)
892 definition_unref(&stream_event
->event_fields
->p
);
893 if (stream_event
->event_context
)
894 definition_unref(&stream_event
->event_context
->p
);
899 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
901 struct ctf_stream_class
*stream_class
;
905 if (stream
->stream_definitions_created
)
908 stream_class
= stream
->stream_class
;
910 if (stream_class
->packet_context_decl
) {
911 struct definition
*definition
=
912 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
913 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
918 stream
->stream_packet_context
= container_of(definition
,
919 struct definition_struct
, p
);
920 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
922 if (stream_class
->event_header_decl
) {
923 struct definition
*definition
=
924 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
925 stream
->parent_def_scope
, 0, 0, "stream.event.header");
930 stream
->stream_event_header
=
931 container_of(definition
, struct definition_struct
, p
);
932 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
934 if (stream_class
->event_context_decl
) {
935 struct definition
*definition
=
936 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
937 stream
->parent_def_scope
, 0, 0, "stream.event.context");
942 stream
->stream_event_context
=
943 container_of(definition
, struct definition_struct
, p
);
944 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
946 stream
->events_by_id
= g_ptr_array_new();
947 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
948 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
949 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
950 struct ctf_stream_event
*stream_event
;
954 stream_event
= create_event_definitions(td
, stream
, event
);
957 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
962 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
963 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
965 g_free(stream_event
);
967 g_ptr_array_free(stream
->events_by_id
, TRUE
);
969 if (stream
->stream_event_context
)
970 definition_unref(&stream
->stream_event_context
->p
);
971 if (stream
->stream_event_header
)
972 definition_unref(&stream
->stream_event_header
->p
);
973 if (stream
->stream_packet_context
)
974 definition_unref(&stream
->stream_packet_context
->p
);
980 int create_stream_packet_index(struct ctf_trace
*td
,
981 struct ctf_file_stream
*file_stream
)
983 struct ctf_stream_class
*stream
;
985 struct ctf_stream_pos
*pos
;
986 struct stat filestats
;
987 struct packet_index packet_index
;
988 int first_packet
= 1;
991 pos
= &file_stream
->pos
;
993 ret
= fstat(pos
->fd
, &filestats
);
997 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1000 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1001 uint64_t stream_id
= 0;
1004 /* unmap old base */
1005 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1007 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1013 /* map new base. Need mapping length from header. */
1014 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1015 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1016 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1017 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1018 pos
->offset
= 0; /* Position of the packet header */
1020 packet_index
.offset
= pos
->mmap_offset
;
1021 packet_index
.content_size
= 0;
1022 packet_index
.packet_size
= 0;
1023 packet_index
.timestamp_begin
= 0;
1024 packet_index
.timestamp_end
= 0;
1025 packet_index
.events_discarded
= 0;
1027 /* read and check header, set stream id (and check) */
1028 if (file_stream
->parent
.trace_packet_header
) {
1029 /* Read packet header */
1030 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1033 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1034 if (len_index
>= 0) {
1035 struct definition
*field
;
1038 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1039 magic
= get_unsigned_int(field
);
1040 if (magic
!= CTF_MAGIC
) {
1041 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1043 file_stream
->pos
.packet_index
->len
,
1044 (ssize_t
) pos
->mmap_offset
);
1050 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1051 if (len_index
>= 0) {
1052 struct definition_array
*defarray
;
1053 struct definition
*field
;
1055 uint8_t uuidval
[UUID_LEN
];
1057 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1058 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1059 defarray
= container_of(field
, struct definition_array
, p
);
1060 assert(array_len(defarray
) == UUID_LEN
);
1062 for (i
= 0; i
< UUID_LEN
; i
++) {
1063 struct definition
*elem
;
1065 elem
= array_index(defarray
, i
);
1066 uuidval
[i
] = get_unsigned_int(elem
);
1068 ret
= uuid_compare(td
->uuid
, uuidval
);
1070 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1076 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1077 if (len_index
>= 0) {
1078 struct definition
*field
;
1080 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1081 stream_id
= get_unsigned_int(field
);
1085 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1086 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1090 file_stream
->parent
.stream_id
= stream_id
;
1091 if (stream_id
>= td
->streams
->len
) {
1092 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1095 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1097 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1100 file_stream
->parent
.stream_class
= stream
;
1101 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1107 if (file_stream
->parent
.stream_packet_context
) {
1108 /* Read packet context */
1109 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1112 /* read content size from header */
1113 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1114 if (len_index
>= 0) {
1115 struct definition
*field
;
1117 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1118 packet_index
.content_size
= get_unsigned_int(field
);
1120 /* Use file size for packet size */
1121 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1124 /* read packet size from header */
1125 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1126 if (len_index
>= 0) {
1127 struct definition
*field
;
1129 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1130 packet_index
.packet_size
= get_unsigned_int(field
);
1132 /* Use content size if non-zero, else file size */
1133 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1136 /* read timestamp begin from header */
1137 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1138 if (len_index
>= 0) {
1139 struct definition
*field
;
1141 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1142 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1145 /* read timestamp end from header */
1146 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1147 if (len_index
>= 0) {
1148 struct definition
*field
;
1150 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1151 packet_index
.timestamp_end
= get_unsigned_int(field
);
1154 /* read events discarded from header */
1155 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1156 if (len_index
>= 0) {
1157 struct definition
*field
;
1159 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1160 packet_index
.events_discarded
= get_unsigned_int(field
);
1163 /* Use file size for packet size */
1164 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1165 /* Use content size if non-zero, else file size */
1166 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1169 /* Validate content size and packet size values */
1170 if (packet_index
.content_size
> packet_index
.packet_size
) {
1171 fprintf(stderr
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1172 packet_index
.content_size
, packet_index
.packet_size
);
1176 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1177 fprintf(stderr
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1178 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1182 /* Save position after header and context */
1183 packet_index
.data_offset
= pos
->offset
;
1185 /* add index to packet array */
1186 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1188 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1191 /* Move pos back to beginning of file */
1192 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1198 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1202 if (td
->packet_header_decl
) {
1203 struct definition
*definition
=
1204 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1205 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1210 stream
->trace_packet_header
=
1211 container_of(definition
, struct definition_struct
, p
);
1212 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1222 * Note: many file streams can inherit from the same stream class
1223 * description (metadata).
1226 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1227 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1231 struct ctf_file_stream
*file_stream
;
1233 ret
= openat(td
->dirfd
, path
, flags
);
1235 perror("File stream openat()");
1238 file_stream
= g_new0(struct ctf_file_stream
, 1);
1240 if (move_pos_slow
) {
1241 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1243 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1248 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1249 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1252 ret
= create_stream_packet_index(td
, file_stream
);
1255 /* Add stream file to stream class */
1256 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1257 &file_stream
->parent
);
1261 if (file_stream
->parent
.trace_packet_header
)
1262 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1264 ctf_fini_pos(&file_stream
->pos
);
1265 close(file_stream
->pos
.fd
);
1266 g_free(file_stream
);
1272 int ctf_open_trace_read(struct ctf_trace
*td
,
1273 const char *path
, int flags
,
1274 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1275 int whence
), FILE *metadata_fp
)
1278 struct dirent
*dirent
;
1279 struct dirent
*diriter
;
1284 /* Open trace directory */
1285 td
->dir
= opendir(path
);
1287 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1292 td
->dirfd
= open(path
, 0);
1293 if (td
->dirfd
< 0) {
1294 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1295 perror("Trace directory open");
1299 strncpy(td
->path
, path
, sizeof(td
->path
));
1300 td
->path
[sizeof(td
->path
) - 1] = '\0';
1303 * Keep the metadata file separate.
1306 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1308 goto error_metadata
;
1312 * Open each stream: for each file, try to open, check magic
1313 * number, and get the stream ID to add to the right location in
1317 dirent_len
= offsetof(struct dirent
, d_name
) +
1318 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1320 dirent
= malloc(dirent_len
);
1323 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1325 fprintf(stderr
, "[error] Readdir error.\n");
1330 /* Ignore hidden files, ., .. and metadata. */
1331 if (!strncmp(diriter
->d_name
, ".", 1)
1332 || !strcmp(diriter
->d_name
, "..")
1333 || !strcmp(diriter
->d_name
, "metadata"))
1335 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1337 fprintf(stderr
, "[error] Open file stream error.\n");
1356 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1357 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1358 int whence
), FILE *metadata_fp
)
1360 struct ctf_trace
*td
;
1363 td
= g_new0(struct ctf_trace
, 1);
1365 switch (flags
& O_ACCMODE
) {
1368 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1371 ret
= ctf_open_trace_read(td
, path
, flags
, move_pos_slow
, metadata_fp
);
1376 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1379 fprintf(stderr
, "[error] Incorrect open flags.\n");
1390 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1391 struct mmap_stream
*mmap_info
)
1393 pos
->mmap_offset
= 0;
1394 pos
->packet_size
= 0;
1395 pos
->content_size
= 0;
1396 pos
->content_size_loc
= NULL
;
1397 pos
->fd
= mmap_info
->fd
;
1402 pos
->packet_index
= NULL
;
1403 pos
->prot
= PROT_READ
;
1404 pos
->flags
= MAP_PRIVATE
;
1405 pos
->parent
.rw_table
= read_dispatch_table
;
1406 pos
->parent
.event_cb
= ctf_read_event
;
1410 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1411 struct ctf_file_stream
*file_stream
)
1413 struct ctf_stream_class
*stream
;
1414 uint64_t stream_id
= 0;
1417 file_stream
->parent
.stream_id
= stream_id
;
1418 if (stream_id
>= td
->streams
->len
) {
1419 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1420 "in metadata.\n", stream_id
);
1424 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1426 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1427 "in metadata.\n", stream_id
);
1431 file_stream
->parent
.stream_class
= stream
;
1432 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1438 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1439 struct mmap_stream
*mmap_info
,
1440 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1444 struct ctf_file_stream
*file_stream
;
1446 file_stream
= g_new0(struct ctf_file_stream
, 1);
1447 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1449 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1451 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1456 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1460 /* Add stream file to stream class */
1461 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1462 &file_stream
->parent
);
1466 if (file_stream
->parent
.trace_packet_header
)
1467 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1469 g_free(file_stream
);
1473 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1474 struct mmap_stream_list
*mmap_list
,
1475 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1480 struct mmap_stream
*mmap_info
;
1482 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1488 * for each stream, try to open, check magic number, and get the
1489 * stream ID to add to the right location in the stream array.
1491 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1492 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1494 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1506 struct trace_descriptor
*ctf_open_mmap_trace(
1507 struct mmap_stream_list
*mmap_list
,
1508 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1511 struct ctf_trace
*td
;
1515 fprintf(stderr
, "[error] No metadata file pointer associated, "
1516 "required for mmap parsing\n");
1519 if (!move_pos_slow
) {
1520 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1523 td
= g_new0(struct ctf_trace
, 1);
1524 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1537 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1539 ctf_fini_pos(&file_stream
->pos
);
1540 close(file_stream
->pos
.fd
);
1544 void ctf_close_trace(struct trace_descriptor
*tdp
)
1546 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1550 for (i
= 0; i
< td
->streams
->len
; i
++) {
1551 struct ctf_stream_class
*stream
;
1554 stream
= g_ptr_array_index(td
->streams
, i
);
1557 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1558 struct ctf_file_stream
*file_stream
;
1559 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1560 ctf_close_file_stream(file_stream
);
1564 g_ptr_array_free(td
->streams
, TRUE
);
1570 void __attribute__((constructor
)) ctf_init(void)
1574 ctf_format
.name
= g_quark_from_static_string("ctf");
1575 ret
= bt_register_format(&ctf_format
);
1579 /* TODO: finalize */