2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
27 #include <uuid/uuid.h>
31 #include <sys/types.h>
39 #include "metadata/ctf-scanner.h"
40 #include "metadata/ctf-parser.h"
41 #include "metadata/ctf-ast.h"
44 * We currently simply map a page to read the packet header and packet
45 * context to get the packet length and content length. (in bits)
47 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
49 #define UUID_LEN 16 /* uuid by value len */
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
55 #define NSEC_PER_SEC 1000000000ULL
62 uint64_t opt_clock_offset
;
67 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
68 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
69 int whence
), FILE *metadata_fp
);
71 struct trace_descriptor
*ctf_open_mmap_trace(
72 struct mmap_stream_list
*mmap_list
,
73 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
77 void ctf_close_trace(struct trace_descriptor
*descriptor
);
80 rw_dispatch read_dispatch_table
[] = {
81 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
82 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
83 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
84 [ CTF_TYPE_STRING
] = ctf_string_read
,
85 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
86 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
87 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
88 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
92 rw_dispatch write_dispatch_table
[] = {
93 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
94 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
95 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
96 [ CTF_TYPE_STRING
] = ctf_string_write
,
97 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
98 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
99 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
100 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
104 struct format ctf_format
= {
105 .open_trace
= ctf_open_trace
,
106 .open_mmap_trace
= ctf_open_mmap_trace
,
107 .close_trace
= ctf_close_trace
,
111 * Update stream current timestamp, keep at clock frequency.
114 void ctf_update_timestamp(struct ctf_stream
*stream
,
115 struct definition_integer
*integer_definition
)
117 struct declaration_integer
*integer_declaration
=
118 integer_definition
->declaration
;
119 uint64_t oldval
, newval
, updateval
;
121 if (unlikely(integer_declaration
->len
== 64)) {
122 stream
->timestamp
= integer_definition
->value
._unsigned
;
126 oldval
= stream
->timestamp
;
127 oldval
&= (1ULL << integer_declaration
->len
) - 1;
128 newval
= integer_definition
->value
._unsigned
;
129 /* Test for overflow by comparing low bits */
131 newval
+= 1ULL << integer_declaration
->len
;
132 /* updateval contains old high bits, and new low bits (sum) */
133 updateval
= stream
->timestamp
;
134 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
136 stream
->prev_timestamp
= stream
->timestamp
;
137 stream
->timestamp
= updateval
;
141 * Print timestamp, rescaling clock frequency to nanoseconds and
142 * applying offsets as needed (unix time).
144 void ctf_print_timestamp(FILE *fp
,
145 struct ctf_stream
*stream
,
148 uint64_t ts_sec
= 0, ts_nsec
;
149 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
150 struct trace_collection
*tc
= trace
->collection
;
151 uint64_t tc_offset
= tc
->single_clock_offset_avg
;
153 if (stream
->current_clock
->freq
== 1000000000ULL) {
156 ts_nsec
= (uint64_t) ((double) timestamp
* 1000000000.0
157 / (double) stream
->current_clock
->freq
);
161 if (!opt_clock_raw
) {
162 ts_nsec
+= tc_offset
;
164 ts_sec
+= opt_clock_offset
;
166 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
167 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
169 if (!opt_clock_seconds
) {
171 time_t time_s
= (time_t) ts_sec
;
173 if (!opt_clock_gmt
) {
176 res
= localtime_r(&time_s
, &tm
);
178 fprintf(stderr
, "[warning] Unable to get localtime.\n");
184 res
= gmtime_r(&time_s
, &tm
);
186 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
190 if (opt_clock_date
) {
194 /* Print date and time */
195 res
= strftime(timestr
, sizeof(timestr
),
198 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
201 fprintf(fp
, "%s", timestr
);
203 /* Print time in HH:MM:SS.ns */
204 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
205 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
209 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
217 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
219 struct ctf_stream_pos
*pos
=
220 container_of(ppos
, struct ctf_stream_pos
, parent
);
221 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
222 struct ctf_stream_event
*event
;
226 /* We need to check for EOF here for empty files. */
227 if (unlikely(pos
->offset
== EOF
))
230 ctf_pos_get_event(pos
);
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_move_pos_slow(pos
, 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_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
441 struct ctf_file_stream
*file_stream
=
442 container_of(pos
, struct ctf_file_stream
, pos
);
445 struct packet_index
*index
;
447 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
448 *pos
->content_size_loc
= pos
->offset
;
452 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
454 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
462 * The caller should never ask for ctf_move_pos across packets,
463 * except to get exactly at the beginning of the next packet.
465 if (pos
->prot
== PROT_WRITE
) {
468 /* The writer will add padding */
469 assert(pos
->offset
+ offset
== pos
->packet_size
);
470 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
473 assert(offset
== 0); /* only seek supported for now */
479 pos
->content_size
= -1U; /* Unknown at this point */
480 pos
->packet_size
= WRITE_PACKET_LEN
;
481 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
482 pos
->packet_size
/ CHAR_BIT
);
490 uint32_t events_discarded_diff
;
492 if (pos
->offset
== EOF
) {
495 /* For printing discarded event count */
496 index
= &g_array_index(pos
->packet_index
,
497 struct packet_index
, pos
->cur_index
);
498 events_discarded_diff
= index
->events_discarded
;
499 file_stream
->parent
.prev_timestamp_end
=
500 index
->timestamp_end
;
501 if (pos
->cur_index
> 0) {
502 index
= &g_array_index(pos
->packet_index
,
505 events_discarded_diff
-= index
->events_discarded
;
507 file_stream
->parent
.events_discarded
= events_discarded_diff
;
508 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
509 /* The reader will expect us to skip padding */
510 assert(pos
->offset
+ offset
== pos
->content_size
);
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 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
534 file_stream
->parent
.events_discarded
);
535 ctf_print_timestamp(stderr
, &file_stream
->parent
,
536 file_stream
->parent
.prev_timestamp
);
537 fprintf(stderr
, "] and [");
538 ctf_print_timestamp(stderr
, &file_stream
->parent
,
539 file_stream
->parent
.prev_timestamp_end
);
540 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
542 file_stream
->parent
.events_discarded
= 0;
547 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
549 pos
->mmap_offset
= index
->offset
;
551 /* Lookup context/packet size in index */
552 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
553 pos
->content_size
= index
->content_size
;
554 pos
->packet_size
= index
->packet_size
;
555 if (index
->data_offset
< index
->content_size
) {
556 pos
->offset
= 0; /* will read headers */
557 } else if (index
->data_offset
== index
->content_size
) {
559 pos
->offset
= index
->data_offset
;
562 goto read_next_packet
;
568 /* map new base. Need mapping length from header. */
569 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
570 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
571 if (pos
->base
== MAP_FAILED
) {
572 fprintf(stderr
, "[error] mmap error %s.\n",
577 /* update trace_packet_header and stream_packet_context */
578 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
579 /* Read packet header */
580 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
583 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
584 /* Read packet context */
585 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
591 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
597 len
= fread(&magic
, sizeof(magic
), 1, fp
);
601 if (magic
== TSDL_MAGIC
) {
603 td
->byte_order
= BYTE_ORDER
;
604 CTF_TRACE_SET_FIELD(td
, byte_order
);
605 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
607 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
608 LITTLE_ENDIAN
: BIG_ENDIAN
;
609 CTF_TRACE_SET_FIELD(td
, byte_order
);
617 * Returns 0 on success, -1 on error.
620 int check_version(unsigned int major
, unsigned int minor
)
635 /* eventually return an error instead of warning */
637 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
643 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
646 struct metadata_packet_header header
;
647 size_t readlen
, writelen
, toread
;
648 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
651 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
655 if (td
->byte_order
!= BYTE_ORDER
) {
656 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
657 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
658 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
659 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
662 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
663 if (header
.compression_scheme
) {
664 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
665 header
.compression_scheme
);
668 if (header
.encryption_scheme
) {
669 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
670 header
.encryption_scheme
);
673 if (header
.checksum_scheme
) {
674 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
675 header
.checksum_scheme
);
678 if (check_version(header
.major
, header
.minor
) < 0)
680 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
681 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
682 CTF_TRACE_SET_FIELD(td
, uuid
);
684 if (uuid_compare(header
.uuid
, td
->uuid
))
688 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
691 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
696 if (babeltrace_debug
) {
698 fprintf(stderr
, "[debug] metadata packet read: %s\n",
702 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
703 if (writelen
< readlen
) {
713 ret
= 0; /* continue reading next packet */
720 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
721 ret
= fseek(in
, toread
, SEEK_CUR
);
723 fprintf(stderr
, "[warning] Missing padding at end of file\n");
730 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
739 * Using strlen on *buf instead of size of open_memstream
740 * because its size includes garbage at the end (after final
741 * \0). This is the allocated size, not the actual string size.
743 out
= open_memstream(buf
, &size
);
745 perror("Metadata open_memstream");
749 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
758 fclose(out
); /* flush the buffer */
760 /* open for reading */
761 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
763 perror("Metadata fmemopen");
770 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
771 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
772 int whence
), FILE *metadata_fp
)
774 struct ctf_scanner
*scanner
;
775 struct ctf_file_stream
*metadata_stream
;
780 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
783 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
785 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
793 td
->metadata
= &metadata_stream
->parent
;
794 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
795 if (metadata_stream
->pos
.fd
< 0) {
796 fprintf(stderr
, "Unable to open metadata.\n");
797 return metadata_stream
->pos
.fd
;
800 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
802 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
803 perror("Metadata stream open");
808 if (babeltrace_debug
)
811 if (packet_metadata(td
, fp
)) {
812 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
814 goto end_packet_read
;
816 unsigned int major
, minor
;
819 td
->byte_order
= BYTE_ORDER
;
821 /* Check text-only metadata header and version */
822 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
824 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
825 if (check_version(major
, minor
) < 0) {
827 goto end_packet_read
;
832 scanner
= ctf_scanner_alloc(fp
);
834 fprintf(stderr
, "[error] Error allocating scanner\n");
836 goto end_scanner_alloc
;
838 ret
= ctf_scanner_append_ast(scanner
);
840 fprintf(stderr
, "[error] Error creating AST\n");
844 if (babeltrace_debug
) {
845 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
847 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
852 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
854 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
857 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
860 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
864 ctf_scanner_free(scanner
);
870 close(metadata_stream
->pos
.fd
);
872 g_free(metadata_stream
);
877 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
878 struct ctf_stream
*stream
,
879 struct ctf_event
*event
)
881 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
883 if (event
->context_decl
) {
884 struct definition
*definition
=
885 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
886 stream
->parent_def_scope
, 0, 0, "event.context");
890 stream_event
->event_context
= container_of(definition
,
891 struct definition_struct
, p
);
892 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
894 if (event
->fields_decl
) {
895 struct definition
*definition
=
896 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
897 stream
->parent_def_scope
, 0, 0, "event.fields");
901 stream_event
->event_fields
= container_of(definition
,
902 struct definition_struct
, p
);
903 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
908 if (stream_event
->event_fields
)
909 definition_unref(&stream_event
->event_fields
->p
);
910 if (stream_event
->event_context
)
911 definition_unref(&stream_event
->event_context
->p
);
916 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
918 struct ctf_stream_class
*stream_class
;
922 if (stream
->stream_definitions_created
)
925 stream_class
= stream
->stream_class
;
927 if (stream_class
->packet_context_decl
) {
928 struct definition
*definition
=
929 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
930 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
935 stream
->stream_packet_context
= container_of(definition
,
936 struct definition_struct
, p
);
937 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
939 if (stream_class
->event_header_decl
) {
940 struct definition
*definition
=
941 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
942 stream
->parent_def_scope
, 0, 0, "stream.event.header");
947 stream
->stream_event_header
=
948 container_of(definition
, struct definition_struct
, p
);
949 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
951 if (stream_class
->event_context_decl
) {
952 struct definition
*definition
=
953 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
954 stream
->parent_def_scope
, 0, 0, "stream.event.context");
959 stream
->stream_event_context
=
960 container_of(definition
, struct definition_struct
, p
);
961 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
963 stream
->events_by_id
= g_ptr_array_new();
964 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
965 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
966 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
967 struct ctf_stream_event
*stream_event
;
971 stream_event
= create_event_definitions(td
, stream
, event
);
974 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
979 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
980 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
982 g_free(stream_event
);
984 g_ptr_array_free(stream
->events_by_id
, TRUE
);
986 if (stream
->stream_event_context
)
987 definition_unref(&stream
->stream_event_context
->p
);
988 if (stream
->stream_event_header
)
989 definition_unref(&stream
->stream_event_header
->p
);
990 if (stream
->stream_packet_context
)
991 definition_unref(&stream
->stream_packet_context
->p
);
997 int create_stream_packet_index(struct ctf_trace
*td
,
998 struct ctf_file_stream
*file_stream
)
1000 struct ctf_stream_class
*stream
;
1002 struct ctf_stream_pos
*pos
;
1003 struct stat filestats
;
1004 struct packet_index packet_index
;
1005 int first_packet
= 1;
1008 pos
= &file_stream
->pos
;
1010 ret
= fstat(pos
->fd
, &filestats
);
1014 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1017 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1018 uint64_t stream_id
= 0;
1021 /* unmap old base */
1022 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1024 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1030 /* map new base. Need mapping length from header. */
1031 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1032 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1033 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1034 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1035 pos
->offset
= 0; /* Position of the packet header */
1037 packet_index
.offset
= pos
->mmap_offset
;
1038 packet_index
.content_size
= 0;
1039 packet_index
.packet_size
= 0;
1040 packet_index
.timestamp_begin
= 0;
1041 packet_index
.timestamp_end
= 0;
1042 packet_index
.events_discarded
= 0;
1044 /* read and check header, set stream id (and check) */
1045 if (file_stream
->parent
.trace_packet_header
) {
1046 /* Read packet header */
1047 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1050 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1051 if (len_index
>= 0) {
1052 struct definition
*field
;
1055 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1056 magic
= get_unsigned_int(field
);
1057 if (magic
!= CTF_MAGIC
) {
1058 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1060 file_stream
->pos
.packet_index
->len
,
1061 (ssize_t
) pos
->mmap_offset
);
1067 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1068 if (len_index
>= 0) {
1069 struct definition_array
*defarray
;
1070 struct definition
*field
;
1072 uint8_t uuidval
[UUID_LEN
];
1074 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1075 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1076 defarray
= container_of(field
, struct definition_array
, p
);
1077 assert(array_len(defarray
) == UUID_LEN
);
1079 for (i
= 0; i
< UUID_LEN
; i
++) {
1080 struct definition
*elem
;
1082 elem
= array_index(defarray
, i
);
1083 uuidval
[i
] = get_unsigned_int(elem
);
1085 ret
= uuid_compare(td
->uuid
, uuidval
);
1087 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1093 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1094 if (len_index
>= 0) {
1095 struct definition
*field
;
1097 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1098 stream_id
= get_unsigned_int(field
);
1102 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1103 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1107 file_stream
->parent
.stream_id
= stream_id
;
1108 if (stream_id
>= td
->streams
->len
) {
1109 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1112 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1114 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1117 file_stream
->parent
.stream_class
= stream
;
1118 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1124 if (file_stream
->parent
.stream_packet_context
) {
1125 /* Read packet context */
1126 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1129 /* read content size from header */
1130 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1131 if (len_index
>= 0) {
1132 struct definition
*field
;
1134 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1135 packet_index
.content_size
= get_unsigned_int(field
);
1137 /* Use file size for packet size */
1138 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1141 /* read packet size from header */
1142 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1143 if (len_index
>= 0) {
1144 struct definition
*field
;
1146 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1147 packet_index
.packet_size
= get_unsigned_int(field
);
1149 /* Use content size if non-zero, else file size */
1150 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1153 /* read timestamp begin from header */
1154 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1155 if (len_index
>= 0) {
1156 struct definition
*field
;
1158 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1159 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1162 /* read timestamp end from header */
1163 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1164 if (len_index
>= 0) {
1165 struct definition
*field
;
1167 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1168 packet_index
.timestamp_end
= get_unsigned_int(field
);
1171 /* read events discarded from header */
1172 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1173 if (len_index
>= 0) {
1174 struct definition
*field
;
1176 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1177 packet_index
.events_discarded
= get_unsigned_int(field
);
1180 /* Use file size for packet size */
1181 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1182 /* Use content size if non-zero, else file size */
1183 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1186 /* Validate content size and packet size values */
1187 if (packet_index
.content_size
> packet_index
.packet_size
) {
1188 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1189 packet_index
.content_size
, packet_index
.packet_size
);
1193 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1194 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1195 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1199 /* Save position after header and context */
1200 packet_index
.data_offset
= pos
->offset
;
1202 /* add index to packet array */
1203 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1205 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1208 /* Move pos back to beginning of file */
1209 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1215 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1219 if (td
->packet_header_decl
) {
1220 struct definition
*definition
=
1221 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1222 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1227 stream
->trace_packet_header
=
1228 container_of(definition
, struct definition_struct
, p
);
1229 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1239 * Note: many file streams can inherit from the same stream class
1240 * description (metadata).
1243 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1244 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1248 struct ctf_file_stream
*file_stream
;
1250 ret
= openat(td
->dirfd
, path
, flags
);
1252 perror("File stream openat()");
1255 file_stream
= g_new0(struct ctf_file_stream
, 1);
1257 if (move_pos_slow
) {
1258 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1260 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1265 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1266 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1270 * For now, only a single slock is supported.
1272 file_stream
->parent
.current_clock
= td
->single_clock
;
1273 ret
= create_stream_packet_index(td
, file_stream
);
1276 /* Add stream file to stream class */
1277 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1278 &file_stream
->parent
);
1282 if (file_stream
->parent
.trace_packet_header
)
1283 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1285 ctf_fini_pos(&file_stream
->pos
);
1286 close(file_stream
->pos
.fd
);
1287 g_free(file_stream
);
1293 int ctf_open_trace_read(struct ctf_trace
*td
,
1294 const char *path
, int flags
,
1295 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1296 int whence
), FILE *metadata_fp
)
1299 struct dirent
*dirent
;
1300 struct dirent
*diriter
;
1305 /* Open trace directory */
1306 td
->dir
= opendir(path
);
1308 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1313 td
->dirfd
= open(path
, 0);
1314 if (td
->dirfd
< 0) {
1315 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1316 perror("Trace directory open");
1320 strncpy(td
->path
, path
, sizeof(td
->path
));
1321 td
->path
[sizeof(td
->path
) - 1] = '\0';
1324 * Keep the metadata file separate.
1327 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1329 goto error_metadata
;
1333 * Open each stream: for each file, try to open, check magic
1334 * number, and get the stream ID to add to the right location in
1338 dirent_len
= offsetof(struct dirent
, d_name
) +
1339 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1341 dirent
= malloc(dirent_len
);
1344 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1346 fprintf(stderr
, "[error] Readdir error.\n");
1351 /* Ignore hidden files, ., .. and metadata. */
1352 if (!strncmp(diriter
->d_name
, ".", 1)
1353 || !strcmp(diriter
->d_name
, "..")
1354 || !strcmp(diriter
->d_name
, "metadata"))
1356 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1358 fprintf(stderr
, "[error] Open file stream error.\n");
1377 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1378 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1379 int whence
), FILE *metadata_fp
)
1381 struct ctf_trace
*td
;
1384 td
= g_new0(struct ctf_trace
, 1);
1386 switch (flags
& O_ACCMODE
) {
1389 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1392 ret
= ctf_open_trace_read(td
, path
, flags
, move_pos_slow
, metadata_fp
);
1397 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1400 fprintf(stderr
, "[error] Incorrect open flags.\n");
1411 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1412 struct mmap_stream
*mmap_info
)
1414 pos
->mmap_offset
= 0;
1415 pos
->packet_size
= 0;
1416 pos
->content_size
= 0;
1417 pos
->content_size_loc
= NULL
;
1418 pos
->fd
= mmap_info
->fd
;
1423 pos
->packet_index
= NULL
;
1424 pos
->prot
= PROT_READ
;
1425 pos
->flags
= MAP_PRIVATE
;
1426 pos
->parent
.rw_table
= read_dispatch_table
;
1427 pos
->parent
.event_cb
= ctf_read_event
;
1431 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1432 struct ctf_file_stream
*file_stream
)
1434 struct ctf_stream_class
*stream
;
1435 uint64_t stream_id
= 0;
1438 file_stream
->parent
.stream_id
= stream_id
;
1439 if (stream_id
>= td
->streams
->len
) {
1440 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1441 "in metadata.\n", stream_id
);
1445 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1447 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1448 "in metadata.\n", stream_id
);
1452 file_stream
->parent
.stream_class
= stream
;
1453 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1459 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1460 struct mmap_stream
*mmap_info
,
1461 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1465 struct ctf_file_stream
*file_stream
;
1467 file_stream
= g_new0(struct ctf_file_stream
, 1);
1468 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1470 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1472 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1477 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1481 /* Add stream file to stream class */
1482 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1483 &file_stream
->parent
);
1487 if (file_stream
->parent
.trace_packet_header
)
1488 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1490 g_free(file_stream
);
1494 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1495 struct mmap_stream_list
*mmap_list
,
1496 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1501 struct mmap_stream
*mmap_info
;
1503 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1509 * for each stream, try to open, check magic number, and get the
1510 * stream ID to add to the right location in the stream array.
1512 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1513 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1515 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1527 struct trace_descriptor
*ctf_open_mmap_trace(
1528 struct mmap_stream_list
*mmap_list
,
1529 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1532 struct ctf_trace
*td
;
1536 fprintf(stderr
, "[error] No metadata file pointer associated, "
1537 "required for mmap parsing\n");
1540 if (!move_pos_slow
) {
1541 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1544 td
= g_new0(struct ctf_trace
, 1);
1545 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1558 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1560 ctf_fini_pos(&file_stream
->pos
);
1561 close(file_stream
->pos
.fd
);
1565 void ctf_close_trace(struct trace_descriptor
*tdp
)
1567 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1571 for (i
= 0; i
< td
->streams
->len
; i
++) {
1572 struct ctf_stream_class
*stream
;
1575 stream
= g_ptr_array_index(td
->streams
, i
);
1578 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1579 struct ctf_file_stream
*file_stream
;
1580 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1581 ctf_close_file_stream(file_stream
);
1585 g_ptr_array_free(td
->streams
, TRUE
);
1591 void __attribute__((constructor
)) ctf_init(void)
1595 ctf_format
.name
= g_quark_from_static_string("ctf");
1596 ret
= bt_register_format(&ctf_format
);
1600 /* TODO: finalize */