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"
45 * We currently simply map a page to read the packet header and packet
46 * context to get the packet length and content length. (in bits)
48 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
49 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
50 #define UUID_LEN 16 /* uuid by value len */
53 #define min(a, b) (((a) < (b)) ? (a) : (b))
56 #define NSEC_PER_SEC 1000000000ULL
63 uint64_t opt_clock_offset
;
68 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
69 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
73 struct trace_descriptor
*ctf_open_mmap_trace(
74 struct mmap_stream_list
*mmap_list
,
75 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
80 void ctf_close_trace(struct trace_descriptor
*descriptor
);
83 rw_dispatch read_dispatch_table
[] = {
84 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
85 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
86 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
87 [ CTF_TYPE_STRING
] = ctf_string_read
,
88 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
89 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
90 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
91 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
95 rw_dispatch write_dispatch_table
[] = {
96 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
97 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
98 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
99 [ CTF_TYPE_STRING
] = ctf_string_write
,
100 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
101 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
102 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
103 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
107 struct format ctf_format
= {
108 .open_trace
= ctf_open_trace
,
109 .open_mmap_trace
= ctf_open_mmap_trace
,
110 .close_trace
= ctf_close_trace
,
114 * Update stream current timestamp, keep at clock frequency.
117 void ctf_update_timestamp(struct ctf_stream
*stream
,
118 struct definition_integer
*integer_definition
)
120 struct declaration_integer
*integer_declaration
=
121 integer_definition
->declaration
;
122 uint64_t oldval
, newval
, updateval
;
124 if (unlikely(integer_declaration
->len
== 64)) {
125 stream
->timestamp
= integer_definition
->value
._unsigned
;
129 oldval
= stream
->timestamp
;
130 oldval
&= (1ULL << integer_declaration
->len
) - 1;
131 newval
= integer_definition
->value
._unsigned
;
132 /* Test for overflow by comparing low bits */
134 newval
+= 1ULL << integer_declaration
->len
;
135 /* updateval contains old high bits, and new low bits (sum) */
136 updateval
= stream
->timestamp
;
137 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
139 stream
->prev_timestamp
= stream
->timestamp
;
140 stream
->timestamp
= updateval
;
144 * Print timestamp, rescaling clock frequency to nanoseconds and
145 * applying offsets as needed (unix time).
147 void ctf_print_timestamp(FILE *fp
,
148 struct ctf_stream
*stream
,
151 uint64_t ts_sec
= 0, ts_nsec
;
152 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
153 struct trace_collection
*tc
= trace
->collection
;
154 uint64_t tc_offset
= tc
->single_clock_offset_avg
;
156 if (stream
->current_clock
->freq
== 1000000000ULL) {
159 ts_nsec
= (uint64_t) ((double) timestamp
* 1000000000.0
160 / (double) stream
->current_clock
->freq
);
164 if (!opt_clock_raw
) {
165 ts_nsec
+= tc_offset
;
167 ts_sec
+= opt_clock_offset
;
169 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
170 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
172 if (!opt_clock_seconds
) {
174 time_t time_s
= (time_t) ts_sec
;
176 if (!opt_clock_gmt
) {
179 res
= localtime_r(&time_s
, &tm
);
181 fprintf(stderr
, "[warning] Unable to get localtime.\n");
187 res
= gmtime_r(&time_s
, &tm
);
189 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
193 if (opt_clock_date
) {
197 /* Print date and time */
198 res
= strftime(timestr
, sizeof(timestr
),
201 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
204 fprintf(fp
, "%s", timestr
);
206 /* Print time in HH:MM:SS.ns */
207 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
208 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
212 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
220 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
222 struct ctf_stream_pos
*pos
=
223 container_of(ppos
, struct ctf_stream_pos
, parent
);
224 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
225 struct ctf_stream_event
*event
;
229 /* We need to check for EOF here for empty files. */
230 if (unlikely(pos
->offset
== EOF
))
233 ctf_pos_get_event(pos
);
235 /* save the current position as a restore point */
236 pos
->last_offset
= pos
->offset
;
237 /* we just read the event, it is consumed when used by the caller */
238 stream
->consumed
= 0;
241 * This is the EOF check after we've advanced the position in
244 if (unlikely(pos
->offset
== EOF
))
246 assert(pos
->offset
< pos
->content_size
);
248 /* Read event header */
249 if (likely(stream
->stream_event_header
)) {
250 struct definition_integer
*integer_definition
;
251 struct definition
*variant
;
253 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
256 /* lookup event id */
257 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
258 if (integer_definition
) {
259 id
= integer_definition
->value
._unsigned
;
261 struct definition_enum
*enum_definition
;
263 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
264 if (enum_definition
) {
265 id
= enum_definition
->integer
->value
._unsigned
;
269 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
271 integer_definition
= lookup_integer(variant
, "id", FALSE
);
272 if (integer_definition
) {
273 id
= integer_definition
->value
._unsigned
;
276 stream
->event_id
= id
;
278 /* lookup timestamp */
279 stream
->has_timestamp
= 0;
280 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
281 if (integer_definition
) {
282 ctf_update_timestamp(stream
, integer_definition
);
283 stream
->has_timestamp
= 1;
286 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
287 if (integer_definition
) {
288 ctf_update_timestamp(stream
, integer_definition
);
289 stream
->has_timestamp
= 1;
295 /* Read stream-declared event context */
296 if (stream
->stream_event_context
) {
297 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
302 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
303 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
306 event
= g_ptr_array_index(stream
->events_by_id
, id
);
307 if (unlikely(!event
)) {
308 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
312 /* Read event-declared event context */
313 if (event
->event_context
) {
314 ret
= generic_rw(ppos
, &event
->event_context
->p
);
319 /* Read event payload */
320 if (likely(event
->event_fields
)) {
321 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
329 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
334 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
336 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
337 struct ctf_stream_event
*event
;
341 id
= stream
->event_id
;
343 /* print event header */
344 if (likely(stream
->stream_event_header
)) {
345 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
350 /* print stream-declared event context */
351 if (stream
->stream_event_context
) {
352 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
357 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
358 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
361 event
= g_ptr_array_index(stream
->events_by_id
, id
);
362 if (unlikely(!event
)) {
363 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
367 /* print event-declared event context */
368 if (event
->event_context
) {
369 ret
= generic_rw(pos
, &event
->event_context
->p
);
374 /* Read and print event payload */
375 if (likely(event
->event_fields
)) {
376 ret
= generic_rw(pos
, &event
->event_fields
->p
);
384 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
388 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
391 pos
->mmap_offset
= 0;
392 pos
->packet_size
= 0;
393 pos
->content_size
= 0;
394 pos
->content_size_loc
= NULL
;
400 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
401 sizeof(struct packet_index
));
403 pos
->packet_index
= NULL
;
404 switch (open_flags
& O_ACCMODE
) {
406 pos
->prot
= PROT_READ
;
407 pos
->flags
= MAP_PRIVATE
;
408 pos
->parent
.rw_table
= read_dispatch_table
;
409 pos
->parent
.event_cb
= ctf_read_event
;
412 pos
->prot
= PROT_WRITE
; /* Write has priority */
413 pos
->flags
= MAP_SHARED
;
414 pos
->parent
.rw_table
= write_dispatch_table
;
415 pos
->parent
.event_cb
= ctf_write_event
;
417 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
424 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
428 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
429 *pos
->content_size_loc
= pos
->offset
;
432 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
434 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
439 (void) g_array_free(pos
->packet_index
, TRUE
);
443 * for SEEK_CUR: go to next packet.
444 * for SEEK_POS: go to packet numer (index).
446 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
448 struct ctf_stream_pos
*pos
=
449 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
450 struct ctf_file_stream
*file_stream
=
451 container_of(pos
, struct ctf_file_stream
, pos
);
454 struct packet_index
*packet_index
;
456 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
457 *pos
->content_size_loc
= pos
->offset
;
461 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
463 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
471 * The caller should never ask for ctf_move_pos across packets,
472 * except to get exactly at the beginning of the next packet.
474 if (pos
->prot
== PROT_WRITE
) {
477 /* The writer will add padding */
478 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
481 assert(index
== 0); /* only seek supported for now */
487 pos
->content_size
= -1U; /* Unknown at this point */
488 pos
->packet_size
= WRITE_PACKET_LEN
;
489 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
490 pos
->packet_size
/ CHAR_BIT
);
498 uint32_t events_discarded_diff
;
500 if (pos
->offset
== EOF
) {
503 /* For printing discarded event count */
504 packet_index
= &g_array_index(pos
->packet_index
,
505 struct packet_index
, pos
->cur_index
);
506 events_discarded_diff
= packet_index
->events_discarded
;
507 file_stream
->parent
.prev_timestamp_end
=
508 packet_index
->timestamp_end
;
509 if (pos
->cur_index
> 0) {
510 packet_index
= &g_array_index(pos
->packet_index
,
513 events_discarded_diff
-= packet_index
->events_discarded
;
515 file_stream
->parent
.events_discarded
= events_discarded_diff
;
516 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
517 /* The reader will expect us to skip padding */
522 pos
->cur_index
= index
;
523 file_stream
->parent
.prev_timestamp
= 0;
524 file_stream
->parent
.prev_timestamp_end
= 0;
529 if (pos
->cur_index
>= pos
->packet_index
->len
) {
531 * When a stream reaches the end of the
532 * file, we need to show the number of
533 * events discarded ourselves, because
534 * there is no next event scheduled to
535 * be printed in the output.
537 if (file_stream
->parent
.events_discarded
) {
539 * We need to check if we are in trace
540 * read or called from packet indexing.
541 * In this last case, the collection is
542 * not there, so we cannot print the
545 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
547 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
548 file_stream
->parent
.events_discarded
);
549 ctf_print_timestamp(stderr
, &file_stream
->parent
,
550 file_stream
->parent
.prev_timestamp
);
551 fprintf(stderr
, "] and [");
552 ctf_print_timestamp(stderr
, &file_stream
->parent
,
553 file_stream
->parent
.prev_timestamp_end
);
554 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
557 file_stream
->parent
.events_discarded
= 0;
562 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
564 pos
->mmap_offset
= packet_index
->offset
;
566 /* Lookup context/packet size in index */
567 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
568 pos
->content_size
= packet_index
->content_size
;
569 pos
->packet_size
= packet_index
->packet_size
;
570 if (packet_index
->data_offset
< packet_index
->content_size
) {
571 pos
->offset
= 0; /* will read headers */
572 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
574 pos
->offset
= packet_index
->data_offset
;
576 goto read_next_packet
;
582 /* map new base. Need mapping length from header. */
583 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
584 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
585 if (pos
->base
== MAP_FAILED
) {
586 fprintf(stderr
, "[error] mmap error %s.\n",
591 /* update trace_packet_header and stream_packet_context */
592 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
593 /* Read packet header */
594 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
597 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
598 /* Read packet context */
599 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
605 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
611 len
= fread(&magic
, sizeof(magic
), 1, fp
);
615 if (magic
== TSDL_MAGIC
) {
617 td
->byte_order
= BYTE_ORDER
;
618 CTF_TRACE_SET_FIELD(td
, byte_order
);
619 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
621 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
622 LITTLE_ENDIAN
: BIG_ENDIAN
;
623 CTF_TRACE_SET_FIELD(td
, byte_order
);
631 * Returns 0 on success, -1 on error.
634 int check_version(unsigned int major
, unsigned int minor
)
649 /* eventually return an error instead of warning */
651 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
657 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
660 struct metadata_packet_header header
;
661 size_t readlen
, writelen
, toread
;
662 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
665 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
669 if (td
->byte_order
!= BYTE_ORDER
) {
670 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
671 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
672 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
673 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
676 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
677 if (header
.compression_scheme
) {
678 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
679 header
.compression_scheme
);
682 if (header
.encryption_scheme
) {
683 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
684 header
.encryption_scheme
);
687 if (header
.checksum_scheme
) {
688 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
689 header
.checksum_scheme
);
692 if (check_version(header
.major
, header
.minor
) < 0)
694 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
695 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
696 CTF_TRACE_SET_FIELD(td
, uuid
);
698 if (uuid_compare(header
.uuid
, td
->uuid
))
702 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
705 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
710 if (babeltrace_debug
) {
712 fprintf(stderr
, "[debug] metadata packet read: %s\n",
716 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
717 if (writelen
< readlen
) {
727 ret
= 0; /* continue reading next packet */
734 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
735 ret
= fseek(in
, toread
, SEEK_CUR
);
737 fprintf(stderr
, "[warning] Missing padding at end of file\n");
744 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
753 * Using strlen on *buf instead of size of open_memstream
754 * because its size includes garbage at the end (after final
755 * \0). This is the allocated size, not the actual string size.
757 out
= open_memstream(buf
, &size
);
759 perror("Metadata open_memstream");
763 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
772 fclose(out
); /* flush the buffer */
774 /* open for reading */
775 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
777 perror("Metadata fmemopen");
784 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
785 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
786 int whence
), FILE *metadata_fp
)
788 struct ctf_scanner
*scanner
;
789 struct ctf_file_stream
*metadata_stream
;
794 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
797 metadata_stream
->pos
.packet_seek
= packet_seek
;
799 fprintf(stderr
, "[error] packet_seek function undefined.\n");
807 td
->metadata
= &metadata_stream
->parent
;
808 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
809 if (metadata_stream
->pos
.fd
< 0) {
810 fprintf(stderr
, "Unable to open metadata.\n");
811 return metadata_stream
->pos
.fd
;
814 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
816 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
817 perror("Metadata stream open");
822 if (babeltrace_debug
)
825 if (packet_metadata(td
, fp
)) {
826 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
828 goto end_packet_read
;
830 unsigned int major
, minor
;
833 td
->byte_order
= BYTE_ORDER
;
835 /* Check text-only metadata header and version */
836 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
838 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
839 if (check_version(major
, minor
) < 0) {
841 goto end_packet_read
;
846 scanner
= ctf_scanner_alloc(fp
);
848 fprintf(stderr
, "[error] Error allocating scanner\n");
850 goto end_scanner_alloc
;
852 ret
= ctf_scanner_append_ast(scanner
);
854 fprintf(stderr
, "[error] Error creating AST\n");
858 if (babeltrace_debug
) {
859 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
861 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
866 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
868 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
871 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
874 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
878 ctf_scanner_free(scanner
);
884 close(metadata_stream
->pos
.fd
);
886 g_free(metadata_stream
);
891 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
892 struct ctf_stream
*stream
,
893 struct ctf_event
*event
)
895 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
897 if (event
->context_decl
) {
898 struct definition
*definition
=
899 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
900 stream
->parent_def_scope
, 0, 0, "event.context");
904 stream_event
->event_context
= container_of(definition
,
905 struct definition_struct
, p
);
906 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
908 if (event
->fields_decl
) {
909 struct definition
*definition
=
910 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
911 stream
->parent_def_scope
, 0, 0, "event.fields");
915 stream_event
->event_fields
= container_of(definition
,
916 struct definition_struct
, p
);
917 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
922 if (stream_event
->event_fields
)
923 definition_unref(&stream_event
->event_fields
->p
);
924 if (stream_event
->event_context
)
925 definition_unref(&stream_event
->event_context
->p
);
930 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
932 struct ctf_stream_class
*stream_class
;
936 if (stream
->stream_definitions_created
)
939 stream_class
= stream
->stream_class
;
941 if (stream_class
->packet_context_decl
) {
942 struct definition
*definition
=
943 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
944 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
949 stream
->stream_packet_context
= container_of(definition
,
950 struct definition_struct
, p
);
951 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
953 if (stream_class
->event_header_decl
) {
954 struct definition
*definition
=
955 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
956 stream
->parent_def_scope
, 0, 0, "stream.event.header");
961 stream
->stream_event_header
=
962 container_of(definition
, struct definition_struct
, p
);
963 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
965 if (stream_class
->event_context_decl
) {
966 struct definition
*definition
=
967 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
968 stream
->parent_def_scope
, 0, 0, "stream.event.context");
973 stream
->stream_event_context
=
974 container_of(definition
, struct definition_struct
, p
);
975 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
977 stream
->events_by_id
= g_ptr_array_new();
978 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
979 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
980 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
981 struct ctf_stream_event
*stream_event
;
985 stream_event
= create_event_definitions(td
, stream
, event
);
988 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
993 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
994 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
996 g_free(stream_event
);
998 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1000 if (stream
->stream_event_context
)
1001 definition_unref(&stream
->stream_event_context
->p
);
1002 if (stream
->stream_event_header
)
1003 definition_unref(&stream
->stream_event_header
->p
);
1004 if (stream
->stream_packet_context
)
1005 definition_unref(&stream
->stream_packet_context
->p
);
1011 int create_stream_packet_index(struct ctf_trace
*td
,
1012 struct ctf_file_stream
*file_stream
)
1014 struct ctf_stream_class
*stream
;
1016 struct ctf_stream_pos
*pos
;
1017 struct stat filestats
;
1018 struct packet_index packet_index
;
1019 int first_packet
= 1;
1022 pos
= &file_stream
->pos
;
1024 ret
= fstat(pos
->fd
, &filestats
);
1028 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1031 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1032 uint64_t stream_id
= 0;
1035 /* unmap old base */
1036 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1038 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1044 /* map new base. Need mapping length from header. */
1045 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1046 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1047 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1048 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1049 pos
->offset
= 0; /* Position of the packet header */
1051 packet_index
.offset
= pos
->mmap_offset
;
1052 packet_index
.content_size
= 0;
1053 packet_index
.packet_size
= 0;
1054 packet_index
.timestamp_begin
= 0;
1055 packet_index
.timestamp_end
= 0;
1056 packet_index
.events_discarded
= 0;
1058 /* read and check header, set stream id (and check) */
1059 if (file_stream
->parent
.trace_packet_header
) {
1060 /* Read packet header */
1061 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1064 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1065 if (len_index
>= 0) {
1066 struct definition
*field
;
1069 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1070 magic
= get_unsigned_int(field
);
1071 if (magic
!= CTF_MAGIC
) {
1072 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1074 file_stream
->pos
.packet_index
->len
,
1075 (ssize_t
) pos
->mmap_offset
);
1081 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1082 if (len_index
>= 0) {
1083 struct definition_array
*defarray
;
1084 struct definition
*field
;
1086 uint8_t uuidval
[UUID_LEN
];
1088 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1089 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1090 defarray
= container_of(field
, struct definition_array
, p
);
1091 assert(array_len(defarray
) == UUID_LEN
);
1093 for (i
= 0; i
< UUID_LEN
; i
++) {
1094 struct definition
*elem
;
1096 elem
= array_index(defarray
, i
);
1097 uuidval
[i
] = get_unsigned_int(elem
);
1099 ret
= uuid_compare(td
->uuid
, uuidval
);
1101 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1107 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1108 if (len_index
>= 0) {
1109 struct definition
*field
;
1111 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1112 stream_id
= get_unsigned_int(field
);
1116 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1117 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1121 file_stream
->parent
.stream_id
= stream_id
;
1122 if (stream_id
>= td
->streams
->len
) {
1123 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1126 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1128 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1131 file_stream
->parent
.stream_class
= stream
;
1132 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1138 if (file_stream
->parent
.stream_packet_context
) {
1139 /* Read packet context */
1140 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1143 /* read content size from header */
1144 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1145 if (len_index
>= 0) {
1146 struct definition
*field
;
1148 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1149 packet_index
.content_size
= get_unsigned_int(field
);
1151 /* Use file size for packet size */
1152 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1155 /* read packet size from header */
1156 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1157 if (len_index
>= 0) {
1158 struct definition
*field
;
1160 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1161 packet_index
.packet_size
= get_unsigned_int(field
);
1163 /* Use content size if non-zero, else file size */
1164 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1167 /* read timestamp begin from header */
1168 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1169 if (len_index
>= 0) {
1170 struct definition
*field
;
1172 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1173 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1176 /* read timestamp end from header */
1177 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1178 if (len_index
>= 0) {
1179 struct definition
*field
;
1181 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1182 packet_index
.timestamp_end
= get_unsigned_int(field
);
1185 /* read events discarded from header */
1186 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1187 if (len_index
>= 0) {
1188 struct definition
*field
;
1190 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1191 packet_index
.events_discarded
= get_unsigned_int(field
);
1194 /* Use file size for packet size */
1195 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1196 /* Use content size if non-zero, else file size */
1197 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1200 /* Validate content size and packet size values */
1201 if (packet_index
.content_size
> packet_index
.packet_size
) {
1202 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1203 packet_index
.content_size
, packet_index
.packet_size
);
1207 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1208 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1209 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1213 /* Save position after header and context */
1214 packet_index
.data_offset
= pos
->offset
;
1216 /* add index to packet array */
1217 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1219 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1222 /* Move pos back to beginning of file */
1223 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1229 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1233 if (td
->packet_header_decl
) {
1234 struct definition
*definition
=
1235 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1236 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1241 stream
->trace_packet_header
=
1242 container_of(definition
, struct definition_struct
, p
);
1243 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1253 * Note: many file streams can inherit from the same stream class
1254 * description (metadata).
1257 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1258 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1262 struct ctf_file_stream
*file_stream
;
1264 ret
= openat(td
->dirfd
, path
, flags
);
1266 perror("File stream openat()");
1269 file_stream
= g_new0(struct ctf_file_stream
, 1);
1272 file_stream
->pos
.packet_seek
= packet_seek
;
1274 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1279 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1280 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1284 * For now, only a single slock is supported.
1286 file_stream
->parent
.current_clock
= td
->single_clock
;
1287 ret
= create_stream_packet_index(td
, file_stream
);
1290 /* Add stream file to stream class */
1291 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1292 &file_stream
->parent
);
1296 if (file_stream
->parent
.trace_packet_header
)
1297 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1299 ctf_fini_pos(&file_stream
->pos
);
1300 close(file_stream
->pos
.fd
);
1301 g_free(file_stream
);
1307 int ctf_open_trace_read(struct ctf_trace
*td
,
1308 const char *path
, int flags
,
1309 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1310 int whence
), FILE *metadata_fp
)
1313 struct dirent
*dirent
;
1314 struct dirent
*diriter
;
1319 /* Open trace directory */
1320 td
->dir
= opendir(path
);
1322 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1327 td
->dirfd
= open(path
, 0);
1328 if (td
->dirfd
< 0) {
1329 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1330 perror("Trace directory open");
1334 strncpy(td
->path
, path
, sizeof(td
->path
));
1335 td
->path
[sizeof(td
->path
) - 1] = '\0';
1338 * Keep the metadata file separate.
1341 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1343 goto error_metadata
;
1347 * Open each stream: for each file, try to open, check magic
1348 * number, and get the stream ID to add to the right location in
1352 dirent_len
= offsetof(struct dirent
, d_name
) +
1353 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1355 dirent
= malloc(dirent_len
);
1358 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1360 fprintf(stderr
, "[error] Readdir error.\n");
1365 /* Ignore hidden files, ., .. and metadata. */
1366 if (!strncmp(diriter
->d_name
, ".", 1)
1367 || !strcmp(diriter
->d_name
, "..")
1368 || !strcmp(diriter
->d_name
, "metadata"))
1370 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1371 flags
, packet_seek
);
1373 fprintf(stderr
, "[error] Open file stream error.\n");
1392 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1393 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1394 int whence
), FILE *metadata_fp
)
1396 struct ctf_trace
*td
;
1400 * If packet_seek is NULL, we provide our default version.
1403 packet_seek
= ctf_packet_seek
;
1405 td
= g_new0(struct ctf_trace
, 1);
1407 switch (flags
& O_ACCMODE
) {
1410 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1413 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1418 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1421 fprintf(stderr
, "[error] Incorrect open flags.\n");
1432 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1433 struct mmap_stream
*mmap_info
)
1435 pos
->mmap_offset
= 0;
1436 pos
->packet_size
= 0;
1437 pos
->content_size
= 0;
1438 pos
->content_size_loc
= NULL
;
1439 pos
->fd
= mmap_info
->fd
;
1444 pos
->packet_index
= NULL
;
1445 pos
->prot
= PROT_READ
;
1446 pos
->flags
= MAP_PRIVATE
;
1447 pos
->parent
.rw_table
= read_dispatch_table
;
1448 pos
->parent
.event_cb
= ctf_read_event
;
1452 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1453 struct ctf_file_stream
*file_stream
)
1455 struct ctf_stream_class
*stream
;
1456 uint64_t stream_id
= 0;
1459 file_stream
->parent
.stream_id
= stream_id
;
1460 if (stream_id
>= td
->streams
->len
) {
1461 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1462 "in metadata.\n", stream_id
);
1466 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1468 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1469 "in metadata.\n", stream_id
);
1473 file_stream
->parent
.stream_class
= stream
;
1474 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1480 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1481 struct mmap_stream
*mmap_info
,
1482 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1486 struct ctf_file_stream
*file_stream
;
1488 file_stream
= g_new0(struct ctf_file_stream
, 1);
1489 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1491 file_stream
->pos
.packet_seek
= packet_seek
;
1493 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1498 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1502 /* Add stream file to stream class */
1503 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1504 &file_stream
->parent
);
1508 if (file_stream
->parent
.trace_packet_header
)
1509 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1511 g_free(file_stream
);
1515 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1516 struct mmap_stream_list
*mmap_list
,
1517 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1522 struct mmap_stream
*mmap_info
;
1524 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1530 * for each stream, try to open, check magic number, and get the
1531 * stream ID to add to the right location in the stream array.
1533 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1534 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1536 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1548 struct trace_descriptor
*ctf_open_mmap_trace(
1549 struct mmap_stream_list
*mmap_list
,
1550 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1554 struct ctf_trace
*td
;
1558 fprintf(stderr
, "[error] No metadata file pointer associated, "
1559 "required for mmap parsing\n");
1563 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1566 td
= g_new0(struct ctf_trace
, 1);
1567 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1580 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1582 ctf_fini_pos(&file_stream
->pos
);
1583 close(file_stream
->pos
.fd
);
1587 void ctf_close_trace(struct trace_descriptor
*tdp
)
1589 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1593 for (i
= 0; i
< td
->streams
->len
; i
++) {
1594 struct ctf_stream_class
*stream
;
1597 stream
= g_ptr_array_index(td
->streams
, i
);
1600 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1601 struct ctf_file_stream
*file_stream
;
1602 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1603 ctf_close_file_stream(file_stream
);
1607 g_ptr_array_free(td
->streams
, TRUE
);
1613 void __attribute__((constructor
)) ctf_init(void)
1617 ctf_format
.name
= g_quark_from_static_string("ctf");
1618 ret
= bt_register_format(&ctf_format
);
1622 /* TODO: finalize */