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 *collection_path
, 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 void ctf_update_timestamp(struct ctf_stream
*stream
,
112 struct definition_integer
*integer_definition
)
114 struct declaration_integer
*integer_declaration
=
115 integer_definition
->declaration
;
116 uint64_t oldval
, newval
, updateval
;
118 if (unlikely(integer_declaration
->len
== 64)) {
119 stream
->timestamp
= integer_definition
->value
._unsigned
;
123 oldval
= stream
->timestamp
;
124 oldval
&= (1ULL << integer_declaration
->len
) - 1;
125 newval
= integer_definition
->value
._unsigned
;
126 /* Test for overflow by comparing low bits */
128 newval
+= 1ULL << integer_declaration
->len
;
129 /* updateval contains old high bits, and new low bits (sum) */
130 updateval
= stream
->timestamp
;
131 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
133 stream
->prev_timestamp
= stream
->timestamp
;
134 stream
->timestamp
= updateval
;
137 void ctf_print_timestamp(FILE *fp
,
138 struct ctf_stream
*stream
,
141 uint64_t ts_sec
= 0, ts_nsec
;
142 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
143 struct trace_collection
*tc
= trace
->collection
;
144 struct ctf_clock
*clock
= tc
->single_clock
;
149 if (!opt_clock_raw
&& clock
) {
150 ts_sec
+= clock
->offset_s
;
151 ts_nsec
+= clock
->offset
;
153 ts_sec
+= opt_clock_offset
;
155 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
156 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
158 if (!opt_clock_seconds
) {
160 time_t time_s
= (time_t) ts_sec
;
162 if (!opt_clock_gmt
) {
165 res
= localtime_r(&time_s
, &tm
);
167 fprintf(stderr
, "[warning] Unable to get localtime.\n");
173 res
= gmtime_r(&time_s
, &tm
);
175 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
179 if (opt_clock_date
) {
183 /* Print date and time */
184 res
= strftime(timestr
, sizeof(timestr
),
187 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
190 fprintf(fp
, "%s", timestr
);
192 /* Print time in HH:MM:SS.ns */
193 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
194 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
198 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
206 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
208 struct ctf_stream_pos
*pos
=
209 container_of(ppos
, struct ctf_stream_pos
, parent
);
210 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
211 struct ctf_stream_event
*event
;
215 /* We need to check for EOF here for empty files. */
216 if (unlikely(pos
->offset
== EOF
))
219 ctf_pos_get_event(pos
);
222 * This is the EOF check after we've advanced the position in
225 if (unlikely(pos
->offset
== EOF
))
227 assert(pos
->offset
< pos
->content_size
);
229 /* Read event header */
230 if (likely(stream
->stream_event_header
)) {
231 struct definition_integer
*integer_definition
;
232 struct definition
*variant
;
234 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
237 /* lookup event id */
238 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
239 if (integer_definition
) {
240 id
= integer_definition
->value
._unsigned
;
242 struct definition_enum
*enum_definition
;
244 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
245 if (enum_definition
) {
246 id
= enum_definition
->integer
->value
._unsigned
;
250 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
252 integer_definition
= lookup_integer(variant
, "id", FALSE
);
253 if (integer_definition
) {
254 id
= integer_definition
->value
._unsigned
;
257 stream
->event_id
= id
;
259 /* lookup timestamp */
260 stream
->has_timestamp
= 0;
261 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
262 if (integer_definition
) {
263 ctf_update_timestamp(stream
, integer_definition
);
264 stream
->has_timestamp
= 1;
267 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
268 if (integer_definition
) {
269 ctf_update_timestamp(stream
, integer_definition
);
270 stream
->has_timestamp
= 1;
276 /* Read stream-declared event context */
277 if (stream
->stream_event_context
) {
278 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
283 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
284 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
287 event
= g_ptr_array_index(stream
->events_by_id
, id
);
288 if (unlikely(!event
)) {
289 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
293 /* Read event-declared event context */
294 if (event
->event_context
) {
295 ret
= generic_rw(ppos
, &event
->event_context
->p
);
300 /* Read event payload */
301 if (likely(event
->event_fields
)) {
302 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
310 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
315 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
317 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
318 struct ctf_stream_event
*event
;
322 id
= stream
->event_id
;
324 /* print event header */
325 if (likely(stream
->stream_event_header
)) {
326 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
331 /* print stream-declared event context */
332 if (stream
->stream_event_context
) {
333 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
338 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
339 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
342 event
= g_ptr_array_index(stream
->events_by_id
, id
);
343 if (unlikely(!event
)) {
344 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
348 /* print event-declared event context */
349 if (event
->event_context
) {
350 ret
= generic_rw(pos
, &event
->event_context
->p
);
355 /* Read and print event payload */
356 if (likely(event
->event_fields
)) {
357 ret
= generic_rw(pos
, &event
->event_fields
->p
);
365 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
369 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
372 pos
->mmap_offset
= 0;
373 pos
->packet_size
= 0;
374 pos
->content_size
= 0;
375 pos
->content_size_loc
= NULL
;
381 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
382 sizeof(struct packet_index
));
384 pos
->packet_index
= NULL
;
385 switch (open_flags
& O_ACCMODE
) {
387 pos
->prot
= PROT_READ
;
388 pos
->flags
= MAP_PRIVATE
;
389 pos
->parent
.rw_table
= read_dispatch_table
;
390 pos
->parent
.event_cb
= ctf_read_event
;
393 pos
->prot
= PROT_WRITE
; /* Write has priority */
394 pos
->flags
= MAP_SHARED
;
395 pos
->parent
.rw_table
= write_dispatch_table
;
396 pos
->parent
.event_cb
= ctf_write_event
;
398 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
405 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
409 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
410 *pos
->content_size_loc
= pos
->offset
;
413 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
415 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
420 (void) g_array_free(pos
->packet_index
, TRUE
);
423 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
425 struct ctf_file_stream
*file_stream
=
426 container_of(pos
, struct ctf_file_stream
, pos
);
429 struct packet_index
*index
;
431 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
432 *pos
->content_size_loc
= pos
->offset
;
436 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
438 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
446 * The caller should never ask for ctf_move_pos across packets,
447 * except to get exactly at the beginning of the next packet.
449 if (pos
->prot
== PROT_WRITE
) {
452 /* The writer will add padding */
453 assert(pos
->offset
+ offset
== pos
->packet_size
);
454 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
457 assert(offset
== 0); /* only seek supported for now */
463 pos
->content_size
= -1U; /* Unknown at this point */
464 pos
->packet_size
= WRITE_PACKET_LEN
;
465 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
466 pos
->packet_size
/ CHAR_BIT
);
474 uint32_t events_discarded_diff
;
476 if (pos
->offset
== EOF
) {
479 /* For printing discarded event count */
480 index
= &g_array_index(pos
->packet_index
,
481 struct packet_index
, pos
->cur_index
);
482 events_discarded_diff
= index
->events_discarded
;
483 file_stream
->parent
.prev_timestamp_end
=
484 index
->timestamp_end
;
485 if (pos
->cur_index
> 0) {
486 index
= &g_array_index(pos
->packet_index
,
489 events_discarded_diff
-= index
->events_discarded
;
491 file_stream
->parent
.events_discarded
= events_discarded_diff
;
492 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
493 /* The reader will expect us to skip padding */
494 assert(pos
->offset
+ offset
== pos
->content_size
);
499 assert(offset
== 0); /* only seek supported for now */
501 file_stream
->parent
.prev_timestamp
= 0;
502 file_stream
->parent
.prev_timestamp_end
= 0;
507 if (pos
->cur_index
>= pos
->packet_index
->len
) {
509 * When a stream reaches the end of the
510 * file, we need to show the number of
511 * events discarded ourselves, because
512 * there is no next event scheduled to
513 * be printed in the output.
515 if (file_stream
->parent
.events_discarded
) {
517 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
518 file_stream
->parent
.events_discarded
);
519 ctf_print_timestamp(stderr
, &file_stream
->parent
,
520 file_stream
->parent
.prev_timestamp
);
521 fprintf(stderr
, "] and [");
522 ctf_print_timestamp(stderr
, &file_stream
->parent
,
523 file_stream
->parent
.prev_timestamp_end
);
524 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
526 file_stream
->parent
.events_discarded
= 0;
531 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
533 pos
->mmap_offset
= index
->offset
;
535 /* Lookup context/packet size in index */
536 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
537 pos
->content_size
= index
->content_size
;
538 pos
->packet_size
= index
->packet_size
;
539 if (index
->data_offset
< index
->content_size
) {
540 pos
->offset
= 0; /* will read headers */
541 } else if (index
->data_offset
== index
->content_size
) {
543 pos
->offset
= index
->data_offset
;
546 goto read_next_packet
;
552 /* map new base. Need mapping length from header. */
553 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
554 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
555 if (pos
->base
== MAP_FAILED
) {
556 fprintf(stderr
, "[error] mmap error %s.\n",
561 /* update trace_packet_header and stream_packet_context */
562 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
563 /* Read packet header */
564 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
567 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
568 /* Read packet context */
569 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
575 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
581 len
= fread(&magic
, sizeof(magic
), 1, fp
);
585 if (magic
== TSDL_MAGIC
) {
587 td
->byte_order
= BYTE_ORDER
;
588 CTF_TRACE_SET_FIELD(td
, byte_order
);
589 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
591 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
592 LITTLE_ENDIAN
: BIG_ENDIAN
;
593 CTF_TRACE_SET_FIELD(td
, byte_order
);
601 * Returns 0 on success, -1 on error.
604 int check_version(unsigned int major
, unsigned int minor
)
619 /* eventually return an error instead of warning */
621 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
627 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
630 struct metadata_packet_header header
;
631 size_t readlen
, writelen
, toread
;
632 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
635 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
639 if (td
->byte_order
!= BYTE_ORDER
) {
640 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
641 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
642 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
643 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
646 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
647 if (header
.compression_scheme
) {
648 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
649 header
.compression_scheme
);
652 if (header
.encryption_scheme
) {
653 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
654 header
.encryption_scheme
);
657 if (header
.checksum_scheme
) {
658 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
659 header
.checksum_scheme
);
662 if (check_version(header
.major
, header
.minor
) < 0)
664 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
665 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
666 CTF_TRACE_SET_FIELD(td
, uuid
);
668 if (uuid_compare(header
.uuid
, td
->uuid
))
672 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
675 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
680 if (babeltrace_debug
) {
682 fprintf(stderr
, "[debug] metadata packet read: %s\n",
686 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
687 if (writelen
< readlen
) {
697 ret
= 0; /* continue reading next packet */
704 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
705 ret
= fseek(in
, toread
, SEEK_CUR
);
707 fprintf(stderr
, "[warning] Missing padding at end of file\n");
714 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
723 * Using strlen on *buf instead of size of open_memstream
724 * because its size includes garbage at the end (after final
725 * \0). This is the allocated size, not the actual string size.
727 out
= open_memstream(buf
, &size
);
729 perror("Metadata open_memstream");
733 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
742 fclose(out
); /* flush the buffer */
744 /* open for reading */
745 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
747 perror("Metadata fmemopen");
754 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
755 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
756 int whence
), FILE *metadata_fp
)
758 struct ctf_scanner
*scanner
;
759 struct ctf_file_stream
*metadata_stream
;
764 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
767 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
769 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
777 td
->metadata
= &metadata_stream
->parent
;
778 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
779 if (metadata_stream
->pos
.fd
< 0) {
780 fprintf(stderr
, "Unable to open metadata.\n");
781 return metadata_stream
->pos
.fd
;
784 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
786 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
787 perror("Metadata stream open");
792 if (babeltrace_debug
)
795 if (packet_metadata(td
, fp
)) {
796 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
798 goto end_packet_read
;
800 unsigned int major
, minor
;
803 td
->byte_order
= BYTE_ORDER
;
805 /* Check text-only metadata header and version */
806 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
808 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
809 if (check_version(major
, minor
) < 0) {
811 goto end_packet_read
;
816 scanner
= ctf_scanner_alloc(fp
);
818 fprintf(stderr
, "[error] Error allocating scanner\n");
820 goto end_scanner_alloc
;
822 ret
= ctf_scanner_append_ast(scanner
);
824 fprintf(stderr
, "[error] Error creating AST\n");
828 if (babeltrace_debug
) {
829 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
831 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
836 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
838 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
841 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
844 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
848 ctf_scanner_free(scanner
);
854 close(metadata_stream
->pos
.fd
);
856 g_free(metadata_stream
);
861 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
862 struct ctf_stream
*stream
,
863 struct ctf_event
*event
)
865 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
867 if (event
->context_decl
) {
868 struct definition
*definition
=
869 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
870 stream
->parent_def_scope
, 0, 0, "event.context");
874 stream_event
->event_context
= container_of(definition
,
875 struct definition_struct
, p
);
876 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
878 if (event
->fields_decl
) {
879 struct definition
*definition
=
880 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
881 stream
->parent_def_scope
, 0, 0, "event.fields");
885 stream_event
->event_fields
= container_of(definition
,
886 struct definition_struct
, p
);
887 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
892 if (stream_event
->event_fields
)
893 definition_unref(&stream_event
->event_fields
->p
);
894 if (stream_event
->event_context
)
895 definition_unref(&stream_event
->event_context
->p
);
900 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
902 struct ctf_stream_class
*stream_class
;
906 if (stream
->stream_definitions_created
)
909 stream_class
= stream
->stream_class
;
911 if (stream_class
->packet_context_decl
) {
912 struct definition
*definition
=
913 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
914 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
919 stream
->stream_packet_context
= container_of(definition
,
920 struct definition_struct
, p
);
921 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
923 if (stream_class
->event_header_decl
) {
924 struct definition
*definition
=
925 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
926 stream
->parent_def_scope
, 0, 0, "stream.event.header");
931 stream
->stream_event_header
=
932 container_of(definition
, struct definition_struct
, p
);
933 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
935 if (stream_class
->event_context_decl
) {
936 struct definition
*definition
=
937 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
938 stream
->parent_def_scope
, 0, 0, "stream.event.context");
943 stream
->stream_event_context
=
944 container_of(definition
, struct definition_struct
, p
);
945 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
947 stream
->events_by_id
= g_ptr_array_new();
948 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
949 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
950 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
951 struct ctf_stream_event
*stream_event
;
955 stream_event
= create_event_definitions(td
, stream
, event
);
958 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
963 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
964 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
966 g_free(stream_event
);
968 g_ptr_array_free(stream
->events_by_id
, TRUE
);
970 if (stream
->stream_event_context
)
971 definition_unref(&stream
->stream_event_context
->p
);
972 if (stream
->stream_event_header
)
973 definition_unref(&stream
->stream_event_header
->p
);
974 if (stream
->stream_packet_context
)
975 definition_unref(&stream
->stream_packet_context
->p
);
981 int create_stream_packet_index(struct ctf_trace
*td
,
982 struct ctf_file_stream
*file_stream
)
984 struct ctf_stream_class
*stream
;
986 struct ctf_stream_pos
*pos
;
987 struct stat filestats
;
988 struct packet_index packet_index
;
989 int first_packet
= 1;
992 pos
= &file_stream
->pos
;
994 ret
= fstat(pos
->fd
, &filestats
);
998 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1001 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1002 uint64_t stream_id
= 0;
1005 /* unmap old base */
1006 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1008 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1014 /* map new base. Need mapping length from header. */
1015 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1016 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1017 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1018 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1019 pos
->offset
= 0; /* Position of the packet header */
1021 packet_index
.offset
= pos
->mmap_offset
;
1022 packet_index
.content_size
= 0;
1023 packet_index
.packet_size
= 0;
1024 packet_index
.timestamp_begin
= 0;
1025 packet_index
.timestamp_end
= 0;
1026 packet_index
.events_discarded
= 0;
1028 /* read and check header, set stream id (and check) */
1029 if (file_stream
->parent
.trace_packet_header
) {
1030 /* Read packet header */
1031 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1034 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1035 if (len_index
>= 0) {
1036 struct definition
*field
;
1039 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1040 magic
= get_unsigned_int(field
);
1041 if (magic
!= CTF_MAGIC
) {
1042 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1044 file_stream
->pos
.packet_index
->len
,
1045 (ssize_t
) pos
->mmap_offset
);
1051 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1052 if (len_index
>= 0) {
1053 struct definition_array
*defarray
;
1054 struct definition
*field
;
1056 uint8_t uuidval
[UUID_LEN
];
1058 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1059 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1060 defarray
= container_of(field
, struct definition_array
, p
);
1061 assert(array_len(defarray
) == UUID_LEN
);
1063 for (i
= 0; i
< UUID_LEN
; i
++) {
1064 struct definition
*elem
;
1066 elem
= array_index(defarray
, i
);
1067 uuidval
[i
] = get_unsigned_int(elem
);
1069 ret
= uuid_compare(td
->uuid
, uuidval
);
1071 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1077 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1078 if (len_index
>= 0) {
1079 struct definition
*field
;
1081 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1082 stream_id
= get_unsigned_int(field
);
1086 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1087 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1091 file_stream
->parent
.stream_id
= stream_id
;
1092 if (stream_id
>= td
->streams
->len
) {
1093 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1096 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1098 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1101 file_stream
->parent
.stream_class
= stream
;
1102 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1108 if (file_stream
->parent
.stream_packet_context
) {
1109 /* Read packet context */
1110 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1113 /* read content size from header */
1114 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1115 if (len_index
>= 0) {
1116 struct definition
*field
;
1118 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1119 packet_index
.content_size
= get_unsigned_int(field
);
1121 /* Use file size for packet size */
1122 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1125 /* read packet size from header */
1126 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1127 if (len_index
>= 0) {
1128 struct definition
*field
;
1130 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1131 packet_index
.packet_size
= get_unsigned_int(field
);
1133 /* Use content size if non-zero, else file size */
1134 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1137 /* read timestamp begin from header */
1138 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1139 if (len_index
>= 0) {
1140 struct definition
*field
;
1142 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1143 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1146 /* read timestamp end from header */
1147 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1148 if (len_index
>= 0) {
1149 struct definition
*field
;
1151 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1152 packet_index
.timestamp_end
= get_unsigned_int(field
);
1155 /* read events discarded from header */
1156 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
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
.events_discarded
= get_unsigned_int(field
);
1164 /* Use file size for packet size */
1165 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1166 /* Use content size if non-zero, else file size */
1167 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1170 /* Validate content size and packet size values */
1171 if (packet_index
.content_size
> packet_index
.packet_size
) {
1172 fprintf(stderr
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1173 packet_index
.content_size
, packet_index
.packet_size
);
1177 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1178 fprintf(stderr
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1179 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1183 /* Save position after header and context */
1184 packet_index
.data_offset
= pos
->offset
;
1186 /* add index to packet array */
1187 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1189 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1192 /* Move pos back to beginning of file */
1193 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1199 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1203 if (td
->packet_header_decl
) {
1204 struct definition
*definition
=
1205 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1206 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1211 stream
->trace_packet_header
=
1212 container_of(definition
, struct definition_struct
, p
);
1213 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1223 * Note: many file streams can inherit from the same stream class
1224 * description (metadata).
1227 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1228 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1232 struct ctf_file_stream
*file_stream
;
1234 ret
= openat(td
->dirfd
, path
, flags
);
1236 perror("File stream openat()");
1239 file_stream
= g_new0(struct ctf_file_stream
, 1);
1241 if (move_pos_slow
) {
1242 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1244 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1249 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1250 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1253 ret
= create_stream_packet_index(td
, file_stream
);
1256 /* Add stream file to stream class */
1257 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1258 &file_stream
->parent
);
1262 if (file_stream
->parent
.trace_packet_header
)
1263 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1265 ctf_fini_pos(&file_stream
->pos
);
1266 close(file_stream
->pos
.fd
);
1267 g_free(file_stream
);
1273 init_domain_name(struct ctf_trace
*td
)
1277 start
= td
->path
+ strlen(td
->collection_path
);
1278 while (start
[0] == '/')
1279 start
++; /* skip / */
1280 end
= strchr(start
, '/');
1282 end
= start
+ strlen(start
);
1283 memcpy(td
->domain
, start
, end
- start
);
1284 td
->domain
[end
- start
] = '\0';
1288 init_proc_name(struct ctf_trace
*td
)
1293 if (td
->domain
[0] == '\0')
1295 memcpy(buf
, td
->path
, PATH_MAX
);
1296 start
= buf
+ strlen(td
->collection_path
);
1297 while (start
[0] == '/')
1298 start
++; /* skip / */
1299 start
= strchr(start
, '/'); /* get begin of domain content */
1302 while (start
[0] == '/')
1303 start
++; /* skip / */
1304 /* find last -, skips time */
1305 end
= strrchr(start
, '-');
1309 /* find previous -, skips date */
1310 end
= strrchr(start
, '-');
1314 /* find previous -, skips pid */
1315 end
= strrchr(start
, '-');
1320 memcpy(td
->procname
, start
, end
- start
);
1321 td
->procname
[end
- start
] = '\0';
1325 init_vpid(struct ctf_trace
*td
)
1330 if (td
->domain
[0] == '\0')
1332 memcpy(buf
, td
->path
, PATH_MAX
);
1333 start
= buf
+ strlen(td
->collection_path
);
1334 while (start
[0] == '/')
1335 start
++; /* skip / */
1336 start
= strchr(start
, '/'); /* get begin of domain content */
1339 while (start
[0] == '/')
1340 start
++; /* skip / */
1341 /* find last -, skips time */
1342 end
= strrchr(start
, '-');
1346 /* find previous -, skips date */
1347 end
= strrchr(start
, '-');
1351 /* find previous -, skips pid */
1352 start
= strrchr(start
, '-');
1355 start
++; /* skip - */
1357 memcpy(td
->vpid
, start
, end
- start
);
1358 td
->vpid
[end
- start
] = '\0';
1362 int ctf_open_trace_read(struct ctf_trace
*td
, const char *collection_path
,
1363 const char *path
, int flags
,
1364 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1365 int whence
), FILE *metadata_fp
)
1368 struct dirent
*dirent
;
1369 struct dirent
*diriter
;
1371 char *respath
, *rescolpath
;
1375 /* Open trace directory */
1376 td
->dir
= opendir(path
);
1378 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1383 td
->dirfd
= open(path
, 0);
1384 if (td
->dirfd
< 0) {
1385 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1386 perror("Trace directory open");
1390 rescolpath
= realpath(collection_path
, td
->collection_path
);
1392 fprintf(stderr
, "[error] collection path resolution failure\n");
1395 respath
= realpath(path
, td
->path
);
1397 fprintf(stderr
, "[error] path resolution failure\n");
1400 init_domain_name(td
);
1405 * Keep the metadata file separate.
1408 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1410 goto error_metadata
;
1414 * Open each stream: for each file, try to open, check magic
1415 * number, and get the stream ID to add to the right location in
1419 dirent_len
= offsetof(struct dirent
, d_name
) +
1420 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1422 dirent
= malloc(dirent_len
);
1425 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1427 fprintf(stderr
, "[error] Readdir error.\n");
1432 /* Ignore hidden files, ., .. and metadata. */
1433 if (!strncmp(diriter
->d_name
, ".", 1)
1434 || !strcmp(diriter
->d_name
, "..")
1435 || !strcmp(diriter
->d_name
, "metadata"))
1437 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1439 fprintf(stderr
, "[error] Open file stream error.\n");
1458 struct trace_descriptor
*ctf_open_trace(const char *collection_path
, const char *path
, int flags
,
1459 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1460 int whence
), FILE *metadata_fp
)
1462 struct ctf_trace
*td
;
1465 td
= g_new0(struct ctf_trace
, 1);
1467 switch (flags
& O_ACCMODE
) {
1470 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1473 ret
= ctf_open_trace_read(td
, collection_path
, path
, flags
, move_pos_slow
, metadata_fp
);
1478 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1481 fprintf(stderr
, "[error] Incorrect open flags.\n");
1492 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1493 struct mmap_stream
*mmap_info
)
1495 pos
->mmap_offset
= 0;
1496 pos
->packet_size
= 0;
1497 pos
->content_size
= 0;
1498 pos
->content_size_loc
= NULL
;
1499 pos
->fd
= mmap_info
->fd
;
1504 pos
->packet_index
= NULL
;
1505 pos
->prot
= PROT_READ
;
1506 pos
->flags
= MAP_PRIVATE
;
1507 pos
->parent
.rw_table
= read_dispatch_table
;
1508 pos
->parent
.event_cb
= ctf_read_event
;
1512 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1513 struct ctf_file_stream
*file_stream
)
1515 struct ctf_stream_class
*stream
;
1516 uint64_t stream_id
= 0;
1519 file_stream
->parent
.stream_id
= stream_id
;
1520 if (stream_id
>= td
->streams
->len
) {
1521 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1522 "in metadata.\n", stream_id
);
1526 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1528 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1529 "in metadata.\n", stream_id
);
1533 file_stream
->parent
.stream_class
= stream
;
1534 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1540 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1541 struct mmap_stream
*mmap_info
,
1542 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1546 struct ctf_file_stream
*file_stream
;
1548 file_stream
= g_new0(struct ctf_file_stream
, 1);
1549 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1551 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1553 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1558 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1562 /* Add stream file to stream class */
1563 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1564 &file_stream
->parent
);
1568 if (file_stream
->parent
.trace_packet_header
)
1569 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1571 g_free(file_stream
);
1575 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1576 struct mmap_stream_list
*mmap_list
,
1577 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1582 struct mmap_stream
*mmap_info
;
1584 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1590 * for each stream, try to open, check magic number, and get the
1591 * stream ID to add to the right location in the stream array.
1593 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1594 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1596 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1608 struct trace_descriptor
*ctf_open_mmap_trace(
1609 struct mmap_stream_list
*mmap_list
,
1610 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1613 struct ctf_trace
*td
;
1617 fprintf(stderr
, "[error] No metadata file pointer associated, "
1618 "required for mmap parsing\n");
1621 if (!move_pos_slow
) {
1622 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1625 td
= g_new0(struct ctf_trace
, 1);
1626 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1639 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1641 ctf_fini_pos(&file_stream
->pos
);
1642 close(file_stream
->pos
.fd
);
1646 void ctf_close_trace(struct trace_descriptor
*tdp
)
1648 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1652 for (i
= 0; i
< td
->streams
->len
; i
++) {
1653 struct ctf_stream_class
*stream
;
1656 stream
= g_ptr_array_index(td
->streams
, i
);
1659 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1660 struct ctf_file_stream
*file_stream
;
1661 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1662 ctf_close_file_stream(file_stream
);
1666 g_ptr_array_free(td
->streams
, TRUE
);
1672 void __attribute__((constructor
)) ctf_init(void)
1676 ctf_format
.name
= g_quark_from_static_string("ctf");
1677 ret
= bt_register_format(&ctf_format
);
1681 /* TODO: finalize */