2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/context-internal.h>
28 #include <babeltrace/uuid.h>
29 #include <babeltrace/endian.h>
34 #include <sys/types.h>
42 #include "metadata/ctf-scanner.h"
43 #include "metadata/ctf-parser.h"
44 #include "metadata/ctf-ast.h"
45 #include "events-private.h"
46 #include "memstream.h"
49 * We currently simply map a page to read the packet header and packet
50 * context to get the packet length and content length. (in bits)
52 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
53 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
56 #define min(a, b) (((a) < (b)) ? (a) : (b))
59 #define NSEC_PER_SEC 1000000000ULL
66 uint64_t opt_clock_offset
;
71 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
72 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
76 struct trace_descriptor
*ctf_open_mmap_trace(
77 struct mmap_stream_list
*mmap_list
,
78 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
82 void ctf_set_context(struct trace_descriptor
*descriptor
,
83 struct bt_context
*ctx
);
85 void ctf_set_handle(struct trace_descriptor
*descriptor
,
86 struct bt_trace_handle
*handle
);
89 void ctf_close_trace(struct trace_descriptor
*descriptor
);
92 rw_dispatch read_dispatch_table
[] = {
93 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
94 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
95 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
96 [ CTF_TYPE_STRING
] = ctf_string_read
,
97 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
98 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
99 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
100 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
104 rw_dispatch write_dispatch_table
[] = {
105 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
106 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
107 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
108 [ CTF_TYPE_STRING
] = ctf_string_write
,
109 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
110 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
111 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
112 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
116 struct format ctf_format
= {
117 .open_trace
= ctf_open_trace
,
118 .open_mmap_trace
= ctf_open_mmap_trace
,
119 .close_trace
= ctf_close_trace
,
120 .set_context
= ctf_set_context
,
121 .set_handle
= ctf_set_handle
,
125 * Update stream current timestamp, keep at clock frequency.
128 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
129 struct definition_integer
*integer_definition
)
131 struct declaration_integer
*integer_declaration
=
132 integer_definition
->declaration
;
133 uint64_t oldval
, newval
, updateval
;
135 if (unlikely(integer_declaration
->len
== 64)) {
136 stream
->timestamp
= integer_definition
->value
._unsigned
;
140 oldval
= stream
->timestamp
;
141 oldval
&= (1ULL << integer_declaration
->len
) - 1;
142 newval
= integer_definition
->value
._unsigned
;
143 /* Test for overflow by comparing low bits */
145 newval
+= 1ULL << integer_declaration
->len
;
146 /* updateval contains old high bits, and new low bits (sum) */
147 updateval
= stream
->timestamp
;
148 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
150 stream
->prev_timestamp
= stream
->timestamp
;
151 stream
->timestamp
= updateval
;
155 * Print timestamp, rescaling clock frequency to nanoseconds and
156 * applying offsets as needed (unix time).
158 void ctf_print_timestamp(FILE *fp
,
159 struct ctf_stream_definition
*stream
,
162 uint64_t ts_sec
= 0, ts_nsec
;
165 ts_nsec
= ctf_get_timestamp_raw(stream
, timestamp
);
167 ts_nsec
= ctf_get_timestamp(stream
, timestamp
);
170 /* Add command-line offset */
171 ts_sec
+= opt_clock_offset
;
173 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
174 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
176 if (!opt_clock_seconds
) {
178 time_t time_s
= (time_t) ts_sec
;
180 if (!opt_clock_gmt
) {
183 res
= localtime_r(&time_s
, &tm
);
185 fprintf(stderr
, "[warning] Unable to get localtime.\n");
191 res
= gmtime_r(&time_s
, &tm
);
193 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
197 if (opt_clock_date
) {
201 /* Print date and time */
202 res
= strftime(timestr
, sizeof(timestr
),
205 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
208 fprintf(fp
, "%s", timestr
);
210 /* Print time in HH:MM:SS.ns */
211 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
212 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
216 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
224 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
226 struct ctf_stream_pos
*pos
=
227 container_of(ppos
, struct ctf_stream_pos
, parent
);
228 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
229 struct ctf_event_definition
*event
;
233 /* We need to check for EOF here for empty files. */
234 if (unlikely(pos
->offset
== EOF
))
237 ctf_pos_get_event(pos
);
239 /* save the current position as a restore point */
240 pos
->last_offset
= pos
->offset
;
241 /* we just read the event, it is consumed when used by the caller */
242 stream
->consumed
= 0;
245 * This is the EOF check after we've advanced the position in
248 if (unlikely(pos
->offset
== EOF
))
250 assert(pos
->offset
< pos
->content_size
);
252 /* Read event header */
253 if (likely(stream
->stream_event_header
)) {
254 struct definition_integer
*integer_definition
;
255 struct definition
*variant
;
257 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
260 /* lookup event id */
261 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
262 if (integer_definition
) {
263 id
= integer_definition
->value
._unsigned
;
265 struct definition_enum
*enum_definition
;
267 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
268 if (enum_definition
) {
269 id
= enum_definition
->integer
->value
._unsigned
;
273 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
275 integer_definition
= lookup_integer(variant
, "id", FALSE
);
276 if (integer_definition
) {
277 id
= integer_definition
->value
._unsigned
;
280 stream
->event_id
= id
;
282 /* lookup timestamp */
283 stream
->has_timestamp
= 0;
284 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
285 if (integer_definition
) {
286 ctf_update_timestamp(stream
, integer_definition
);
287 stream
->has_timestamp
= 1;
290 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
291 if (integer_definition
) {
292 ctf_update_timestamp(stream
, integer_definition
);
293 stream
->has_timestamp
= 1;
299 /* Read stream-declared event context */
300 if (stream
->stream_event_context
) {
301 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
306 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
307 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
310 event
= g_ptr_array_index(stream
->events_by_id
, id
);
311 if (unlikely(!event
)) {
312 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
316 /* Read event-declared event context */
317 if (event
->event_context
) {
318 ret
= generic_rw(ppos
, &event
->event_context
->p
);
323 /* Read event payload */
324 if (likely(event
->event_fields
)) {
325 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
333 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
338 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream_definition
*stream
)
340 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
341 struct ctf_event_definition
*event
;
345 id
= stream
->event_id
;
347 /* print event header */
348 if (likely(stream
->stream_event_header
)) {
349 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
354 /* print stream-declared event context */
355 if (stream
->stream_event_context
) {
356 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
361 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
362 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
365 event
= g_ptr_array_index(stream
->events_by_id
, id
);
366 if (unlikely(!event
)) {
367 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
371 /* print event-declared event context */
372 if (event
->event_context
) {
373 ret
= generic_rw(pos
, &event
->event_context
->p
);
378 /* Read and print event payload */
379 if (likely(event
->event_fields
)) {
380 ret
= generic_rw(pos
, &event
->event_fields
->p
);
388 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
392 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
395 pos
->mmap_offset
= 0;
396 pos
->packet_size
= 0;
397 pos
->content_size
= 0;
398 pos
->content_size_loc
= NULL
;
404 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
405 sizeof(struct packet_index
));
407 pos
->packet_index
= NULL
;
408 switch (open_flags
& O_ACCMODE
) {
410 pos
->prot
= PROT_READ
;
411 pos
->flags
= MAP_PRIVATE
;
412 pos
->parent
.rw_table
= read_dispatch_table
;
413 pos
->parent
.event_cb
= ctf_read_event
;
416 pos
->prot
= PROT_WRITE
; /* Write has priority */
417 pos
->flags
= MAP_SHARED
;
418 pos
->parent
.rw_table
= write_dispatch_table
;
419 pos
->parent
.event_cb
= ctf_write_event
;
421 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
428 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
432 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
433 *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",
443 (void) g_array_free(pos
->packet_index
, TRUE
);
447 * for SEEK_CUR: go to next packet.
448 * for SEEK_POS: go to packet numer (index).
450 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
452 struct ctf_stream_pos
*pos
=
453 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
454 struct ctf_file_stream
*file_stream
=
455 container_of(pos
, struct ctf_file_stream
, pos
);
458 struct packet_index
*packet_index
;
460 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
461 *pos
->content_size_loc
= pos
->offset
;
465 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
467 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
475 * The caller should never ask for ctf_move_pos across packets,
476 * except to get exactly at the beginning of the next packet.
478 if (pos
->prot
== PROT_WRITE
) {
481 /* The writer will add padding */
482 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
485 assert(index
== 0); /* only seek supported for now */
491 pos
->content_size
= -1U; /* Unknown at this point */
492 pos
->packet_size
= WRITE_PACKET_LEN
;
493 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
494 pos
->packet_size
/ CHAR_BIT
);
502 uint32_t events_discarded_diff
;
504 if (pos
->offset
== EOF
) {
507 /* For printing discarded event count */
508 packet_index
= &g_array_index(pos
->packet_index
,
509 struct packet_index
, pos
->cur_index
);
510 events_discarded_diff
= packet_index
->events_discarded
;
511 file_stream
->parent
.prev_timestamp_end
=
512 packet_index
->timestamp_end
;
513 if (pos
->cur_index
> 0) {
514 packet_index
= &g_array_index(pos
->packet_index
,
517 events_discarded_diff
-= packet_index
->events_discarded
;
519 file_stream
->parent
.events_discarded
= events_discarded_diff
;
520 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
521 /* The reader will expect us to skip padding */
526 pos
->cur_index
= index
;
527 file_stream
->parent
.prev_timestamp
= 0;
528 file_stream
->parent
.prev_timestamp_end
= 0;
533 if (pos
->cur_index
>= pos
->packet_index
->len
) {
535 * When a stream reaches the end of the
536 * file, we need to show the number of
537 * events discarded ourselves, because
538 * there is no next event scheduled to
539 * be printed in the output.
541 if (file_stream
->parent
.events_discarded
) {
543 * We need to check if we are in trace
544 * read or called from packet indexing.
545 * In this last case, the collection is
546 * not there, so we cannot print the
549 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
551 fprintf(stderr
, "[warning] Tracer discarded %d events at end of stream between [",
552 file_stream
->parent
.events_discarded
);
553 ctf_print_timestamp(stderr
, &file_stream
->parent
,
554 file_stream
->parent
.prev_timestamp
);
555 fprintf(stderr
, "] and [");
556 ctf_print_timestamp(stderr
, &file_stream
->parent
,
557 file_stream
->parent
.prev_timestamp_end
);
558 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
561 file_stream
->parent
.events_discarded
= 0;
566 packet_index
= &g_array_index(pos
->packet_index
, struct packet_index
,
568 pos
->mmap_offset
= packet_index
->offset
;
570 /* Lookup context/packet size in index */
571 file_stream
->parent
.timestamp
= packet_index
->timestamp_begin
;
572 pos
->content_size
= packet_index
->content_size
;
573 pos
->packet_size
= packet_index
->packet_size
;
574 if (packet_index
->data_offset
< packet_index
->content_size
) {
575 pos
->offset
= 0; /* will read headers */
576 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
578 pos
->offset
= packet_index
->data_offset
;
580 goto read_next_packet
;
586 /* map new base. Need mapping length from header. */
587 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
588 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
589 if (pos
->base
== MAP_FAILED
) {
590 fprintf(stderr
, "[error] mmap error %s.\n",
595 /* update trace_packet_header and stream_packet_context */
596 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
597 /* Read packet header */
598 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
601 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
602 /* Read packet context */
603 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
609 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
615 len
= fread(&magic
, sizeof(magic
), 1, fp
);
619 if (magic
== TSDL_MAGIC
) {
621 td
->byte_order
= BYTE_ORDER
;
622 CTF_TRACE_SET_FIELD(td
, byte_order
);
623 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
625 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
626 LITTLE_ENDIAN
: BIG_ENDIAN
;
627 CTF_TRACE_SET_FIELD(td
, byte_order
);
635 * Returns 0 on success, -1 on error.
638 int check_version(unsigned int major
, unsigned int minor
)
653 /* eventually return an error instead of warning */
655 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
661 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
664 struct metadata_packet_header header
;
665 size_t readlen
, writelen
, toread
;
666 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
669 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
673 if (td
->byte_order
!= BYTE_ORDER
) {
674 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
675 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
676 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
677 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
680 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
681 if (header
.compression_scheme
) {
682 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
683 header
.compression_scheme
);
686 if (header
.encryption_scheme
) {
687 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
688 header
.encryption_scheme
);
691 if (header
.checksum_scheme
) {
692 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
693 header
.checksum_scheme
);
696 if (check_version(header
.major
, header
.minor
) < 0)
698 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
699 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
700 CTF_TRACE_SET_FIELD(td
, uuid
);
702 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
706 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
709 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
714 if (babeltrace_debug
) {
716 fprintf(stderr
, "[debug] metadata packet read: %s\n",
720 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
721 if (writelen
< readlen
) {
731 ret
= 0; /* continue reading next packet */
738 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
739 ret
= fseek(in
, toread
, SEEK_CUR
);
741 fprintf(stderr
, "[warning] Missing padding at end of file\n");
748 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
757 * Using strlen on *buf instead of size of open_memstream
758 * because its size includes garbage at the end (after final
759 * \0). This is the allocated size, not the actual string size.
761 out
= babeltrace_open_memstream(buf
, &size
);
763 perror("Metadata open_memstream");
767 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
776 /* close to flush the buffer */
777 ret
= babeltrace_close_memstream(buf
, &size
, out
);
779 perror("babeltrace_flush_memstream");
784 /* open for reading */
785 *fp
= babeltrace_fmemopen(*buf
, strlen(*buf
), "rb");
787 perror("Metadata fmemopen");
794 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
795 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
796 int whence
), FILE *metadata_fp
)
798 struct ctf_scanner
*scanner
;
799 struct ctf_file_stream
*metadata_stream
;
804 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
807 metadata_stream
->pos
.packet_seek
= packet_seek
;
809 fprintf(stderr
, "[error] packet_seek function undefined.\n");
817 td
->metadata
= &metadata_stream
->parent
;
818 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
819 if (metadata_stream
->pos
.fd
< 0) {
820 fprintf(stderr
, "Unable to open metadata.\n");
821 return metadata_stream
->pos
.fd
;
824 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
826 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
827 perror("Metadata stream open");
832 if (babeltrace_debug
)
835 if (packet_metadata(td
, fp
)) {
836 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
838 goto end_packet_read
;
840 unsigned int major
, minor
;
843 td
->byte_order
= BYTE_ORDER
;
845 /* Check text-only metadata header and version */
846 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
848 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
849 if (check_version(major
, minor
) < 0) {
851 goto end_packet_read
;
856 scanner
= ctf_scanner_alloc(fp
);
858 fprintf(stderr
, "[error] Error allocating scanner\n");
860 goto end_scanner_alloc
;
862 ret
= ctf_scanner_append_ast(scanner
);
864 fprintf(stderr
, "[error] Error creating AST\n");
868 if (babeltrace_debug
) {
869 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
871 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
876 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
878 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
881 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
884 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
888 ctf_scanner_free(scanner
);
894 close(metadata_stream
->pos
.fd
);
896 g_free(metadata_stream
);
901 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
902 struct ctf_stream_definition
*stream
,
903 struct ctf_event_declaration
*event
)
905 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
907 if (event
->context_decl
) {
908 struct definition
*definition
=
909 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
910 stream
->parent_def_scope
, 0, 0, "event.context");
914 stream_event
->event_context
= container_of(definition
,
915 struct definition_struct
, p
);
916 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
918 if (event
->fields_decl
) {
919 struct definition
*definition
=
920 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
921 stream
->parent_def_scope
, 0, 0, "event.fields");
925 stream_event
->event_fields
= container_of(definition
,
926 struct definition_struct
, p
);
927 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
929 stream_event
->stream
= stream
;
933 if (stream_event
->event_fields
)
934 definition_unref(&stream_event
->event_fields
->p
);
935 if (stream_event
->event_context
)
936 definition_unref(&stream_event
->event_context
->p
);
941 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
943 struct ctf_stream_declaration
*stream_class
;
947 if (stream
->stream_definitions_created
)
950 stream_class
= stream
->stream_class
;
952 if (stream_class
->packet_context_decl
) {
953 struct definition
*definition
=
954 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
955 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
960 stream
->stream_packet_context
= container_of(definition
,
961 struct definition_struct
, p
);
962 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
964 if (stream_class
->event_header_decl
) {
965 struct definition
*definition
=
966 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
967 stream
->parent_def_scope
, 0, 0, "stream.event.header");
972 stream
->stream_event_header
=
973 container_of(definition
, struct definition_struct
, p
);
974 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
976 if (stream_class
->event_context_decl
) {
977 struct definition
*definition
=
978 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
979 stream
->parent_def_scope
, 0, 0, "stream.event.context");
984 stream
->stream_event_context
=
985 container_of(definition
, struct definition_struct
, p
);
986 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
988 stream
->events_by_id
= g_ptr_array_new();
989 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
990 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
991 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
992 struct ctf_event_definition
*stream_event
;
996 stream_event
= create_event_definitions(td
, stream
, event
);
999 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1004 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1005 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1007 g_free(stream_event
);
1009 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1011 if (stream
->stream_event_context
)
1012 definition_unref(&stream
->stream_event_context
->p
);
1013 if (stream
->stream_event_header
)
1014 definition_unref(&stream
->stream_event_header
->p
);
1015 if (stream
->stream_packet_context
)
1016 definition_unref(&stream
->stream_packet_context
->p
);
1022 int create_stream_packet_index(struct ctf_trace
*td
,
1023 struct ctf_file_stream
*file_stream
)
1025 struct ctf_stream_declaration
*stream
;
1027 struct ctf_stream_pos
*pos
;
1028 struct stat filestats
;
1029 struct packet_index packet_index
;
1030 int first_packet
= 1;
1033 pos
= &file_stream
->pos
;
1035 ret
= fstat(pos
->fd
, &filestats
);
1039 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1042 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1043 uint64_t stream_id
= 0;
1046 /* unmap old base */
1047 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1049 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1055 /* map new base. Need mapping length from header. */
1056 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1057 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1058 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1059 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1060 pos
->offset
= 0; /* Position of the packet header */
1062 packet_index
.offset
= pos
->mmap_offset
;
1063 packet_index
.content_size
= 0;
1064 packet_index
.packet_size
= 0;
1065 packet_index
.timestamp_begin
= 0;
1066 packet_index
.timestamp_end
= 0;
1067 packet_index
.events_discarded
= 0;
1069 /* read and check header, set stream id (and check) */
1070 if (file_stream
->parent
.trace_packet_header
) {
1071 /* Read packet header */
1072 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1075 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1076 if (len_index
>= 0) {
1077 struct definition
*field
;
1080 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1081 magic
= get_unsigned_int(field
);
1082 if (magic
!= CTF_MAGIC
) {
1083 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1085 file_stream
->pos
.packet_index
->len
,
1086 (ssize_t
) pos
->mmap_offset
);
1092 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1093 if (len_index
>= 0) {
1094 struct definition_array
*defarray
;
1095 struct definition
*field
;
1097 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1099 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1100 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1101 defarray
= container_of(field
, struct definition_array
, p
);
1102 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1104 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1105 struct definition
*elem
;
1107 elem
= array_index(defarray
, i
);
1108 uuidval
[i
] = get_unsigned_int(elem
);
1110 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1112 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1118 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1119 if (len_index
>= 0) {
1120 struct definition
*field
;
1122 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1123 stream_id
= get_unsigned_int(field
);
1127 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1128 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1132 file_stream
->parent
.stream_id
= stream_id
;
1133 if (stream_id
>= td
->streams
->len
) {
1134 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1137 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1139 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1142 file_stream
->parent
.stream_class
= stream
;
1143 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1149 if (file_stream
->parent
.stream_packet_context
) {
1150 /* Read packet context */
1151 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1154 /* read content size from header */
1155 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1156 if (len_index
>= 0) {
1157 struct definition
*field
;
1159 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1160 packet_index
.content_size
= get_unsigned_int(field
);
1162 /* Use file size for packet size */
1163 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1166 /* read packet size from header */
1167 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
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
.packet_size
= get_unsigned_int(field
);
1174 /* Use content size if non-zero, else file size */
1175 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1178 /* read timestamp begin from header */
1179 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1180 if (len_index
>= 0) {
1181 struct definition
*field
;
1183 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1184 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1187 /* read timestamp end from header */
1188 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1189 if (len_index
>= 0) {
1190 struct definition
*field
;
1192 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1193 packet_index
.timestamp_end
= get_unsigned_int(field
);
1196 /* read events discarded from header */
1197 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1198 if (len_index
>= 0) {
1199 struct definition
*field
;
1201 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1202 packet_index
.events_discarded
= get_unsigned_int(field
);
1205 /* Use file size for packet size */
1206 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1207 /* Use content size if non-zero, else file size */
1208 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1211 /* Validate content size and packet size values */
1212 if (packet_index
.content_size
> packet_index
.packet_size
) {
1213 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1214 packet_index
.content_size
, packet_index
.packet_size
);
1218 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1219 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1220 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1224 /* Save position after header and context */
1225 packet_index
.data_offset
= pos
->offset
;
1227 /* add index to packet array */
1228 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1230 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1233 /* Move pos back to beginning of file */
1234 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1240 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1244 if (td
->packet_header_decl
) {
1245 struct definition
*definition
=
1246 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1247 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1252 stream
->trace_packet_header
=
1253 container_of(definition
, struct definition_struct
, p
);
1254 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1264 * Note: many file streams can inherit from the same stream class
1265 * description (metadata).
1268 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1269 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1273 struct ctf_file_stream
*file_stream
;
1275 ret
= openat(td
->dirfd
, path
, flags
);
1277 perror("File stream openat()");
1280 file_stream
= g_new0(struct ctf_file_stream
, 1);
1283 file_stream
->pos
.packet_seek
= packet_seek
;
1285 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1290 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1291 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1295 * For now, only a single slock is supported.
1297 file_stream
->parent
.current_clock
= td
->single_clock
;
1298 ret
= create_stream_packet_index(td
, file_stream
);
1301 /* Add stream file to stream class */
1302 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1303 &file_stream
->parent
);
1307 if (file_stream
->parent
.trace_packet_header
)
1308 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1310 ctf_fini_pos(&file_stream
->pos
);
1311 close(file_stream
->pos
.fd
);
1312 g_free(file_stream
);
1318 int ctf_open_trace_read(struct ctf_trace
*td
,
1319 const char *path
, int flags
,
1320 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1321 int whence
), FILE *metadata_fp
)
1324 struct dirent
*dirent
;
1325 struct dirent
*diriter
;
1330 /* Open trace directory */
1331 td
->dir
= opendir(path
);
1333 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1338 td
->dirfd
= open(path
, 0);
1339 if (td
->dirfd
< 0) {
1340 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1341 perror("Trace directory open");
1345 strncpy(td
->path
, path
, sizeof(td
->path
));
1346 td
->path
[sizeof(td
->path
) - 1] = '\0';
1349 * Keep the metadata file separate.
1352 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1354 goto error_metadata
;
1358 * Open each stream: for each file, try to open, check magic
1359 * number, and get the stream ID to add to the right location in
1363 dirent_len
= offsetof(struct dirent
, d_name
) +
1364 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1366 dirent
= malloc(dirent_len
);
1369 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1371 fprintf(stderr
, "[error] Readdir error.\n");
1376 /* Ignore hidden files, ., .. and metadata. */
1377 if (!strncmp(diriter
->d_name
, ".", 1)
1378 || !strcmp(diriter
->d_name
, "..")
1379 || !strcmp(diriter
->d_name
, "metadata"))
1381 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1382 flags
, packet_seek
);
1384 fprintf(stderr
, "[error] Open file stream error.\n");
1403 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1404 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1405 int whence
), FILE *metadata_fp
)
1407 struct ctf_trace
*td
;
1411 * If packet_seek is NULL, we provide our default version.
1414 packet_seek
= ctf_packet_seek
;
1416 td
= g_new0(struct ctf_trace
, 1);
1418 switch (flags
& O_ACCMODE
) {
1421 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1424 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1429 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1432 fprintf(stderr
, "[error] Incorrect open flags.\n");
1443 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1444 struct mmap_stream
*mmap_info
)
1446 pos
->mmap_offset
= 0;
1447 pos
->packet_size
= 0;
1448 pos
->content_size
= 0;
1449 pos
->content_size_loc
= NULL
;
1450 pos
->fd
= mmap_info
->fd
;
1455 pos
->packet_index
= NULL
;
1456 pos
->prot
= PROT_READ
;
1457 pos
->flags
= MAP_PRIVATE
;
1458 pos
->parent
.rw_table
= read_dispatch_table
;
1459 pos
->parent
.event_cb
= ctf_read_event
;
1463 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1464 struct ctf_file_stream
*file_stream
)
1466 struct ctf_stream_declaration
*stream
;
1467 uint64_t stream_id
= 0;
1470 file_stream
->parent
.stream_id
= stream_id
;
1471 if (stream_id
>= td
->streams
->len
) {
1472 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1473 "in metadata.\n", stream_id
);
1477 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1479 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1480 "in metadata.\n", stream_id
);
1484 file_stream
->parent
.stream_class
= stream
;
1485 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1491 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1492 struct mmap_stream
*mmap_info
,
1493 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1497 struct ctf_file_stream
*file_stream
;
1499 file_stream
= g_new0(struct ctf_file_stream
, 1);
1500 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1502 file_stream
->pos
.packet_seek
= packet_seek
;
1504 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1509 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1513 /* Add stream file to stream class */
1514 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1515 &file_stream
->parent
);
1519 if (file_stream
->parent
.trace_packet_header
)
1520 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1522 g_free(file_stream
);
1526 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1527 struct mmap_stream_list
*mmap_list
,
1528 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1533 struct mmap_stream
*mmap_info
;
1535 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1541 * for each stream, try to open, check magic number, and get the
1542 * stream ID to add to the right location in the stream array.
1544 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1545 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1547 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1559 struct trace_descriptor
*ctf_open_mmap_trace(
1560 struct mmap_stream_list
*mmap_list
,
1561 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1565 struct ctf_trace
*td
;
1569 fprintf(stderr
, "[error] No metadata file pointer associated, "
1570 "required for mmap parsing\n");
1574 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1577 td
= g_new0(struct ctf_trace
, 1);
1578 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1591 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1593 ctf_fini_pos(&file_stream
->pos
);
1594 close(file_stream
->pos
.fd
);
1598 void ctf_close_trace(struct trace_descriptor
*tdp
)
1600 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1604 for (i
= 0; i
< td
->streams
->len
; i
++) {
1605 struct ctf_stream_declaration
*stream
;
1608 stream
= g_ptr_array_index(td
->streams
, i
);
1611 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1612 struct ctf_file_stream
*file_stream
;
1613 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1614 ctf_close_file_stream(file_stream
);
1618 g_ptr_array_free(td
->streams
, TRUE
);
1621 if (td
->event_declarations
) {
1622 for (i
= 0; i
< td
->event_declarations
->len
; i
++) {
1623 struct bt_ctf_event_decl
*event
;
1625 event
= g_ptr_array_index(td
->event_declarations
, i
);
1628 g_ptr_array_free(td
->event_declarations
, TRUE
);
1635 void ctf_set_context(struct trace_descriptor
*descriptor
,
1636 struct bt_context
*ctx
)
1638 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1645 void ctf_set_handle(struct trace_descriptor
*descriptor
,
1646 struct bt_trace_handle
*handle
)
1648 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1651 td
->handle
= handle
;
1654 void __attribute__((constructor
)) ctf_init(void)
1658 ctf_format
.name
= g_quark_from_static_string("ctf");
1659 ret
= bt_register_format(&ctf_format
);
1663 /* TODO: finalize */