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 * Update stream current timestamp, keep at clock frequency.
114 void ctf_update_timestamp(struct ctf_stream
*stream
,
115 struct definition_integer
*integer_definition
)
117 struct declaration_integer
*integer_declaration
=
118 integer_definition
->declaration
;
119 uint64_t oldval
, newval
, updateval
;
121 if (unlikely(integer_declaration
->len
== 64)) {
122 stream
->timestamp
= integer_definition
->value
._unsigned
;
126 oldval
= stream
->timestamp
;
127 oldval
&= (1ULL << integer_declaration
->len
) - 1;
128 newval
= integer_definition
->value
._unsigned
;
129 /* Test for overflow by comparing low bits */
131 newval
+= 1ULL << integer_declaration
->len
;
132 /* updateval contains old high bits, and new low bits (sum) */
133 updateval
= stream
->timestamp
;
134 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
136 stream
->prev_timestamp
= stream
->timestamp
;
137 stream
->timestamp
= updateval
;
141 * Print timestamp, rescaling clock frequency to nanoseconds and
142 * applying offsets as needed (unix time).
144 void ctf_print_timestamp(FILE *fp
,
145 struct ctf_stream
*stream
,
148 uint64_t ts_sec
= 0, ts_nsec
;
149 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
150 struct trace_collection
*tc
= trace
->collection
;
151 uint64_t tc_offset
= tc
->single_clock_offset_avg
;
153 if (stream
->current_clock
->freq
== 1000000000ULL) {
156 ts_nsec
= (uint64_t) ((double) timestamp
* 1000000000.0
157 / (double) stream
->current_clock
->freq
);
161 if (!opt_clock_raw
) {
162 ts_nsec
+= tc_offset
;
164 ts_sec
+= opt_clock_offset
;
166 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
167 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
169 if (!opt_clock_seconds
) {
171 time_t time_s
= (time_t) ts_sec
;
173 if (!opt_clock_gmt
) {
176 res
= localtime_r(&time_s
, &tm
);
178 fprintf(stderr
, "[warning] Unable to get localtime.\n");
184 res
= gmtime_r(&time_s
, &tm
);
186 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
190 if (opt_clock_date
) {
194 /* Print date and time */
195 res
= strftime(timestr
, sizeof(timestr
),
198 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
201 fprintf(fp
, "%s", timestr
);
203 /* Print time in HH:MM:SS.ns */
204 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
205 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
209 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
217 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
219 struct ctf_stream_pos
*pos
=
220 container_of(ppos
, struct ctf_stream_pos
, parent
);
221 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
222 struct ctf_stream_event
*event
;
226 /* We need to check for EOF here for empty files. */
227 if (unlikely(pos
->offset
== EOF
))
230 ctf_pos_get_event(pos
);
233 * This is the EOF check after we've advanced the position in
236 if (unlikely(pos
->offset
== EOF
))
238 assert(pos
->offset
< pos
->content_size
);
240 /* Read event header */
241 if (likely(stream
->stream_event_header
)) {
242 struct definition_integer
*integer_definition
;
243 struct definition
*variant
;
245 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
248 /* lookup event id */
249 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
250 if (integer_definition
) {
251 id
= integer_definition
->value
._unsigned
;
253 struct definition_enum
*enum_definition
;
255 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
256 if (enum_definition
) {
257 id
= enum_definition
->integer
->value
._unsigned
;
261 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
263 integer_definition
= lookup_integer(variant
, "id", FALSE
);
264 if (integer_definition
) {
265 id
= integer_definition
->value
._unsigned
;
268 stream
->event_id
= id
;
270 /* lookup timestamp */
271 stream
->has_timestamp
= 0;
272 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
273 if (integer_definition
) {
274 ctf_update_timestamp(stream
, integer_definition
);
275 stream
->has_timestamp
= 1;
278 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
279 if (integer_definition
) {
280 ctf_update_timestamp(stream
, integer_definition
);
281 stream
->has_timestamp
= 1;
287 /* Read stream-declared event context */
288 if (stream
->stream_event_context
) {
289 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
294 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
295 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
298 event
= g_ptr_array_index(stream
->events_by_id
, id
);
299 if (unlikely(!event
)) {
300 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
304 /* Read event-declared event context */
305 if (event
->event_context
) {
306 ret
= generic_rw(ppos
, &event
->event_context
->p
);
311 /* Read event payload */
312 if (likely(event
->event_fields
)) {
313 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
321 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
326 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
328 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
329 struct ctf_stream_event
*event
;
333 id
= stream
->event_id
;
335 /* print event header */
336 if (likely(stream
->stream_event_header
)) {
337 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
342 /* print stream-declared event context */
343 if (stream
->stream_event_context
) {
344 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
349 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
350 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
353 event
= g_ptr_array_index(stream
->events_by_id
, id
);
354 if (unlikely(!event
)) {
355 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
359 /* print event-declared event context */
360 if (event
->event_context
) {
361 ret
= generic_rw(pos
, &event
->event_context
->p
);
366 /* Read and print event payload */
367 if (likely(event
->event_fields
)) {
368 ret
= generic_rw(pos
, &event
->event_fields
->p
);
376 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
380 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
383 pos
->mmap_offset
= 0;
384 pos
->packet_size
= 0;
385 pos
->content_size
= 0;
386 pos
->content_size_loc
= NULL
;
392 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
393 sizeof(struct packet_index
));
395 pos
->packet_index
= NULL
;
396 switch (open_flags
& O_ACCMODE
) {
398 pos
->prot
= PROT_READ
;
399 pos
->flags
= MAP_PRIVATE
;
400 pos
->parent
.rw_table
= read_dispatch_table
;
401 pos
->parent
.event_cb
= ctf_read_event
;
404 pos
->prot
= PROT_WRITE
; /* Write has priority */
405 pos
->flags
= MAP_SHARED
;
406 pos
->parent
.rw_table
= write_dispatch_table
;
407 pos
->parent
.event_cb
= ctf_write_event
;
409 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
416 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
420 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
421 *pos
->content_size_loc
= pos
->offset
;
424 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
426 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
431 (void) g_array_free(pos
->packet_index
, TRUE
);
434 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
436 struct ctf_file_stream
*file_stream
=
437 container_of(pos
, struct ctf_file_stream
, pos
);
440 struct packet_index
*index
;
442 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
443 *pos
->content_size_loc
= pos
->offset
;
447 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
449 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
457 * The caller should never ask for ctf_move_pos across packets,
458 * except to get exactly at the beginning of the next packet.
460 if (pos
->prot
== PROT_WRITE
) {
463 /* The writer will add padding */
464 assert(pos
->offset
+ offset
== pos
->packet_size
);
465 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
468 assert(offset
== 0); /* only seek supported for now */
474 pos
->content_size
= -1U; /* Unknown at this point */
475 pos
->packet_size
= WRITE_PACKET_LEN
;
476 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
477 pos
->packet_size
/ CHAR_BIT
);
485 uint32_t events_discarded_diff
;
487 if (pos
->offset
== EOF
) {
490 /* For printing discarded event count */
491 index
= &g_array_index(pos
->packet_index
,
492 struct packet_index
, pos
->cur_index
);
493 events_discarded_diff
= index
->events_discarded
;
494 file_stream
->parent
.prev_timestamp_end
=
495 index
->timestamp_end
;
496 if (pos
->cur_index
> 0) {
497 index
= &g_array_index(pos
->packet_index
,
500 events_discarded_diff
-= index
->events_discarded
;
502 file_stream
->parent
.events_discarded
= events_discarded_diff
;
503 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
504 /* The reader will expect us to skip padding */
505 assert(pos
->offset
+ offset
== pos
->content_size
);
510 assert(offset
== 0); /* only seek supported for now */
512 file_stream
->parent
.prev_timestamp
= 0;
513 file_stream
->parent
.prev_timestamp_end
= 0;
518 if (pos
->cur_index
>= pos
->packet_index
->len
) {
520 * When a stream reaches the end of the
521 * file, we need to show the number of
522 * events discarded ourselves, because
523 * there is no next event scheduled to
524 * be printed in the output.
526 if (file_stream
->parent
.events_discarded
) {
528 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
529 file_stream
->parent
.events_discarded
);
530 ctf_print_timestamp(stderr
, &file_stream
->parent
,
531 file_stream
->parent
.prev_timestamp
);
532 fprintf(stderr
, "] and [");
533 ctf_print_timestamp(stderr
, &file_stream
->parent
,
534 file_stream
->parent
.prev_timestamp_end
);
535 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
537 file_stream
->parent
.events_discarded
= 0;
542 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
544 pos
->mmap_offset
= index
->offset
;
546 /* Lookup context/packet size in index */
547 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
548 pos
->content_size
= index
->content_size
;
549 pos
->packet_size
= index
->packet_size
;
550 if (index
->data_offset
< index
->content_size
) {
551 pos
->offset
= 0; /* will read headers */
552 } else if (index
->data_offset
== index
->content_size
) {
554 pos
->offset
= index
->data_offset
;
557 goto read_next_packet
;
563 /* map new base. Need mapping length from header. */
564 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
565 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
566 if (pos
->base
== MAP_FAILED
) {
567 fprintf(stderr
, "[error] mmap error %s.\n",
572 /* update trace_packet_header and stream_packet_context */
573 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
574 /* Read packet header */
575 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
578 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
579 /* Read packet context */
580 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
586 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
592 len
= fread(&magic
, sizeof(magic
), 1, fp
);
596 if (magic
== TSDL_MAGIC
) {
598 td
->byte_order
= BYTE_ORDER
;
599 CTF_TRACE_SET_FIELD(td
, byte_order
);
600 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
602 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
603 LITTLE_ENDIAN
: BIG_ENDIAN
;
604 CTF_TRACE_SET_FIELD(td
, byte_order
);
612 * Returns 0 on success, -1 on error.
615 int check_version(unsigned int major
, unsigned int minor
)
630 /* eventually return an error instead of warning */
632 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
638 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
641 struct metadata_packet_header header
;
642 size_t readlen
, writelen
, toread
;
643 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
646 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
650 if (td
->byte_order
!= BYTE_ORDER
) {
651 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
652 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
653 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
654 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
657 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
658 if (header
.compression_scheme
) {
659 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
660 header
.compression_scheme
);
663 if (header
.encryption_scheme
) {
664 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
665 header
.encryption_scheme
);
668 if (header
.checksum_scheme
) {
669 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
670 header
.checksum_scheme
);
673 if (check_version(header
.major
, header
.minor
) < 0)
675 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
676 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
677 CTF_TRACE_SET_FIELD(td
, uuid
);
679 if (uuid_compare(header
.uuid
, td
->uuid
))
683 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
686 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
691 if (babeltrace_debug
) {
693 fprintf(stderr
, "[debug] metadata packet read: %s\n",
697 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
698 if (writelen
< readlen
) {
708 ret
= 0; /* continue reading next packet */
715 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
716 ret
= fseek(in
, toread
, SEEK_CUR
);
718 fprintf(stderr
, "[warning] Missing padding at end of file\n");
725 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
734 * Using strlen on *buf instead of size of open_memstream
735 * because its size includes garbage at the end (after final
736 * \0). This is the allocated size, not the actual string size.
738 out
= open_memstream(buf
, &size
);
740 perror("Metadata open_memstream");
744 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
753 fclose(out
); /* flush the buffer */
755 /* open for reading */
756 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
758 perror("Metadata fmemopen");
765 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
766 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
767 int whence
), FILE *metadata_fp
)
769 struct ctf_scanner
*scanner
;
770 struct ctf_file_stream
*metadata_stream
;
775 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
778 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
780 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
788 td
->metadata
= &metadata_stream
->parent
;
789 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
790 if (metadata_stream
->pos
.fd
< 0) {
791 fprintf(stderr
, "Unable to open metadata.\n");
792 return metadata_stream
->pos
.fd
;
795 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
797 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
798 perror("Metadata stream open");
803 if (babeltrace_debug
)
806 if (packet_metadata(td
, fp
)) {
807 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
809 goto end_packet_read
;
811 unsigned int major
, minor
;
814 td
->byte_order
= BYTE_ORDER
;
816 /* Check text-only metadata header and version */
817 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
819 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
820 if (check_version(major
, minor
) < 0) {
822 goto end_packet_read
;
827 scanner
= ctf_scanner_alloc(fp
);
829 fprintf(stderr
, "[error] Error allocating scanner\n");
831 goto end_scanner_alloc
;
833 ret
= ctf_scanner_append_ast(scanner
);
835 fprintf(stderr
, "[error] Error creating AST\n");
839 if (babeltrace_debug
) {
840 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
842 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
847 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
849 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
852 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
855 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
859 ctf_scanner_free(scanner
);
865 close(metadata_stream
->pos
.fd
);
867 g_free(metadata_stream
);
872 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
873 struct ctf_stream
*stream
,
874 struct ctf_event
*event
)
876 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
878 if (event
->context_decl
) {
879 struct definition
*definition
=
880 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
881 stream
->parent_def_scope
, 0, 0, "event.context");
885 stream_event
->event_context
= container_of(definition
,
886 struct definition_struct
, p
);
887 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
889 if (event
->fields_decl
) {
890 struct definition
*definition
=
891 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
892 stream
->parent_def_scope
, 0, 0, "event.fields");
896 stream_event
->event_fields
= container_of(definition
,
897 struct definition_struct
, p
);
898 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
903 if (stream_event
->event_fields
)
904 definition_unref(&stream_event
->event_fields
->p
);
905 if (stream_event
->event_context
)
906 definition_unref(&stream_event
->event_context
->p
);
911 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
913 struct ctf_stream_class
*stream_class
;
917 if (stream
->stream_definitions_created
)
920 stream_class
= stream
->stream_class
;
922 if (stream_class
->packet_context_decl
) {
923 struct definition
*definition
=
924 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
925 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
930 stream
->stream_packet_context
= container_of(definition
,
931 struct definition_struct
, p
);
932 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
934 if (stream_class
->event_header_decl
) {
935 struct definition
*definition
=
936 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
937 stream
->parent_def_scope
, 0, 0, "stream.event.header");
942 stream
->stream_event_header
=
943 container_of(definition
, struct definition_struct
, p
);
944 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
946 if (stream_class
->event_context_decl
) {
947 struct definition
*definition
=
948 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
949 stream
->parent_def_scope
, 0, 0, "stream.event.context");
954 stream
->stream_event_context
=
955 container_of(definition
, struct definition_struct
, p
);
956 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
958 stream
->events_by_id
= g_ptr_array_new();
959 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
960 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
961 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
962 struct ctf_stream_event
*stream_event
;
966 stream_event
= create_event_definitions(td
, stream
, event
);
969 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
974 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
975 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
977 g_free(stream_event
);
979 g_ptr_array_free(stream
->events_by_id
, TRUE
);
981 if (stream
->stream_event_context
)
982 definition_unref(&stream
->stream_event_context
->p
);
983 if (stream
->stream_event_header
)
984 definition_unref(&stream
->stream_event_header
->p
);
985 if (stream
->stream_packet_context
)
986 definition_unref(&stream
->stream_packet_context
->p
);
992 int create_stream_packet_index(struct ctf_trace
*td
,
993 struct ctf_file_stream
*file_stream
)
995 struct ctf_stream_class
*stream
;
997 struct ctf_stream_pos
*pos
;
998 struct stat filestats
;
999 struct packet_index packet_index
;
1000 int first_packet
= 1;
1003 pos
= &file_stream
->pos
;
1005 ret
= fstat(pos
->fd
, &filestats
);
1009 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1012 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1013 uint64_t stream_id
= 0;
1016 /* unmap old base */
1017 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1019 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1025 /* map new base. Need mapping length from header. */
1026 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1027 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1028 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1029 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1030 pos
->offset
= 0; /* Position of the packet header */
1032 packet_index
.offset
= pos
->mmap_offset
;
1033 packet_index
.content_size
= 0;
1034 packet_index
.packet_size
= 0;
1035 packet_index
.timestamp_begin
= 0;
1036 packet_index
.timestamp_end
= 0;
1037 packet_index
.events_discarded
= 0;
1039 /* read and check header, set stream id (and check) */
1040 if (file_stream
->parent
.trace_packet_header
) {
1041 /* Read packet header */
1042 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1045 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1046 if (len_index
>= 0) {
1047 struct definition
*field
;
1050 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1051 magic
= get_unsigned_int(field
);
1052 if (magic
!= CTF_MAGIC
) {
1053 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1055 file_stream
->pos
.packet_index
->len
,
1056 (ssize_t
) pos
->mmap_offset
);
1062 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1063 if (len_index
>= 0) {
1064 struct definition_array
*defarray
;
1065 struct definition
*field
;
1067 uint8_t uuidval
[UUID_LEN
];
1069 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1070 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1071 defarray
= container_of(field
, struct definition_array
, p
);
1072 assert(array_len(defarray
) == UUID_LEN
);
1074 for (i
= 0; i
< UUID_LEN
; i
++) {
1075 struct definition
*elem
;
1077 elem
= array_index(defarray
, i
);
1078 uuidval
[i
] = get_unsigned_int(elem
);
1080 ret
= uuid_compare(td
->uuid
, uuidval
);
1082 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1088 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1089 if (len_index
>= 0) {
1090 struct definition
*field
;
1092 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1093 stream_id
= get_unsigned_int(field
);
1097 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1098 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1102 file_stream
->parent
.stream_id
= stream_id
;
1103 if (stream_id
>= td
->streams
->len
) {
1104 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1107 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1109 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1112 file_stream
->parent
.stream_class
= stream
;
1113 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1119 if (file_stream
->parent
.stream_packet_context
) {
1120 /* Read packet context */
1121 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1124 /* read content size from header */
1125 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_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
.content_size
= get_unsigned_int(field
);
1132 /* Use file size for packet size */
1133 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1136 /* read packet size from header */
1137 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
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
.packet_size
= get_unsigned_int(field
);
1144 /* Use content size if non-zero, else file size */
1145 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1148 /* read timestamp begin from header */
1149 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1150 if (len_index
>= 0) {
1151 struct definition
*field
;
1153 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1154 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1157 /* read timestamp end from header */
1158 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1159 if (len_index
>= 0) {
1160 struct definition
*field
;
1162 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1163 packet_index
.timestamp_end
= get_unsigned_int(field
);
1166 /* read events discarded from header */
1167 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1168 if (len_index
>= 0) {
1169 struct definition
*field
;
1171 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1172 packet_index
.events_discarded
= get_unsigned_int(field
);
1175 /* Use file size for packet size */
1176 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1177 /* Use content size if non-zero, else file size */
1178 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1181 /* Validate content size and packet size values */
1182 if (packet_index
.content_size
> packet_index
.packet_size
) {
1183 fprintf(stderr
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1184 packet_index
.content_size
, packet_index
.packet_size
);
1188 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1189 fprintf(stderr
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1190 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1194 /* Save position after header and context */
1195 packet_index
.data_offset
= pos
->offset
;
1197 /* add index to packet array */
1198 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1200 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1203 /* Move pos back to beginning of file */
1204 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1210 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1214 if (td
->packet_header_decl
) {
1215 struct definition
*definition
=
1216 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1217 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1222 stream
->trace_packet_header
=
1223 container_of(definition
, struct definition_struct
, p
);
1224 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1234 * Note: many file streams can inherit from the same stream class
1235 * description (metadata).
1238 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1239 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1243 struct ctf_file_stream
*file_stream
;
1245 ret
= openat(td
->dirfd
, path
, flags
);
1247 perror("File stream openat()");
1250 file_stream
= g_new0(struct ctf_file_stream
, 1);
1252 if (move_pos_slow
) {
1253 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1255 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1260 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1261 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1265 * For now, only a single slock is supported.
1267 file_stream
->parent
.current_clock
= td
->single_clock
;
1268 ret
= create_stream_packet_index(td
, file_stream
);
1271 /* Add stream file to stream class */
1272 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1273 &file_stream
->parent
);
1277 if (file_stream
->parent
.trace_packet_header
)
1278 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1280 ctf_fini_pos(&file_stream
->pos
);
1281 close(file_stream
->pos
.fd
);
1282 g_free(file_stream
);
1288 int ctf_open_trace_read(struct ctf_trace
*td
,
1289 const char *path
, int flags
,
1290 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1291 int whence
), FILE *metadata_fp
)
1294 struct dirent
*dirent
;
1295 struct dirent
*diriter
;
1300 /* Open trace directory */
1301 td
->dir
= opendir(path
);
1303 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1308 td
->dirfd
= open(path
, 0);
1309 if (td
->dirfd
< 0) {
1310 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1311 perror("Trace directory open");
1315 strncpy(td
->path
, path
, sizeof(td
->path
));
1316 td
->path
[sizeof(td
->path
) - 1] = '\0';
1319 * Keep the metadata file separate.
1322 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1324 goto error_metadata
;
1328 * Open each stream: for each file, try to open, check magic
1329 * number, and get the stream ID to add to the right location in
1333 dirent_len
= offsetof(struct dirent
, d_name
) +
1334 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1336 dirent
= malloc(dirent_len
);
1339 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1341 fprintf(stderr
, "[error] Readdir error.\n");
1346 /* Ignore hidden files, ., .. and metadata. */
1347 if (!strncmp(diriter
->d_name
, ".", 1)
1348 || !strcmp(diriter
->d_name
, "..")
1349 || !strcmp(diriter
->d_name
, "metadata"))
1351 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1353 fprintf(stderr
, "[error] Open file stream error.\n");
1372 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1373 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1374 int whence
), FILE *metadata_fp
)
1376 struct ctf_trace
*td
;
1379 td
= g_new0(struct ctf_trace
, 1);
1381 switch (flags
& O_ACCMODE
) {
1384 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1387 ret
= ctf_open_trace_read(td
, path
, flags
, move_pos_slow
, metadata_fp
);
1392 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1395 fprintf(stderr
, "[error] Incorrect open flags.\n");
1406 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1407 struct mmap_stream
*mmap_info
)
1409 pos
->mmap_offset
= 0;
1410 pos
->packet_size
= 0;
1411 pos
->content_size
= 0;
1412 pos
->content_size_loc
= NULL
;
1413 pos
->fd
= mmap_info
->fd
;
1418 pos
->packet_index
= NULL
;
1419 pos
->prot
= PROT_READ
;
1420 pos
->flags
= MAP_PRIVATE
;
1421 pos
->parent
.rw_table
= read_dispatch_table
;
1422 pos
->parent
.event_cb
= ctf_read_event
;
1426 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1427 struct ctf_file_stream
*file_stream
)
1429 struct ctf_stream_class
*stream
;
1430 uint64_t stream_id
= 0;
1433 file_stream
->parent
.stream_id
= stream_id
;
1434 if (stream_id
>= td
->streams
->len
) {
1435 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1436 "in metadata.\n", stream_id
);
1440 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1442 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1443 "in metadata.\n", stream_id
);
1447 file_stream
->parent
.stream_class
= stream
;
1448 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1454 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1455 struct mmap_stream
*mmap_info
,
1456 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1460 struct ctf_file_stream
*file_stream
;
1462 file_stream
= g_new0(struct ctf_file_stream
, 1);
1463 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1465 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1467 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1472 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1476 /* Add stream file to stream class */
1477 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1478 &file_stream
->parent
);
1482 if (file_stream
->parent
.trace_packet_header
)
1483 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1485 g_free(file_stream
);
1489 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1490 struct mmap_stream_list
*mmap_list
,
1491 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1496 struct mmap_stream
*mmap_info
;
1498 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1504 * for each stream, try to open, check magic number, and get the
1505 * stream ID to add to the right location in the stream array.
1507 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1508 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1510 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1522 struct trace_descriptor
*ctf_open_mmap_trace(
1523 struct mmap_stream_list
*mmap_list
,
1524 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1527 struct ctf_trace
*td
;
1531 fprintf(stderr
, "[error] No metadata file pointer associated, "
1532 "required for mmap parsing\n");
1535 if (!move_pos_slow
) {
1536 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1539 td
= g_new0(struct ctf_trace
, 1);
1540 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1553 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1555 ctf_fini_pos(&file_stream
->pos
);
1556 close(file_stream
->pos
.fd
);
1560 void ctf_close_trace(struct trace_descriptor
*tdp
)
1562 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1566 for (i
= 0; i
< td
->streams
->len
; i
++) {
1567 struct ctf_stream_class
*stream
;
1570 stream
= g_ptr_array_index(td
->streams
, i
);
1573 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1574 struct ctf_file_stream
*file_stream
;
1575 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1576 ctf_close_file_stream(file_stream
);
1580 g_ptr_array_free(td
->streams
, TRUE
);
1586 void __attribute__((constructor
)) ctf_init(void)
1590 ctf_format
.name
= g_quark_from_static_string("ctf");
1591 ret
= bt_register_format(&ctf_format
);
1595 /* TODO: finalize */