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 index
,
72 struct trace_descriptor
*ctf_open_mmap_trace(
73 struct mmap_stream_list
*mmap_list
,
74 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
79 void ctf_close_trace(struct trace_descriptor
*descriptor
);
82 rw_dispatch read_dispatch_table
[] = {
83 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
84 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
85 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
86 [ CTF_TYPE_STRING
] = ctf_string_read
,
87 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
88 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
89 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
90 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
94 rw_dispatch write_dispatch_table
[] = {
95 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
96 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
97 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
98 [ CTF_TYPE_STRING
] = ctf_string_write
,
99 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
100 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
101 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
102 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
106 struct format ctf_format
= {
107 .open_trace
= ctf_open_trace
,
108 .open_mmap_trace
= ctf_open_mmap_trace
,
109 .close_trace
= ctf_close_trace
,
113 * Update stream current timestamp, keep at clock frequency.
116 void ctf_update_timestamp(struct ctf_stream
*stream
,
117 struct definition_integer
*integer_definition
)
119 struct declaration_integer
*integer_declaration
=
120 integer_definition
->declaration
;
121 uint64_t oldval
, newval
, updateval
;
123 if (unlikely(integer_declaration
->len
== 64)) {
124 stream
->timestamp
= integer_definition
->value
._unsigned
;
128 oldval
= stream
->timestamp
;
129 oldval
&= (1ULL << integer_declaration
->len
) - 1;
130 newval
= integer_definition
->value
._unsigned
;
131 /* Test for overflow by comparing low bits */
133 newval
+= 1ULL << integer_declaration
->len
;
134 /* updateval contains old high bits, and new low bits (sum) */
135 updateval
= stream
->timestamp
;
136 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
138 stream
->prev_timestamp
= stream
->timestamp
;
139 stream
->timestamp
= updateval
;
143 * Print timestamp, rescaling clock frequency to nanoseconds and
144 * applying offsets as needed (unix time).
146 void ctf_print_timestamp(FILE *fp
,
147 struct ctf_stream
*stream
,
150 uint64_t ts_sec
= 0, ts_nsec
;
151 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
152 struct trace_collection
*tc
= trace
->collection
;
153 uint64_t tc_offset
= tc
->single_clock_offset_avg
;
155 if (stream
->current_clock
->freq
== 1000000000ULL) {
158 ts_nsec
= (uint64_t) ((double) timestamp
* 1000000000.0
159 / (double) stream
->current_clock
->freq
);
163 if (!opt_clock_raw
) {
164 ts_nsec
+= tc_offset
;
166 ts_sec
+= opt_clock_offset
;
168 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
169 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
171 if (!opt_clock_seconds
) {
173 time_t time_s
= (time_t) ts_sec
;
175 if (!opt_clock_gmt
) {
178 res
= localtime_r(&time_s
, &tm
);
180 fprintf(stderr
, "[warning] Unable to get localtime.\n");
186 res
= gmtime_r(&time_s
, &tm
);
188 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
192 if (opt_clock_date
) {
196 /* Print date and time */
197 res
= strftime(timestr
, sizeof(timestr
),
200 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
203 fprintf(fp
, "%s", timestr
);
205 /* Print time in HH:MM:SS.ns */
206 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
207 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
211 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
219 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
221 struct ctf_stream_pos
*pos
=
222 container_of(ppos
, struct ctf_stream_pos
, parent
);
223 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
224 struct ctf_stream_event
*event
;
228 /* We need to check for EOF here for empty files. */
229 if (unlikely(pos
->offset
== EOF
))
232 ctf_pos_get_event(pos
);
234 /* save the current position as a restore point */
235 pos
->last_offset
= pos
->offset
;
236 /* we just read the event, it is consumed when used by the caller */
237 stream
->consumed
= 0;
240 * This is the EOF check after we've advanced the position in
243 if (unlikely(pos
->offset
== EOF
))
245 assert(pos
->offset
< pos
->content_size
);
247 /* Read event header */
248 if (likely(stream
->stream_event_header
)) {
249 struct definition_integer
*integer_definition
;
250 struct definition
*variant
;
252 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
255 /* lookup event id */
256 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
257 if (integer_definition
) {
258 id
= integer_definition
->value
._unsigned
;
260 struct definition_enum
*enum_definition
;
262 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
263 if (enum_definition
) {
264 id
= enum_definition
->integer
->value
._unsigned
;
268 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
270 integer_definition
= lookup_integer(variant
, "id", FALSE
);
271 if (integer_definition
) {
272 id
= integer_definition
->value
._unsigned
;
275 stream
->event_id
= id
;
277 /* lookup timestamp */
278 stream
->has_timestamp
= 0;
279 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
280 if (integer_definition
) {
281 ctf_update_timestamp(stream
, integer_definition
);
282 stream
->has_timestamp
= 1;
285 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
286 if (integer_definition
) {
287 ctf_update_timestamp(stream
, integer_definition
);
288 stream
->has_timestamp
= 1;
294 /* Read stream-declared event context */
295 if (stream
->stream_event_context
) {
296 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
301 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
302 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
305 event
= g_ptr_array_index(stream
->events_by_id
, id
);
306 if (unlikely(!event
)) {
307 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
311 /* Read event-declared event context */
312 if (event
->event_context
) {
313 ret
= generic_rw(ppos
, &event
->event_context
->p
);
318 /* Read event payload */
319 if (likely(event
->event_fields
)) {
320 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
328 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
333 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
335 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
336 struct ctf_stream_event
*event
;
340 id
= stream
->event_id
;
342 /* print event header */
343 if (likely(stream
->stream_event_header
)) {
344 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
349 /* print stream-declared event context */
350 if (stream
->stream_event_context
) {
351 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
356 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
357 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
360 event
= g_ptr_array_index(stream
->events_by_id
, id
);
361 if (unlikely(!event
)) {
362 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
366 /* print event-declared event context */
367 if (event
->event_context
) {
368 ret
= generic_rw(pos
, &event
->event_context
->p
);
373 /* Read and print event payload */
374 if (likely(event
->event_fields
)) {
375 ret
= generic_rw(pos
, &event
->event_fields
->p
);
383 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
387 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
390 pos
->mmap_offset
= 0;
391 pos
->packet_size
= 0;
392 pos
->content_size
= 0;
393 pos
->content_size_loc
= NULL
;
399 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
400 sizeof(struct packet_index
));
402 pos
->packet_index
= NULL
;
403 switch (open_flags
& O_ACCMODE
) {
405 pos
->prot
= PROT_READ
;
406 pos
->flags
= MAP_PRIVATE
;
407 pos
->parent
.rw_table
= read_dispatch_table
;
408 pos
->parent
.event_cb
= ctf_read_event
;
411 pos
->prot
= PROT_WRITE
; /* Write has priority */
412 pos
->flags
= MAP_SHARED
;
413 pos
->parent
.rw_table
= write_dispatch_table
;
414 pos
->parent
.event_cb
= ctf_write_event
;
416 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
423 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
427 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
428 *pos
->content_size_loc
= pos
->offset
;
431 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
433 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
438 (void) g_array_free(pos
->packet_index
, TRUE
);
442 * for SEEK_CUR: go to next packet.
443 * for SEEK_POS: go to packet numer (index).
445 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
447 struct ctf_stream_pos
*pos
=
448 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
449 struct ctf_file_stream
*file_stream
=
450 container_of(pos
, struct ctf_file_stream
, pos
);
453 struct packet_index
*packet_index
;
455 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
456 *pos
->content_size_loc
= pos
->offset
;
460 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
462 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
470 * The caller should never ask for ctf_move_pos across packets,
471 * except to get exactly at the beginning of the next packet.
473 if (pos
->prot
== PROT_WRITE
) {
476 /* The writer will add padding */
477 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
480 assert(index
== 0); /* only seek supported for now */
486 pos
->content_size
= -1U; /* Unknown at this point */
487 pos
->packet_size
= WRITE_PACKET_LEN
;
488 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
489 pos
->packet_size
/ CHAR_BIT
);
497 uint32_t events_discarded_diff
;
499 if (pos
->offset
== EOF
) {
502 /* For printing discarded event count */
503 packet_index
= &g_array_index(pos
->packet_index
,
504 struct packet_index
, pos
->cur_index
);
505 events_discarded_diff
= packet_index
->events_discarded
;
506 file_stream
->parent
.prev_timestamp_end
=
507 packet_index
->timestamp_end
;
508 if (pos
->cur_index
> 0) {
509 packet_index
= &g_array_index(pos
->packet_index
,
512 events_discarded_diff
-= packet_index
->events_discarded
;
514 file_stream
->parent
.events_discarded
= events_discarded_diff
;
515 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
516 /* The reader will expect us to skip padding */
521 pos
->cur_index
= index
;
522 file_stream
->parent
.prev_timestamp
= 0;
523 file_stream
->parent
.prev_timestamp_end
= 0;
528 if (pos
->cur_index
>= pos
->packet_index
->len
) {
530 * When a stream reaches the end of the
531 * file, we need to show the number of
532 * events discarded ourselves, because
533 * there is no next event scheduled to
534 * be printed in the output.
536 if (file_stream
->parent
.events_discarded
) {
538 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
539 file_stream
->parent
.events_discarded
);
540 ctf_print_timestamp(stderr
, &file_stream
->parent
,
541 file_stream
->parent
.prev_timestamp
);
542 fprintf(stderr
, "] and [");
543 ctf_print_timestamp(stderr
, &file_stream
->parent
,
544 file_stream
->parent
.prev_timestamp_end
);
545 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
547 file_stream
->parent
.events_discarded
= 0;
552 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
554 pos
->mmap_offset
= packet_index
->offset
;
556 /* Lookup context/packet size in index */
557 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
558 pos
->content_size
= packet_index
->content_size
;
559 pos
->packet_size
= packet_index
->packet_size
;
560 if (packet_index
->data_offset
< packet_index
->content_size
) {
561 pos
->offset
= 0; /* will read headers */
562 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
564 pos
->offset
= packet_index
->data_offset
;
566 goto read_next_packet
;
572 /* map new base. Need mapping length from header. */
573 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
574 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
575 if (pos
->base
== MAP_FAILED
) {
576 fprintf(stderr
, "[error] mmap error %s.\n",
581 /* update trace_packet_header and stream_packet_context */
582 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
583 /* Read packet header */
584 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
587 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
588 /* Read packet context */
589 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
595 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
601 len
= fread(&magic
, sizeof(magic
), 1, fp
);
605 if (magic
== TSDL_MAGIC
) {
607 td
->byte_order
= BYTE_ORDER
;
608 CTF_TRACE_SET_FIELD(td
, byte_order
);
609 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
611 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
612 LITTLE_ENDIAN
: BIG_ENDIAN
;
613 CTF_TRACE_SET_FIELD(td
, byte_order
);
621 * Returns 0 on success, -1 on error.
624 int check_version(unsigned int major
, unsigned int minor
)
639 /* eventually return an error instead of warning */
641 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
647 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
650 struct metadata_packet_header header
;
651 size_t readlen
, writelen
, toread
;
652 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
655 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
659 if (td
->byte_order
!= BYTE_ORDER
) {
660 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
661 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
662 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
663 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
666 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
667 if (header
.compression_scheme
) {
668 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
669 header
.compression_scheme
);
672 if (header
.encryption_scheme
) {
673 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
674 header
.encryption_scheme
);
677 if (header
.checksum_scheme
) {
678 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
679 header
.checksum_scheme
);
682 if (check_version(header
.major
, header
.minor
) < 0)
684 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
685 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
686 CTF_TRACE_SET_FIELD(td
, uuid
);
688 if (uuid_compare(header
.uuid
, td
->uuid
))
692 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
695 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
700 if (babeltrace_debug
) {
702 fprintf(stderr
, "[debug] metadata packet read: %s\n",
706 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
707 if (writelen
< readlen
) {
717 ret
= 0; /* continue reading next packet */
724 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
725 ret
= fseek(in
, toread
, SEEK_CUR
);
727 fprintf(stderr
, "[warning] Missing padding at end of file\n");
734 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
743 * Using strlen on *buf instead of size of open_memstream
744 * because its size includes garbage at the end (after final
745 * \0). This is the allocated size, not the actual string size.
747 out
= open_memstream(buf
, &size
);
749 perror("Metadata open_memstream");
753 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
762 fclose(out
); /* flush the buffer */
764 /* open for reading */
765 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
767 perror("Metadata fmemopen");
774 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
775 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
776 int whence
), FILE *metadata_fp
)
778 struct ctf_scanner
*scanner
;
779 struct ctf_file_stream
*metadata_stream
;
784 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
787 metadata_stream
->pos
.packet_seek
= packet_seek
;
789 fprintf(stderr
, "[error] packet_seek function undefined.\n");
797 td
->metadata
= &metadata_stream
->parent
;
798 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
799 if (metadata_stream
->pos
.fd
< 0) {
800 fprintf(stderr
, "Unable to open metadata.\n");
801 return metadata_stream
->pos
.fd
;
804 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
806 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
807 perror("Metadata stream open");
812 if (babeltrace_debug
)
815 if (packet_metadata(td
, fp
)) {
816 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
818 goto end_packet_read
;
820 unsigned int major
, minor
;
823 td
->byte_order
= BYTE_ORDER
;
825 /* Check text-only metadata header and version */
826 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
828 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
829 if (check_version(major
, minor
) < 0) {
831 goto end_packet_read
;
836 scanner
= ctf_scanner_alloc(fp
);
838 fprintf(stderr
, "[error] Error allocating scanner\n");
840 goto end_scanner_alloc
;
842 ret
= ctf_scanner_append_ast(scanner
);
844 fprintf(stderr
, "[error] Error creating AST\n");
848 if (babeltrace_debug
) {
849 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
851 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
856 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
858 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
861 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
864 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
868 ctf_scanner_free(scanner
);
874 close(metadata_stream
->pos
.fd
);
876 g_free(metadata_stream
);
881 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
882 struct ctf_stream
*stream
,
883 struct ctf_event
*event
)
885 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
887 if (event
->context_decl
) {
888 struct definition
*definition
=
889 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
890 stream
->parent_def_scope
, 0, 0, "event.context");
894 stream_event
->event_context
= container_of(definition
,
895 struct definition_struct
, p
);
896 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
898 if (event
->fields_decl
) {
899 struct definition
*definition
=
900 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
901 stream
->parent_def_scope
, 0, 0, "event.fields");
905 stream_event
->event_fields
= container_of(definition
,
906 struct definition_struct
, p
);
907 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
912 if (stream_event
->event_fields
)
913 definition_unref(&stream_event
->event_fields
->p
);
914 if (stream_event
->event_context
)
915 definition_unref(&stream_event
->event_context
->p
);
920 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
922 struct ctf_stream_class
*stream_class
;
926 if (stream
->stream_definitions_created
)
929 stream_class
= stream
->stream_class
;
931 if (stream_class
->packet_context_decl
) {
932 struct definition
*definition
=
933 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
934 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
939 stream
->stream_packet_context
= container_of(definition
,
940 struct definition_struct
, p
);
941 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
943 if (stream_class
->event_header_decl
) {
944 struct definition
*definition
=
945 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
946 stream
->parent_def_scope
, 0, 0, "stream.event.header");
951 stream
->stream_event_header
=
952 container_of(definition
, struct definition_struct
, p
);
953 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
955 if (stream_class
->event_context_decl
) {
956 struct definition
*definition
=
957 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
958 stream
->parent_def_scope
, 0, 0, "stream.event.context");
963 stream
->stream_event_context
=
964 container_of(definition
, struct definition_struct
, p
);
965 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
967 stream
->events_by_id
= g_ptr_array_new();
968 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
969 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
970 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
971 struct ctf_stream_event
*stream_event
;
975 stream_event
= create_event_definitions(td
, stream
, event
);
978 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
983 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
984 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
986 g_free(stream_event
);
988 g_ptr_array_free(stream
->events_by_id
, TRUE
);
990 if (stream
->stream_event_context
)
991 definition_unref(&stream
->stream_event_context
->p
);
992 if (stream
->stream_event_header
)
993 definition_unref(&stream
->stream_event_header
->p
);
994 if (stream
->stream_packet_context
)
995 definition_unref(&stream
->stream_packet_context
->p
);
1001 int create_stream_packet_index(struct ctf_trace
*td
,
1002 struct ctf_file_stream
*file_stream
)
1004 struct ctf_stream_class
*stream
;
1006 struct ctf_stream_pos
*pos
;
1007 struct stat filestats
;
1008 struct packet_index packet_index
;
1009 int first_packet
= 1;
1012 pos
= &file_stream
->pos
;
1014 ret
= fstat(pos
->fd
, &filestats
);
1018 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1021 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1022 uint64_t stream_id
= 0;
1025 /* unmap old base */
1026 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1028 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1034 /* map new base. Need mapping length from header. */
1035 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1036 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1037 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1038 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1039 pos
->offset
= 0; /* Position of the packet header */
1041 packet_index
.offset
= pos
->mmap_offset
;
1042 packet_index
.content_size
= 0;
1043 packet_index
.packet_size
= 0;
1044 packet_index
.timestamp_begin
= 0;
1045 packet_index
.timestamp_end
= 0;
1046 packet_index
.events_discarded
= 0;
1048 /* read and check header, set stream id (and check) */
1049 if (file_stream
->parent
.trace_packet_header
) {
1050 /* Read packet header */
1051 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1054 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1055 if (len_index
>= 0) {
1056 struct definition
*field
;
1059 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1060 magic
= get_unsigned_int(field
);
1061 if (magic
!= CTF_MAGIC
) {
1062 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1064 file_stream
->pos
.packet_index
->len
,
1065 (ssize_t
) pos
->mmap_offset
);
1071 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1072 if (len_index
>= 0) {
1073 struct definition_array
*defarray
;
1074 struct definition
*field
;
1076 uint8_t uuidval
[UUID_LEN
];
1078 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1079 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1080 defarray
= container_of(field
, struct definition_array
, p
);
1081 assert(array_len(defarray
) == UUID_LEN
);
1083 for (i
= 0; i
< UUID_LEN
; i
++) {
1084 struct definition
*elem
;
1086 elem
= array_index(defarray
, i
);
1087 uuidval
[i
] = get_unsigned_int(elem
);
1089 ret
= uuid_compare(td
->uuid
, uuidval
);
1091 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1097 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1098 if (len_index
>= 0) {
1099 struct definition
*field
;
1101 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1102 stream_id
= get_unsigned_int(field
);
1106 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1107 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1111 file_stream
->parent
.stream_id
= stream_id
;
1112 if (stream_id
>= td
->streams
->len
) {
1113 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1116 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1118 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1121 file_stream
->parent
.stream_class
= stream
;
1122 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1128 if (file_stream
->parent
.stream_packet_context
) {
1129 /* Read packet context */
1130 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1133 /* read content size from header */
1134 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1135 if (len_index
>= 0) {
1136 struct definition
*field
;
1138 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1139 packet_index
.content_size
= get_unsigned_int(field
);
1141 /* Use file size for packet size */
1142 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1145 /* read packet size from header */
1146 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1147 if (len_index
>= 0) {
1148 struct definition
*field
;
1150 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1151 packet_index
.packet_size
= get_unsigned_int(field
);
1153 /* Use content size if non-zero, else file size */
1154 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1157 /* read timestamp begin from header */
1158 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1159 if (len_index
>= 0) {
1160 struct definition
*field
;
1162 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1163 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1166 /* read timestamp end from header */
1167 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1168 if (len_index
>= 0) {
1169 struct definition
*field
;
1171 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1172 packet_index
.timestamp_end
= get_unsigned_int(field
);
1175 /* read events discarded from header */
1176 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1177 if (len_index
>= 0) {
1178 struct definition
*field
;
1180 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1181 packet_index
.events_discarded
= get_unsigned_int(field
);
1184 /* Use file size for packet size */
1185 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1186 /* Use content size if non-zero, else file size */
1187 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1190 /* Validate content size and packet size values */
1191 if (packet_index
.content_size
> packet_index
.packet_size
) {
1192 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1193 packet_index
.content_size
, packet_index
.packet_size
);
1197 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1198 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1199 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1203 /* Save position after header and context */
1204 packet_index
.data_offset
= pos
->offset
;
1206 /* add index to packet array */
1207 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1209 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1212 /* Move pos back to beginning of file */
1213 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1219 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1223 if (td
->packet_header_decl
) {
1224 struct definition
*definition
=
1225 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1226 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1231 stream
->trace_packet_header
=
1232 container_of(definition
, struct definition_struct
, p
);
1233 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1243 * Note: many file streams can inherit from the same stream class
1244 * description (metadata).
1247 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1248 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1252 struct ctf_file_stream
*file_stream
;
1254 ret
= openat(td
->dirfd
, path
, flags
);
1256 perror("File stream openat()");
1259 file_stream
= g_new0(struct ctf_file_stream
, 1);
1262 file_stream
->pos
.packet_seek
= packet_seek
;
1264 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1269 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1270 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1274 * For now, only a single slock is supported.
1276 file_stream
->parent
.current_clock
= td
->single_clock
;
1277 ret
= create_stream_packet_index(td
, file_stream
);
1280 /* Add stream file to stream class */
1281 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1282 &file_stream
->parent
);
1286 if (file_stream
->parent
.trace_packet_header
)
1287 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1289 ctf_fini_pos(&file_stream
->pos
);
1290 close(file_stream
->pos
.fd
);
1291 g_free(file_stream
);
1297 int ctf_open_trace_read(struct ctf_trace
*td
,
1298 const char *path
, int flags
,
1299 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1300 int whence
), FILE *metadata_fp
)
1303 struct dirent
*dirent
;
1304 struct dirent
*diriter
;
1309 /* Open trace directory */
1310 td
->dir
= opendir(path
);
1312 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1317 td
->dirfd
= open(path
, 0);
1318 if (td
->dirfd
< 0) {
1319 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1320 perror("Trace directory open");
1324 strncpy(td
->path
, path
, sizeof(td
->path
));
1325 td
->path
[sizeof(td
->path
) - 1] = '\0';
1328 * Keep the metadata file separate.
1331 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1333 goto error_metadata
;
1337 * Open each stream: for each file, try to open, check magic
1338 * number, and get the stream ID to add to the right location in
1342 dirent_len
= offsetof(struct dirent
, d_name
) +
1343 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1345 dirent
= malloc(dirent_len
);
1348 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1350 fprintf(stderr
, "[error] Readdir error.\n");
1355 /* Ignore hidden files, ., .. and metadata. */
1356 if (!strncmp(diriter
->d_name
, ".", 1)
1357 || !strcmp(diriter
->d_name
, "..")
1358 || !strcmp(diriter
->d_name
, "metadata"))
1360 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1361 flags
, packet_seek
);
1363 fprintf(stderr
, "[error] Open file stream error.\n");
1382 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1383 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1384 int whence
), FILE *metadata_fp
)
1386 struct ctf_trace
*td
;
1390 * If packet_seek is NULL, we provide our default version.
1393 packet_seek
= ctf_packet_seek
;
1395 td
= g_new0(struct ctf_trace
, 1);
1397 switch (flags
& O_ACCMODE
) {
1400 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1403 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1408 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1411 fprintf(stderr
, "[error] Incorrect open flags.\n");
1422 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1423 struct mmap_stream
*mmap_info
)
1425 pos
->mmap_offset
= 0;
1426 pos
->packet_size
= 0;
1427 pos
->content_size
= 0;
1428 pos
->content_size_loc
= NULL
;
1429 pos
->fd
= mmap_info
->fd
;
1434 pos
->packet_index
= NULL
;
1435 pos
->prot
= PROT_READ
;
1436 pos
->flags
= MAP_PRIVATE
;
1437 pos
->parent
.rw_table
= read_dispatch_table
;
1438 pos
->parent
.event_cb
= ctf_read_event
;
1442 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1443 struct ctf_file_stream
*file_stream
)
1445 struct ctf_stream_class
*stream
;
1446 uint64_t stream_id
= 0;
1449 file_stream
->parent
.stream_id
= stream_id
;
1450 if (stream_id
>= td
->streams
->len
) {
1451 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1452 "in metadata.\n", stream_id
);
1456 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1458 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1459 "in metadata.\n", stream_id
);
1463 file_stream
->parent
.stream_class
= stream
;
1464 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1470 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1471 struct mmap_stream
*mmap_info
,
1472 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1476 struct ctf_file_stream
*file_stream
;
1478 file_stream
= g_new0(struct ctf_file_stream
, 1);
1479 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1481 file_stream
->pos
.packet_seek
= packet_seek
;
1483 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1488 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1492 /* Add stream file to stream class */
1493 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1494 &file_stream
->parent
);
1498 if (file_stream
->parent
.trace_packet_header
)
1499 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1501 g_free(file_stream
);
1505 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1506 struct mmap_stream_list
*mmap_list
,
1507 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1512 struct mmap_stream
*mmap_info
;
1514 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1520 * for each stream, try to open, check magic number, and get the
1521 * stream ID to add to the right location in the stream array.
1523 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1524 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1526 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1538 struct trace_descriptor
*ctf_open_mmap_trace(
1539 struct mmap_stream_list
*mmap_list
,
1540 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1544 struct ctf_trace
*td
;
1548 fprintf(stderr
, "[error] No metadata file pointer associated, "
1549 "required for mmap parsing\n");
1553 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1556 td
= g_new0(struct ctf_trace
, 1);
1557 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1570 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1572 ctf_fini_pos(&file_stream
->pos
);
1573 close(file_stream
->pos
.fd
);
1577 void ctf_close_trace(struct trace_descriptor
*tdp
)
1579 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1583 for (i
= 0; i
< td
->streams
->len
; i
++) {
1584 struct ctf_stream_class
*stream
;
1587 stream
= g_ptr_array_index(td
->streams
, i
);
1590 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1591 struct ctf_file_stream
*file_stream
;
1592 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1593 ctf_close_file_stream(file_stream
);
1597 g_ptr_array_free(td
->streams
, TRUE
);
1603 void __attribute__((constructor
)) ctf_init(void)
1607 ctf_format
.name
= g_quark_from_static_string("ctf");
1608 ret
= bt_register_format(&ctf_format
);
1612 /* TODO: finalize */