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>
28 #include <uuid/uuid.h>
32 #include <sys/types.h>
40 #include "metadata/ctf-scanner.h"
41 #include "metadata/ctf-parser.h"
42 #include "metadata/ctf-ast.h"
43 #include "events-private.h"
46 * We currently simply map a page to read the packet header and packet
47 * context to get the packet length and content length. (in bits)
49 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
50 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
51 #define UUID_LEN 16 /* uuid by value len */
54 #define min(a, b) (((a) < (b)) ? (a) : (b))
57 #define NSEC_PER_SEC 1000000000ULL
64 uint64_t opt_clock_offset
;
69 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
70 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
74 struct trace_descriptor
*ctf_open_mmap_trace(
75 struct mmap_stream_list
*mmap_list
,
76 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
81 void ctf_close_trace(struct trace_descriptor
*descriptor
);
84 rw_dispatch read_dispatch_table
[] = {
85 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
86 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
87 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
88 [ CTF_TYPE_STRING
] = ctf_string_read
,
89 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
90 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
91 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
92 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
96 rw_dispatch write_dispatch_table
[] = {
97 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
98 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
99 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
100 [ CTF_TYPE_STRING
] = ctf_string_write
,
101 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
102 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
103 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
104 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
108 struct format ctf_format
= {
109 .open_trace
= ctf_open_trace
,
110 .open_mmap_trace
= ctf_open_mmap_trace
,
111 .close_trace
= ctf_close_trace
,
115 * Update stream current timestamp, keep at clock frequency.
118 void ctf_update_timestamp(struct ctf_stream
*stream
,
119 struct definition_integer
*integer_definition
)
121 struct declaration_integer
*integer_declaration
=
122 integer_definition
->declaration
;
123 uint64_t oldval
, newval
, updateval
;
125 if (unlikely(integer_declaration
->len
== 64)) {
126 stream
->timestamp
= integer_definition
->value
._unsigned
;
130 oldval
= stream
->timestamp
;
131 oldval
&= (1ULL << integer_declaration
->len
) - 1;
132 newval
= integer_definition
->value
._unsigned
;
133 /* Test for overflow by comparing low bits */
135 newval
+= 1ULL << integer_declaration
->len
;
136 /* updateval contains old high bits, and new low bits (sum) */
137 updateval
= stream
->timestamp
;
138 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
140 stream
->prev_timestamp
= stream
->timestamp
;
141 stream
->timestamp
= updateval
;
145 * Print timestamp, rescaling clock frequency to nanoseconds and
146 * applying offsets as needed (unix time).
148 void ctf_print_timestamp(FILE *fp
,
149 struct ctf_stream
*stream
,
152 uint64_t ts_sec
= 0, ts_nsec
;
155 ts_nsec
= ctf_get_timestamp_raw(stream
, timestamp
);
157 ts_nsec
= ctf_get_timestamp(stream
, timestamp
);
160 /* Add command-line offset */
161 ts_sec
+= opt_clock_offset
;
163 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
164 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
166 if (!opt_clock_seconds
) {
168 time_t time_s
= (time_t) ts_sec
;
170 if (!opt_clock_gmt
) {
173 res
= localtime_r(&time_s
, &tm
);
175 fprintf(stderr
, "[warning] Unable to get localtime.\n");
181 res
= gmtime_r(&time_s
, &tm
);
183 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
187 if (opt_clock_date
) {
191 /* Print date and time */
192 res
= strftime(timestr
, sizeof(timestr
),
195 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
198 fprintf(fp
, "%s", timestr
);
200 /* Print time in HH:MM:SS.ns */
201 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
202 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
206 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
214 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
216 struct ctf_stream_pos
*pos
=
217 container_of(ppos
, struct ctf_stream_pos
, parent
);
218 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
219 struct ctf_stream_event
*event
;
223 /* We need to check for EOF here for empty files. */
224 if (unlikely(pos
->offset
== EOF
))
227 ctf_pos_get_event(pos
);
229 /* save the current position as a restore point */
230 pos
->last_offset
= pos
->offset
;
231 /* we just read the event, it is consumed when used by the caller */
232 stream
->consumed
= 0;
235 * This is the EOF check after we've advanced the position in
238 if (unlikely(pos
->offset
== EOF
))
240 assert(pos
->offset
< pos
->content_size
);
242 /* Read event header */
243 if (likely(stream
->stream_event_header
)) {
244 struct definition_integer
*integer_definition
;
245 struct definition
*variant
;
247 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
250 /* lookup event id */
251 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
252 if (integer_definition
) {
253 id
= integer_definition
->value
._unsigned
;
255 struct definition_enum
*enum_definition
;
257 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
258 if (enum_definition
) {
259 id
= enum_definition
->integer
->value
._unsigned
;
263 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
265 integer_definition
= lookup_integer(variant
, "id", FALSE
);
266 if (integer_definition
) {
267 id
= integer_definition
->value
._unsigned
;
270 stream
->event_id
= id
;
272 /* lookup timestamp */
273 stream
->has_timestamp
= 0;
274 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
275 if (integer_definition
) {
276 ctf_update_timestamp(stream
, integer_definition
);
277 stream
->has_timestamp
= 1;
280 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
281 if (integer_definition
) {
282 ctf_update_timestamp(stream
, integer_definition
);
283 stream
->has_timestamp
= 1;
289 /* Read stream-declared event context */
290 if (stream
->stream_event_context
) {
291 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
296 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
297 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
300 event
= g_ptr_array_index(stream
->events_by_id
, id
);
301 if (unlikely(!event
)) {
302 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
306 /* Read event-declared event context */
307 if (event
->event_context
) {
308 ret
= generic_rw(ppos
, &event
->event_context
->p
);
313 /* Read event payload */
314 if (likely(event
->event_fields
)) {
315 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
323 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
328 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
330 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
331 struct ctf_stream_event
*event
;
335 id
= stream
->event_id
;
337 /* print event header */
338 if (likely(stream
->stream_event_header
)) {
339 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
344 /* print stream-declared event context */
345 if (stream
->stream_event_context
) {
346 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
351 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
352 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
355 event
= g_ptr_array_index(stream
->events_by_id
, id
);
356 if (unlikely(!event
)) {
357 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
361 /* print event-declared event context */
362 if (event
->event_context
) {
363 ret
= generic_rw(pos
, &event
->event_context
->p
);
368 /* Read and print event payload */
369 if (likely(event
->event_fields
)) {
370 ret
= generic_rw(pos
, &event
->event_fields
->p
);
378 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
382 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
385 pos
->mmap_offset
= 0;
386 pos
->packet_size
= 0;
387 pos
->content_size
= 0;
388 pos
->content_size_loc
= NULL
;
394 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
395 sizeof(struct packet_index
));
397 pos
->packet_index
= NULL
;
398 switch (open_flags
& O_ACCMODE
) {
400 pos
->prot
= PROT_READ
;
401 pos
->flags
= MAP_PRIVATE
;
402 pos
->parent
.rw_table
= read_dispatch_table
;
403 pos
->parent
.event_cb
= ctf_read_event
;
406 pos
->prot
= PROT_WRITE
; /* Write has priority */
407 pos
->flags
= MAP_SHARED
;
408 pos
->parent
.rw_table
= write_dispatch_table
;
409 pos
->parent
.event_cb
= ctf_write_event
;
411 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
418 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
422 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
423 *pos
->content_size_loc
= pos
->offset
;
426 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
428 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
433 (void) g_array_free(pos
->packet_index
, TRUE
);
437 * for SEEK_CUR: go to next packet.
438 * for SEEK_POS: go to packet numer (index).
440 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
442 struct ctf_stream_pos
*pos
=
443 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
444 struct ctf_file_stream
*file_stream
=
445 container_of(pos
, struct ctf_file_stream
, pos
);
448 struct packet_index
*packet_index
;
450 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
451 *pos
->content_size_loc
= pos
->offset
;
455 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
457 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
465 * The caller should never ask for ctf_move_pos across packets,
466 * except to get exactly at the beginning of the next packet.
468 if (pos
->prot
== PROT_WRITE
) {
471 /* The writer will add padding */
472 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
475 assert(index
== 0); /* only seek supported for now */
481 pos
->content_size
= -1U; /* Unknown at this point */
482 pos
->packet_size
= WRITE_PACKET_LEN
;
483 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
484 pos
->packet_size
/ CHAR_BIT
);
492 uint32_t events_discarded_diff
;
494 if (pos
->offset
== EOF
) {
497 /* For printing discarded event count */
498 packet_index
= &g_array_index(pos
->packet_index
,
499 struct packet_index
, pos
->cur_index
);
500 events_discarded_diff
= packet_index
->events_discarded
;
501 file_stream
->parent
.prev_timestamp_end
=
502 packet_index
->timestamp_end
;
503 if (pos
->cur_index
> 0) {
504 packet_index
= &g_array_index(pos
->packet_index
,
507 events_discarded_diff
-= packet_index
->events_discarded
;
509 file_stream
->parent
.events_discarded
= events_discarded_diff
;
510 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
511 /* The reader will expect us to skip padding */
516 pos
->cur_index
= index
;
517 file_stream
->parent
.prev_timestamp
= 0;
518 file_stream
->parent
.prev_timestamp_end
= 0;
523 if (pos
->cur_index
>= pos
->packet_index
->len
) {
525 * When a stream reaches the end of the
526 * file, we need to show the number of
527 * events discarded ourselves, because
528 * there is no next event scheduled to
529 * be printed in the output.
531 if (file_stream
->parent
.events_discarded
) {
533 * We need to check if we are in trace
534 * read or called from packet indexing.
535 * In this last case, the collection is
536 * not there, so we cannot print the
539 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
541 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
542 file_stream
->parent
.events_discarded
);
543 ctf_print_timestamp(stderr
, &file_stream
->parent
,
544 file_stream
->parent
.prev_timestamp
);
545 fprintf(stderr
, "] and [");
546 ctf_print_timestamp(stderr
, &file_stream
->parent
,
547 file_stream
->parent
.prev_timestamp_end
);
548 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
551 file_stream
->parent
.events_discarded
= 0;
556 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
558 pos
->mmap_offset
= packet_index
->offset
;
560 /* Lookup context/packet size in index */
561 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
562 pos
->content_size
= packet_index
->content_size
;
563 pos
->packet_size
= packet_index
->packet_size
;
564 if (packet_index
->data_offset
< packet_index
->content_size
) {
565 pos
->offset
= 0; /* will read headers */
566 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
568 pos
->offset
= packet_index
->data_offset
;
570 goto read_next_packet
;
576 /* map new base. Need mapping length from header. */
577 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
578 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
579 if (pos
->base
== MAP_FAILED
) {
580 fprintf(stderr
, "[error] mmap error %s.\n",
585 /* update trace_packet_header and stream_packet_context */
586 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
587 /* Read packet header */
588 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
591 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
592 /* Read packet context */
593 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
599 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
605 len
= fread(&magic
, sizeof(magic
), 1, fp
);
609 if (magic
== TSDL_MAGIC
) {
611 td
->byte_order
= BYTE_ORDER
;
612 CTF_TRACE_SET_FIELD(td
, byte_order
);
613 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
615 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
616 LITTLE_ENDIAN
: BIG_ENDIAN
;
617 CTF_TRACE_SET_FIELD(td
, byte_order
);
625 * Returns 0 on success, -1 on error.
628 int check_version(unsigned int major
, unsigned int minor
)
643 /* eventually return an error instead of warning */
645 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
651 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
654 struct metadata_packet_header header
;
655 size_t readlen
, writelen
, toread
;
656 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
659 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
663 if (td
->byte_order
!= BYTE_ORDER
) {
664 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
665 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
666 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
667 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
670 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
671 if (header
.compression_scheme
) {
672 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
673 header
.compression_scheme
);
676 if (header
.encryption_scheme
) {
677 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
678 header
.encryption_scheme
);
681 if (header
.checksum_scheme
) {
682 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
683 header
.checksum_scheme
);
686 if (check_version(header
.major
, header
.minor
) < 0)
688 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
689 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
690 CTF_TRACE_SET_FIELD(td
, uuid
);
692 if (uuid_compare(header
.uuid
, td
->uuid
))
696 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
699 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
704 if (babeltrace_debug
) {
706 fprintf(stderr
, "[debug] metadata packet read: %s\n",
710 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
711 if (writelen
< readlen
) {
721 ret
= 0; /* continue reading next packet */
728 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
729 ret
= fseek(in
, toread
, SEEK_CUR
);
731 fprintf(stderr
, "[warning] Missing padding at end of file\n");
738 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
747 * Using strlen on *buf instead of size of open_memstream
748 * because its size includes garbage at the end (after final
749 * \0). This is the allocated size, not the actual string size.
751 out
= open_memstream(buf
, &size
);
753 perror("Metadata open_memstream");
757 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
766 fclose(out
); /* flush the buffer */
768 /* open for reading */
769 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
771 perror("Metadata fmemopen");
778 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
779 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
780 int whence
), FILE *metadata_fp
)
782 struct ctf_scanner
*scanner
;
783 struct ctf_file_stream
*metadata_stream
;
788 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
791 metadata_stream
->pos
.packet_seek
= packet_seek
;
793 fprintf(stderr
, "[error] packet_seek function undefined.\n");
801 td
->metadata
= &metadata_stream
->parent
;
802 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
803 if (metadata_stream
->pos
.fd
< 0) {
804 fprintf(stderr
, "Unable to open metadata.\n");
805 return metadata_stream
->pos
.fd
;
808 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
810 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
811 perror("Metadata stream open");
816 if (babeltrace_debug
)
819 if (packet_metadata(td
, fp
)) {
820 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
822 goto end_packet_read
;
824 unsigned int major
, minor
;
827 td
->byte_order
= BYTE_ORDER
;
829 /* Check text-only metadata header and version */
830 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
832 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
833 if (check_version(major
, minor
) < 0) {
835 goto end_packet_read
;
840 scanner
= ctf_scanner_alloc(fp
);
842 fprintf(stderr
, "[error] Error allocating scanner\n");
844 goto end_scanner_alloc
;
846 ret
= ctf_scanner_append_ast(scanner
);
848 fprintf(stderr
, "[error] Error creating AST\n");
852 if (babeltrace_debug
) {
853 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
855 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
860 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
862 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
865 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
868 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
872 ctf_scanner_free(scanner
);
878 close(metadata_stream
->pos
.fd
);
880 g_free(metadata_stream
);
885 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
886 struct ctf_stream
*stream
,
887 struct ctf_event
*event
)
889 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
891 if (event
->context_decl
) {
892 struct definition
*definition
=
893 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
894 stream
->parent_def_scope
, 0, 0, "event.context");
898 stream_event
->event_context
= container_of(definition
,
899 struct definition_struct
, p
);
900 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
902 if (event
->fields_decl
) {
903 struct definition
*definition
=
904 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
905 stream
->parent_def_scope
, 0, 0, "event.fields");
909 stream_event
->event_fields
= container_of(definition
,
910 struct definition_struct
, p
);
911 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
916 if (stream_event
->event_fields
)
917 definition_unref(&stream_event
->event_fields
->p
);
918 if (stream_event
->event_context
)
919 definition_unref(&stream_event
->event_context
->p
);
924 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
926 struct ctf_stream_class
*stream_class
;
930 if (stream
->stream_definitions_created
)
933 stream_class
= stream
->stream_class
;
935 if (stream_class
->packet_context_decl
) {
936 struct definition
*definition
=
937 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
938 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
943 stream
->stream_packet_context
= container_of(definition
,
944 struct definition_struct
, p
);
945 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
947 if (stream_class
->event_header_decl
) {
948 struct definition
*definition
=
949 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
950 stream
->parent_def_scope
, 0, 0, "stream.event.header");
955 stream
->stream_event_header
=
956 container_of(definition
, struct definition_struct
, p
);
957 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
959 if (stream_class
->event_context_decl
) {
960 struct definition
*definition
=
961 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
962 stream
->parent_def_scope
, 0, 0, "stream.event.context");
967 stream
->stream_event_context
=
968 container_of(definition
, struct definition_struct
, p
);
969 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
971 stream
->events_by_id
= g_ptr_array_new();
972 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
973 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
974 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
975 struct ctf_stream_event
*stream_event
;
979 stream_event
= create_event_definitions(td
, stream
, event
);
982 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
987 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
988 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
990 g_free(stream_event
);
992 g_ptr_array_free(stream
->events_by_id
, TRUE
);
994 if (stream
->stream_event_context
)
995 definition_unref(&stream
->stream_event_context
->p
);
996 if (stream
->stream_event_header
)
997 definition_unref(&stream
->stream_event_header
->p
);
998 if (stream
->stream_packet_context
)
999 definition_unref(&stream
->stream_packet_context
->p
);
1005 int create_stream_packet_index(struct ctf_trace
*td
,
1006 struct ctf_file_stream
*file_stream
)
1008 struct ctf_stream_class
*stream
;
1010 struct ctf_stream_pos
*pos
;
1011 struct stat filestats
;
1012 struct packet_index packet_index
;
1013 int first_packet
= 1;
1016 pos
= &file_stream
->pos
;
1018 ret
= fstat(pos
->fd
, &filestats
);
1022 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1025 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1026 uint64_t stream_id
= 0;
1029 /* unmap old base */
1030 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1032 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1038 /* map new base. Need mapping length from header. */
1039 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1040 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1041 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1042 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1043 pos
->offset
= 0; /* Position of the packet header */
1045 packet_index
.offset
= pos
->mmap_offset
;
1046 packet_index
.content_size
= 0;
1047 packet_index
.packet_size
= 0;
1048 packet_index
.timestamp_begin
= 0;
1049 packet_index
.timestamp_end
= 0;
1050 packet_index
.events_discarded
= 0;
1052 /* read and check header, set stream id (and check) */
1053 if (file_stream
->parent
.trace_packet_header
) {
1054 /* Read packet header */
1055 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1058 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1059 if (len_index
>= 0) {
1060 struct definition
*field
;
1063 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1064 magic
= get_unsigned_int(field
);
1065 if (magic
!= CTF_MAGIC
) {
1066 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1068 file_stream
->pos
.packet_index
->len
,
1069 (ssize_t
) pos
->mmap_offset
);
1075 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1076 if (len_index
>= 0) {
1077 struct definition_array
*defarray
;
1078 struct definition
*field
;
1080 uint8_t uuidval
[UUID_LEN
];
1082 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1083 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1084 defarray
= container_of(field
, struct definition_array
, p
);
1085 assert(array_len(defarray
) == UUID_LEN
);
1087 for (i
= 0; i
< UUID_LEN
; i
++) {
1088 struct definition
*elem
;
1090 elem
= array_index(defarray
, i
);
1091 uuidval
[i
] = get_unsigned_int(elem
);
1093 ret
= uuid_compare(td
->uuid
, uuidval
);
1095 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1101 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1102 if (len_index
>= 0) {
1103 struct definition
*field
;
1105 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1106 stream_id
= get_unsigned_int(field
);
1110 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1111 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1115 file_stream
->parent
.stream_id
= stream_id
;
1116 if (stream_id
>= td
->streams
->len
) {
1117 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1120 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1122 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1125 file_stream
->parent
.stream_class
= stream
;
1126 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1132 if (file_stream
->parent
.stream_packet_context
) {
1133 /* Read packet context */
1134 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1137 /* read content size from header */
1138 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1139 if (len_index
>= 0) {
1140 struct definition
*field
;
1142 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1143 packet_index
.content_size
= get_unsigned_int(field
);
1145 /* Use file size for packet size */
1146 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1149 /* read packet size from header */
1150 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1151 if (len_index
>= 0) {
1152 struct definition
*field
;
1154 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1155 packet_index
.packet_size
= get_unsigned_int(field
);
1157 /* Use content size if non-zero, else file size */
1158 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1161 /* read timestamp begin from header */
1162 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1163 if (len_index
>= 0) {
1164 struct definition
*field
;
1166 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1167 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1170 /* read timestamp end from header */
1171 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1172 if (len_index
>= 0) {
1173 struct definition
*field
;
1175 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1176 packet_index
.timestamp_end
= get_unsigned_int(field
);
1179 /* read events discarded from header */
1180 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1181 if (len_index
>= 0) {
1182 struct definition
*field
;
1184 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1185 packet_index
.events_discarded
= get_unsigned_int(field
);
1188 /* Use file size for packet size */
1189 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1190 /* Use content size if non-zero, else file size */
1191 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1194 /* Validate content size and packet size values */
1195 if (packet_index
.content_size
> packet_index
.packet_size
) {
1196 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1197 packet_index
.content_size
, packet_index
.packet_size
);
1201 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1202 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1203 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1207 /* Save position after header and context */
1208 packet_index
.data_offset
= pos
->offset
;
1210 /* add index to packet array */
1211 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1213 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1216 /* Move pos back to beginning of file */
1217 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1223 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1227 if (td
->packet_header_decl
) {
1228 struct definition
*definition
=
1229 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1230 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1235 stream
->trace_packet_header
=
1236 container_of(definition
, struct definition_struct
, p
);
1237 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1247 * Note: many file streams can inherit from the same stream class
1248 * description (metadata).
1251 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1252 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1256 struct ctf_file_stream
*file_stream
;
1258 ret
= openat(td
->dirfd
, path
, flags
);
1260 perror("File stream openat()");
1263 file_stream
= g_new0(struct ctf_file_stream
, 1);
1266 file_stream
->pos
.packet_seek
= packet_seek
;
1268 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1273 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1274 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1278 * For now, only a single slock is supported.
1280 file_stream
->parent
.current_clock
= td
->single_clock
;
1281 ret
= create_stream_packet_index(td
, file_stream
);
1284 /* Add stream file to stream class */
1285 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1286 &file_stream
->parent
);
1290 if (file_stream
->parent
.trace_packet_header
)
1291 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1293 ctf_fini_pos(&file_stream
->pos
);
1294 close(file_stream
->pos
.fd
);
1295 g_free(file_stream
);
1301 int ctf_open_trace_read(struct ctf_trace
*td
,
1302 const char *path
, int flags
,
1303 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1304 int whence
), FILE *metadata_fp
)
1307 struct dirent
*dirent
;
1308 struct dirent
*diriter
;
1313 /* Open trace directory */
1314 td
->dir
= opendir(path
);
1316 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1321 td
->dirfd
= open(path
, 0);
1322 if (td
->dirfd
< 0) {
1323 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1324 perror("Trace directory open");
1328 strncpy(td
->path
, path
, sizeof(td
->path
));
1329 td
->path
[sizeof(td
->path
) - 1] = '\0';
1332 * Keep the metadata file separate.
1335 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1337 goto error_metadata
;
1341 * Open each stream: for each file, try to open, check magic
1342 * number, and get the stream ID to add to the right location in
1346 dirent_len
= offsetof(struct dirent
, d_name
) +
1347 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1349 dirent
= malloc(dirent_len
);
1352 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1354 fprintf(stderr
, "[error] Readdir error.\n");
1359 /* Ignore hidden files, ., .. and metadata. */
1360 if (!strncmp(diriter
->d_name
, ".", 1)
1361 || !strcmp(diriter
->d_name
, "..")
1362 || !strcmp(diriter
->d_name
, "metadata"))
1364 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1365 flags
, packet_seek
);
1367 fprintf(stderr
, "[error] Open file stream error.\n");
1386 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1387 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1388 int whence
), FILE *metadata_fp
)
1390 struct ctf_trace
*td
;
1394 * If packet_seek is NULL, we provide our default version.
1397 packet_seek
= ctf_packet_seek
;
1399 td
= g_new0(struct ctf_trace
, 1);
1401 switch (flags
& O_ACCMODE
) {
1404 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1407 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1412 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1415 fprintf(stderr
, "[error] Incorrect open flags.\n");
1426 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1427 struct mmap_stream
*mmap_info
)
1429 pos
->mmap_offset
= 0;
1430 pos
->packet_size
= 0;
1431 pos
->content_size
= 0;
1432 pos
->content_size_loc
= NULL
;
1433 pos
->fd
= mmap_info
->fd
;
1438 pos
->packet_index
= NULL
;
1439 pos
->prot
= PROT_READ
;
1440 pos
->flags
= MAP_PRIVATE
;
1441 pos
->parent
.rw_table
= read_dispatch_table
;
1442 pos
->parent
.event_cb
= ctf_read_event
;
1446 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1447 struct ctf_file_stream
*file_stream
)
1449 struct ctf_stream_class
*stream
;
1450 uint64_t stream_id
= 0;
1453 file_stream
->parent
.stream_id
= stream_id
;
1454 if (stream_id
>= td
->streams
->len
) {
1455 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1456 "in metadata.\n", stream_id
);
1460 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1462 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1463 "in metadata.\n", stream_id
);
1467 file_stream
->parent
.stream_class
= stream
;
1468 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1474 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1475 struct mmap_stream
*mmap_info
,
1476 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1480 struct ctf_file_stream
*file_stream
;
1482 file_stream
= g_new0(struct ctf_file_stream
, 1);
1483 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1485 file_stream
->pos
.packet_seek
= packet_seek
;
1487 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1492 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1496 /* Add stream file to stream class */
1497 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1498 &file_stream
->parent
);
1502 if (file_stream
->parent
.trace_packet_header
)
1503 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1505 g_free(file_stream
);
1509 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1510 struct mmap_stream_list
*mmap_list
,
1511 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1516 struct mmap_stream
*mmap_info
;
1518 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1524 * for each stream, try to open, check magic number, and get the
1525 * stream ID to add to the right location in the stream array.
1527 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1528 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1530 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1542 struct trace_descriptor
*ctf_open_mmap_trace(
1543 struct mmap_stream_list
*mmap_list
,
1544 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1548 struct ctf_trace
*td
;
1552 fprintf(stderr
, "[error] No metadata file pointer associated, "
1553 "required for mmap parsing\n");
1557 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1560 td
= g_new0(struct ctf_trace
, 1);
1561 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1574 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1576 ctf_fini_pos(&file_stream
->pos
);
1577 close(file_stream
->pos
.fd
);
1581 void ctf_close_trace(struct trace_descriptor
*tdp
)
1583 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1587 for (i
= 0; i
< td
->streams
->len
; i
++) {
1588 struct ctf_stream_class
*stream
;
1591 stream
= g_ptr_array_index(td
->streams
, i
);
1594 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1595 struct ctf_file_stream
*file_stream
;
1596 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1597 ctf_close_file_stream(file_stream
);
1601 g_ptr_array_free(td
->streams
, TRUE
);
1607 void __attribute__((constructor
)) ctf_init(void)
1611 ctf_format
.name
= g_quark_from_static_string("ctf");
1612 ret
= bt_register_format(&ctf_format
);
1616 /* TODO: finalize */