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 (*packet_seek
)(struct 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 (*packet_seek
)(struct 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
);
232 /* save the current position as a restore point */
233 pos
->last_offset
= pos
->offset
;
234 /* we just read the event, it is consumed when used by the caller */
235 stream
->consumed
= 0;
238 * This is the EOF check after we've advanced the position in
241 if (unlikely(pos
->offset
== EOF
))
243 assert(pos
->offset
< pos
->content_size
);
245 /* Read event header */
246 if (likely(stream
->stream_event_header
)) {
247 struct definition_integer
*integer_definition
;
248 struct definition
*variant
;
250 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
253 /* lookup event id */
254 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
255 if (integer_definition
) {
256 id
= integer_definition
->value
._unsigned
;
258 struct definition_enum
*enum_definition
;
260 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
261 if (enum_definition
) {
262 id
= enum_definition
->integer
->value
._unsigned
;
266 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
268 integer_definition
= lookup_integer(variant
, "id", FALSE
);
269 if (integer_definition
) {
270 id
= integer_definition
->value
._unsigned
;
273 stream
->event_id
= id
;
275 /* lookup timestamp */
276 stream
->has_timestamp
= 0;
277 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
278 if (integer_definition
) {
279 ctf_update_timestamp(stream
, integer_definition
);
280 stream
->has_timestamp
= 1;
283 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
284 if (integer_definition
) {
285 ctf_update_timestamp(stream
, integer_definition
);
286 stream
->has_timestamp
= 1;
292 /* Read stream-declared event context */
293 if (stream
->stream_event_context
) {
294 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
299 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
300 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
303 event
= g_ptr_array_index(stream
->events_by_id
, id
);
304 if (unlikely(!event
)) {
305 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
309 /* Read event-declared event context */
310 if (event
->event_context
) {
311 ret
= generic_rw(ppos
, &event
->event_context
->p
);
316 /* Read event payload */
317 if (likely(event
->event_fields
)) {
318 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
326 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
331 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
333 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
334 struct ctf_stream_event
*event
;
338 id
= stream
->event_id
;
340 /* print event header */
341 if (likely(stream
->stream_event_header
)) {
342 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
347 /* print stream-declared event context */
348 if (stream
->stream_event_context
) {
349 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
354 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
355 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
358 event
= g_ptr_array_index(stream
->events_by_id
, id
);
359 if (unlikely(!event
)) {
360 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
364 /* print event-declared event context */
365 if (event
->event_context
) {
366 ret
= generic_rw(pos
, &event
->event_context
->p
);
371 /* Read and print event payload */
372 if (likely(event
->event_fields
)) {
373 ret
= generic_rw(pos
, &event
->event_fields
->p
);
381 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
385 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
388 pos
->mmap_offset
= 0;
389 pos
->packet_size
= 0;
390 pos
->content_size
= 0;
391 pos
->content_size_loc
= NULL
;
397 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
398 sizeof(struct packet_index
));
400 pos
->packet_index
= NULL
;
401 switch (open_flags
& O_ACCMODE
) {
403 pos
->prot
= PROT_READ
;
404 pos
->flags
= MAP_PRIVATE
;
405 pos
->parent
.rw_table
= read_dispatch_table
;
406 pos
->parent
.event_cb
= ctf_read_event
;
409 pos
->prot
= PROT_WRITE
; /* Write has priority */
410 pos
->flags
= MAP_SHARED
;
411 pos
->parent
.rw_table
= write_dispatch_table
;
412 pos
->parent
.event_cb
= ctf_write_event
;
414 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
421 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
425 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
426 *pos
->content_size_loc
= pos
->offset
;
429 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
431 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
436 (void) g_array_free(pos
->packet_index
, TRUE
);
439 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t offset
, int whence
)
441 struct ctf_stream_pos
*pos
=
442 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
443 struct ctf_file_stream
*file_stream
=
444 container_of(pos
, struct ctf_file_stream
, pos
);
447 struct packet_index
*index
;
449 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
450 *pos
->content_size_loc
= pos
->offset
;
454 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
456 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
464 * The caller should never ask for ctf_move_pos across packets,
465 * except to get exactly at the beginning of the next packet.
467 if (pos
->prot
== PROT_WRITE
) {
470 /* The writer will add padding */
471 assert(pos
->offset
+ offset
== pos
->packet_size
);
472 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
475 assert(offset
== 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 index
= &g_array_index(pos
->packet_index
,
499 struct packet_index
, pos
->cur_index
);
500 events_discarded_diff
= index
->events_discarded
;
501 file_stream
->parent
.prev_timestamp_end
=
502 index
->timestamp_end
;
503 if (pos
->cur_index
> 0) {
504 index
= &g_array_index(pos
->packet_index
,
507 events_discarded_diff
-= 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 */
512 assert(pos
->offset
+ offset
== pos
->content_size
);
519 file_stream
->parent
.prev_timestamp
= 0;
520 file_stream
->parent
.prev_timestamp_end
= 0;
525 if (pos
->cur_index
>= pos
->packet_index
->len
) {
527 * When a stream reaches the end of the
528 * file, we need to show the number of
529 * events discarded ourselves, because
530 * there is no next event scheduled to
531 * be printed in the output.
533 if (file_stream
->parent
.events_discarded
) {
535 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
536 file_stream
->parent
.events_discarded
);
537 ctf_print_timestamp(stderr
, &file_stream
->parent
,
538 file_stream
->parent
.prev_timestamp
);
539 fprintf(stderr
, "] and [");
540 ctf_print_timestamp(stderr
, &file_stream
->parent
,
541 file_stream
->parent
.prev_timestamp_end
);
542 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
544 file_stream
->parent
.events_discarded
= 0;
549 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
551 pos
->mmap_offset
= index
->offset
;
553 /* Lookup context/packet size in index */
554 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
555 pos
->content_size
= index
->content_size
;
556 pos
->packet_size
= index
->packet_size
;
557 if (index
->data_offset
< index
->content_size
) {
558 pos
->offset
= 0; /* will read headers */
559 } else if (index
->data_offset
== index
->content_size
) {
561 pos
->offset
= index
->data_offset
;
564 goto read_next_packet
;
570 /* map new base. Need mapping length from header. */
571 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
572 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
573 if (pos
->base
== MAP_FAILED
) {
574 fprintf(stderr
, "[error] mmap error %s.\n",
579 /* update trace_packet_header and stream_packet_context */
580 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
581 /* Read packet header */
582 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
585 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
586 /* Read packet context */
587 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
593 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
599 len
= fread(&magic
, sizeof(magic
), 1, fp
);
603 if (magic
== TSDL_MAGIC
) {
605 td
->byte_order
= BYTE_ORDER
;
606 CTF_TRACE_SET_FIELD(td
, byte_order
);
607 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
609 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
610 LITTLE_ENDIAN
: BIG_ENDIAN
;
611 CTF_TRACE_SET_FIELD(td
, byte_order
);
619 * Returns 0 on success, -1 on error.
622 int check_version(unsigned int major
, unsigned int minor
)
637 /* eventually return an error instead of warning */
639 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
645 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
648 struct metadata_packet_header header
;
649 size_t readlen
, writelen
, toread
;
650 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
653 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
657 if (td
->byte_order
!= BYTE_ORDER
) {
658 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
659 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
660 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
661 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
664 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
665 if (header
.compression_scheme
) {
666 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
667 header
.compression_scheme
);
670 if (header
.encryption_scheme
) {
671 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
672 header
.encryption_scheme
);
675 if (header
.checksum_scheme
) {
676 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
677 header
.checksum_scheme
);
680 if (check_version(header
.major
, header
.minor
) < 0)
682 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
683 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
684 CTF_TRACE_SET_FIELD(td
, uuid
);
686 if (uuid_compare(header
.uuid
, td
->uuid
))
690 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
693 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
698 if (babeltrace_debug
) {
700 fprintf(stderr
, "[debug] metadata packet read: %s\n",
704 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
705 if (writelen
< readlen
) {
715 ret
= 0; /* continue reading next packet */
722 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
723 ret
= fseek(in
, toread
, SEEK_CUR
);
725 fprintf(stderr
, "[warning] Missing padding at end of file\n");
732 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
741 * Using strlen on *buf instead of size of open_memstream
742 * because its size includes garbage at the end (after final
743 * \0). This is the allocated size, not the actual string size.
745 out
= open_memstream(buf
, &size
);
747 perror("Metadata open_memstream");
751 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
760 fclose(out
); /* flush the buffer */
762 /* open for reading */
763 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
765 perror("Metadata fmemopen");
772 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
773 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
774 int whence
), FILE *metadata_fp
)
776 struct ctf_scanner
*scanner
;
777 struct ctf_file_stream
*metadata_stream
;
782 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
785 metadata_stream
->pos
.packet_seek
= packet_seek
;
787 fprintf(stderr
, "[error] packet_seek function undefined.\n");
795 td
->metadata
= &metadata_stream
->parent
;
796 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
797 if (metadata_stream
->pos
.fd
< 0) {
798 fprintf(stderr
, "Unable to open metadata.\n");
799 return metadata_stream
->pos
.fd
;
802 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
804 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
805 perror("Metadata stream open");
810 if (babeltrace_debug
)
813 if (packet_metadata(td
, fp
)) {
814 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
816 goto end_packet_read
;
818 unsigned int major
, minor
;
821 td
->byte_order
= BYTE_ORDER
;
823 /* Check text-only metadata header and version */
824 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
826 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
827 if (check_version(major
, minor
) < 0) {
829 goto end_packet_read
;
834 scanner
= ctf_scanner_alloc(fp
);
836 fprintf(stderr
, "[error] Error allocating scanner\n");
838 goto end_scanner_alloc
;
840 ret
= ctf_scanner_append_ast(scanner
);
842 fprintf(stderr
, "[error] Error creating AST\n");
846 if (babeltrace_debug
) {
847 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
849 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
854 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
856 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
859 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
862 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
866 ctf_scanner_free(scanner
);
872 close(metadata_stream
->pos
.fd
);
874 g_free(metadata_stream
);
879 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
880 struct ctf_stream
*stream
,
881 struct ctf_event
*event
)
883 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
885 if (event
->context_decl
) {
886 struct definition
*definition
=
887 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
888 stream
->parent_def_scope
, 0, 0, "event.context");
892 stream_event
->event_context
= container_of(definition
,
893 struct definition_struct
, p
);
894 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
896 if (event
->fields_decl
) {
897 struct definition
*definition
=
898 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
899 stream
->parent_def_scope
, 0, 0, "event.fields");
903 stream_event
->event_fields
= container_of(definition
,
904 struct definition_struct
, p
);
905 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
910 if (stream_event
->event_fields
)
911 definition_unref(&stream_event
->event_fields
->p
);
912 if (stream_event
->event_context
)
913 definition_unref(&stream_event
->event_context
->p
);
918 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
920 struct ctf_stream_class
*stream_class
;
924 if (stream
->stream_definitions_created
)
927 stream_class
= stream
->stream_class
;
929 if (stream_class
->packet_context_decl
) {
930 struct definition
*definition
=
931 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
932 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
937 stream
->stream_packet_context
= container_of(definition
,
938 struct definition_struct
, p
);
939 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
941 if (stream_class
->event_header_decl
) {
942 struct definition
*definition
=
943 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
944 stream
->parent_def_scope
, 0, 0, "stream.event.header");
949 stream
->stream_event_header
=
950 container_of(definition
, struct definition_struct
, p
);
951 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
953 if (stream_class
->event_context_decl
) {
954 struct definition
*definition
=
955 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
956 stream
->parent_def_scope
, 0, 0, "stream.event.context");
961 stream
->stream_event_context
=
962 container_of(definition
, struct definition_struct
, p
);
963 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
965 stream
->events_by_id
= g_ptr_array_new();
966 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
967 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
968 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
969 struct ctf_stream_event
*stream_event
;
973 stream_event
= create_event_definitions(td
, stream
, event
);
976 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
981 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
982 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
984 g_free(stream_event
);
986 g_ptr_array_free(stream
->events_by_id
, TRUE
);
988 if (stream
->stream_event_context
)
989 definition_unref(&stream
->stream_event_context
->p
);
990 if (stream
->stream_event_header
)
991 definition_unref(&stream
->stream_event_header
->p
);
992 if (stream
->stream_packet_context
)
993 definition_unref(&stream
->stream_packet_context
->p
);
999 int create_stream_packet_index(struct ctf_trace
*td
,
1000 struct ctf_file_stream
*file_stream
)
1002 struct ctf_stream_class
*stream
;
1004 struct ctf_stream_pos
*pos
;
1005 struct stat filestats
;
1006 struct packet_index packet_index
;
1007 int first_packet
= 1;
1010 pos
= &file_stream
->pos
;
1012 ret
= fstat(pos
->fd
, &filestats
);
1016 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1019 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1020 uint64_t stream_id
= 0;
1023 /* unmap old base */
1024 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1026 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1032 /* map new base. Need mapping length from header. */
1033 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1034 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1035 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1036 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1037 pos
->offset
= 0; /* Position of the packet header */
1039 packet_index
.offset
= pos
->mmap_offset
;
1040 packet_index
.content_size
= 0;
1041 packet_index
.packet_size
= 0;
1042 packet_index
.timestamp_begin
= 0;
1043 packet_index
.timestamp_end
= 0;
1044 packet_index
.events_discarded
= 0;
1046 /* read and check header, set stream id (and check) */
1047 if (file_stream
->parent
.trace_packet_header
) {
1048 /* Read packet header */
1049 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1052 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1053 if (len_index
>= 0) {
1054 struct definition
*field
;
1057 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1058 magic
= get_unsigned_int(field
);
1059 if (magic
!= CTF_MAGIC
) {
1060 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1062 file_stream
->pos
.packet_index
->len
,
1063 (ssize_t
) pos
->mmap_offset
);
1069 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1070 if (len_index
>= 0) {
1071 struct definition_array
*defarray
;
1072 struct definition
*field
;
1074 uint8_t uuidval
[UUID_LEN
];
1076 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1077 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1078 defarray
= container_of(field
, struct definition_array
, p
);
1079 assert(array_len(defarray
) == UUID_LEN
);
1081 for (i
= 0; i
< UUID_LEN
; i
++) {
1082 struct definition
*elem
;
1084 elem
= array_index(defarray
, i
);
1085 uuidval
[i
] = get_unsigned_int(elem
);
1087 ret
= uuid_compare(td
->uuid
, uuidval
);
1089 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1095 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1096 if (len_index
>= 0) {
1097 struct definition
*field
;
1099 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1100 stream_id
= get_unsigned_int(field
);
1104 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1105 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1109 file_stream
->parent
.stream_id
= stream_id
;
1110 if (stream_id
>= td
->streams
->len
) {
1111 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1114 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1116 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1119 file_stream
->parent
.stream_class
= stream
;
1120 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1126 if (file_stream
->parent
.stream_packet_context
) {
1127 /* Read packet context */
1128 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1131 /* read content size from header */
1132 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1133 if (len_index
>= 0) {
1134 struct definition
*field
;
1136 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1137 packet_index
.content_size
= get_unsigned_int(field
);
1139 /* Use file size for packet size */
1140 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1143 /* read packet size from header */
1144 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_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
.packet_size
= get_unsigned_int(field
);
1151 /* Use content size if non-zero, else file size */
1152 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1155 /* read timestamp begin from header */
1156 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
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
.timestamp_begin
= get_unsigned_int(field
);
1164 /* read timestamp end from header */
1165 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1166 if (len_index
>= 0) {
1167 struct definition
*field
;
1169 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1170 packet_index
.timestamp_end
= get_unsigned_int(field
);
1173 /* read events discarded from header */
1174 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1175 if (len_index
>= 0) {
1176 struct definition
*field
;
1178 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1179 packet_index
.events_discarded
= get_unsigned_int(field
);
1182 /* Use file size for packet size */
1183 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1184 /* Use content size if non-zero, else file size */
1185 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1188 /* Validate content size and packet size values */
1189 if (packet_index
.content_size
> packet_index
.packet_size
) {
1190 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1191 packet_index
.content_size
, packet_index
.packet_size
);
1195 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1196 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1197 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1201 /* Save position after header and context */
1202 packet_index
.data_offset
= pos
->offset
;
1204 /* add index to packet array */
1205 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1207 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1210 /* Move pos back to beginning of file */
1211 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1217 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1221 if (td
->packet_header_decl
) {
1222 struct definition
*definition
=
1223 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1224 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1229 stream
->trace_packet_header
=
1230 container_of(definition
, struct definition_struct
, p
);
1231 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1241 * Note: many file streams can inherit from the same stream class
1242 * description (metadata).
1245 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1246 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
1250 struct ctf_file_stream
*file_stream
;
1252 ret
= openat(td
->dirfd
, path
, flags
);
1254 perror("File stream openat()");
1257 file_stream
= g_new0(struct ctf_file_stream
, 1);
1260 file_stream
->pos
.packet_seek
= packet_seek
;
1262 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1267 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1268 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1272 * For now, only a single slock is supported.
1274 file_stream
->parent
.current_clock
= td
->single_clock
;
1275 ret
= create_stream_packet_index(td
, file_stream
);
1278 /* Add stream file to stream class */
1279 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1280 &file_stream
->parent
);
1284 if (file_stream
->parent
.trace_packet_header
)
1285 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1287 ctf_fini_pos(&file_stream
->pos
);
1288 close(file_stream
->pos
.fd
);
1289 g_free(file_stream
);
1295 int ctf_open_trace_read(struct ctf_trace
*td
,
1296 const char *path
, int flags
,
1297 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
1298 int whence
), FILE *metadata_fp
)
1301 struct dirent
*dirent
;
1302 struct dirent
*diriter
;
1307 /* Open trace directory */
1308 td
->dir
= opendir(path
);
1310 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1315 td
->dirfd
= open(path
, 0);
1316 if (td
->dirfd
< 0) {
1317 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1318 perror("Trace directory open");
1322 strncpy(td
->path
, path
, sizeof(td
->path
));
1323 td
->path
[sizeof(td
->path
) - 1] = '\0';
1326 * Keep the metadata file separate.
1329 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1331 goto error_metadata
;
1335 * Open each stream: for each file, try to open, check magic
1336 * number, and get the stream ID to add to the right location in
1340 dirent_len
= offsetof(struct dirent
, d_name
) +
1341 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1343 dirent
= malloc(dirent_len
);
1346 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1348 fprintf(stderr
, "[error] Readdir error.\n");
1353 /* Ignore hidden files, ., .. and metadata. */
1354 if (!strncmp(diriter
->d_name
, ".", 1)
1355 || !strcmp(diriter
->d_name
, "..")
1356 || !strcmp(diriter
->d_name
, "metadata"))
1358 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1359 flags
, packet_seek
);
1361 fprintf(stderr
, "[error] Open file stream error.\n");
1380 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1381 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
1382 int whence
), FILE *metadata_fp
)
1384 struct ctf_trace
*td
;
1388 * If packet_seek is NULL, we provide our default version.
1391 packet_seek
= ctf_packet_seek
;
1393 td
= g_new0(struct ctf_trace
, 1);
1395 switch (flags
& O_ACCMODE
) {
1398 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1401 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1406 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1409 fprintf(stderr
, "[error] Incorrect open flags.\n");
1420 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1421 struct mmap_stream
*mmap_info
)
1423 pos
->mmap_offset
= 0;
1424 pos
->packet_size
= 0;
1425 pos
->content_size
= 0;
1426 pos
->content_size_loc
= NULL
;
1427 pos
->fd
= mmap_info
->fd
;
1432 pos
->packet_index
= NULL
;
1433 pos
->prot
= PROT_READ
;
1434 pos
->flags
= MAP_PRIVATE
;
1435 pos
->parent
.rw_table
= read_dispatch_table
;
1436 pos
->parent
.event_cb
= ctf_read_event
;
1440 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1441 struct ctf_file_stream
*file_stream
)
1443 struct ctf_stream_class
*stream
;
1444 uint64_t stream_id
= 0;
1447 file_stream
->parent
.stream_id
= stream_id
;
1448 if (stream_id
>= td
->streams
->len
) {
1449 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1450 "in metadata.\n", stream_id
);
1454 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1456 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1457 "in metadata.\n", stream_id
);
1461 file_stream
->parent
.stream_class
= stream
;
1462 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1468 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1469 struct mmap_stream
*mmap_info
,
1470 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
1474 struct ctf_file_stream
*file_stream
;
1476 file_stream
= g_new0(struct ctf_file_stream
, 1);
1477 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1479 file_stream
->pos
.packet_seek
= packet_seek
;
1481 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1486 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1490 /* Add stream file to stream class */
1491 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1492 &file_stream
->parent
);
1496 if (file_stream
->parent
.trace_packet_header
)
1497 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1499 g_free(file_stream
);
1503 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1504 struct mmap_stream_list
*mmap_list
,
1505 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
,
1510 struct mmap_stream
*mmap_info
;
1512 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1518 * for each stream, try to open, check magic number, and get the
1519 * stream ID to add to the right location in the stream array.
1521 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1522 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1524 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1536 struct trace_descriptor
*ctf_open_mmap_trace(
1537 struct mmap_stream_list
*mmap_list
,
1538 void (*packet_seek
)(struct stream_pos
*pos
, size_t offset
, int whence
),
1541 struct ctf_trace
*td
;
1545 fprintf(stderr
, "[error] No metadata file pointer associated, "
1546 "required for mmap parsing\n");
1550 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1553 td
= g_new0(struct ctf_trace
, 1);
1554 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1567 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1569 ctf_fini_pos(&file_stream
->pos
);
1570 close(file_stream
->pos
.fd
);
1574 void ctf_close_trace(struct trace_descriptor
*tdp
)
1576 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1580 for (i
= 0; i
< td
->streams
->len
; i
++) {
1581 struct ctf_stream_class
*stream
;
1584 stream
= g_ptr_array_index(td
->streams
, i
);
1587 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1588 struct ctf_file_stream
*file_stream
;
1589 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1590 ctf_close_file_stream(file_stream
);
1594 g_ptr_array_free(td
->streams
, TRUE
);
1600 void __attribute__((constructor
)) ctf_init(void)
1604 ctf_format
.name
= g_quark_from_static_string("ctf");
1605 ret
= bt_register_format(&ctf_format
);
1609 /* TODO: finalize */