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
;
932 if (stream_event
->event_fields
)
933 definition_unref(&stream_event
->event_fields
->p
);
934 if (stream_event
->event_context
)
935 definition_unref(&stream_event
->event_context
->p
);
940 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
942 struct ctf_stream_declaration
*stream_class
;
946 if (stream
->stream_definitions_created
)
949 stream_class
= stream
->stream_class
;
951 if (stream_class
->packet_context_decl
) {
952 struct definition
*definition
=
953 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
954 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
959 stream
->stream_packet_context
= container_of(definition
,
960 struct definition_struct
, p
);
961 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
963 if (stream_class
->event_header_decl
) {
964 struct definition
*definition
=
965 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
966 stream
->parent_def_scope
, 0, 0, "stream.event.header");
971 stream
->stream_event_header
=
972 container_of(definition
, struct definition_struct
, p
);
973 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
975 if (stream_class
->event_context_decl
) {
976 struct definition
*definition
=
977 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
978 stream
->parent_def_scope
, 0, 0, "stream.event.context");
983 stream
->stream_event_context
=
984 container_of(definition
, struct definition_struct
, p
);
985 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
987 stream
->events_by_id
= g_ptr_array_new();
988 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
989 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
990 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
991 struct ctf_event_definition
*stream_event
;
995 stream_event
= create_event_definitions(td
, stream
, event
);
998 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1003 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1004 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1006 g_free(stream_event
);
1008 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1010 if (stream
->stream_event_context
)
1011 definition_unref(&stream
->stream_event_context
->p
);
1012 if (stream
->stream_event_header
)
1013 definition_unref(&stream
->stream_event_header
->p
);
1014 if (stream
->stream_packet_context
)
1015 definition_unref(&stream
->stream_packet_context
->p
);
1021 int create_stream_packet_index(struct ctf_trace
*td
,
1022 struct ctf_file_stream
*file_stream
)
1024 struct ctf_stream_declaration
*stream
;
1026 struct ctf_stream_pos
*pos
;
1027 struct stat filestats
;
1028 struct packet_index packet_index
;
1029 int first_packet
= 1;
1032 pos
= &file_stream
->pos
;
1034 ret
= fstat(pos
->fd
, &filestats
);
1038 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1041 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1042 uint64_t stream_id
= 0;
1045 /* unmap old base */
1046 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
1048 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1054 /* map new base. Need mapping length from header. */
1055 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1056 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1057 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1058 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1059 pos
->offset
= 0; /* Position of the packet header */
1061 packet_index
.offset
= pos
->mmap_offset
;
1062 packet_index
.content_size
= 0;
1063 packet_index
.packet_size
= 0;
1064 packet_index
.timestamp_begin
= 0;
1065 packet_index
.timestamp_end
= 0;
1066 packet_index
.events_discarded
= 0;
1068 /* read and check header, set stream id (and check) */
1069 if (file_stream
->parent
.trace_packet_header
) {
1070 /* Read packet header */
1071 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1074 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1075 if (len_index
>= 0) {
1076 struct definition
*field
;
1079 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1080 magic
= get_unsigned_int(field
);
1081 if (magic
!= CTF_MAGIC
) {
1082 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1084 file_stream
->pos
.packet_index
->len
,
1085 (ssize_t
) pos
->mmap_offset
);
1091 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1092 if (len_index
>= 0) {
1093 struct definition_array
*defarray
;
1094 struct definition
*field
;
1096 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1098 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1099 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1100 defarray
= container_of(field
, struct definition_array
, p
);
1101 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1103 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1104 struct definition
*elem
;
1106 elem
= array_index(defarray
, i
);
1107 uuidval
[i
] = get_unsigned_int(elem
);
1109 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1111 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1117 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1118 if (len_index
>= 0) {
1119 struct definition
*field
;
1121 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1122 stream_id
= get_unsigned_int(field
);
1126 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1127 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1131 file_stream
->parent
.stream_id
= stream_id
;
1132 if (stream_id
>= td
->streams
->len
) {
1133 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1136 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1138 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1141 file_stream
->parent
.stream_class
= stream
;
1142 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1148 if (file_stream
->parent
.stream_packet_context
) {
1149 /* Read packet context */
1150 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1153 /* read content size from header */
1154 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1155 if (len_index
>= 0) {
1156 struct definition
*field
;
1158 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1159 packet_index
.content_size
= get_unsigned_int(field
);
1161 /* Use file size for packet size */
1162 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1165 /* read packet size from header */
1166 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1167 if (len_index
>= 0) {
1168 struct definition
*field
;
1170 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1171 packet_index
.packet_size
= get_unsigned_int(field
);
1173 /* Use content size if non-zero, else file size */
1174 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1177 /* read timestamp begin from header */
1178 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1179 if (len_index
>= 0) {
1180 struct definition
*field
;
1182 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1183 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1186 /* read timestamp end from header */
1187 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1188 if (len_index
>= 0) {
1189 struct definition
*field
;
1191 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1192 packet_index
.timestamp_end
= get_unsigned_int(field
);
1195 /* read events discarded from header */
1196 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1197 if (len_index
>= 0) {
1198 struct definition
*field
;
1200 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1201 packet_index
.events_discarded
= get_unsigned_int(field
);
1204 /* Use file size for packet size */
1205 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1206 /* Use content size if non-zero, else file size */
1207 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1210 /* Validate content size and packet size values */
1211 if (packet_index
.content_size
> packet_index
.packet_size
) {
1212 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1213 packet_index
.content_size
, packet_index
.packet_size
);
1217 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1218 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1219 packet_index
.content_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1223 /* Save position after header and context */
1224 packet_index
.data_offset
= pos
->offset
;
1226 /* add index to packet array */
1227 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1229 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1232 /* Move pos back to beginning of file */
1233 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
1239 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1243 if (td
->packet_header_decl
) {
1244 struct definition
*definition
=
1245 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1246 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1251 stream
->trace_packet_header
=
1252 container_of(definition
, struct definition_struct
, p
);
1253 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1263 * Note: many file streams can inherit from the same stream class
1264 * description (metadata).
1267 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1268 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1272 struct ctf_file_stream
*file_stream
;
1274 ret
= openat(td
->dirfd
, path
, flags
);
1276 perror("File stream openat()");
1279 file_stream
= g_new0(struct ctf_file_stream
, 1);
1282 file_stream
->pos
.packet_seek
= packet_seek
;
1284 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1289 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1290 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1294 * For now, only a single slock is supported.
1296 file_stream
->parent
.current_clock
= td
->single_clock
;
1297 ret
= create_stream_packet_index(td
, file_stream
);
1300 /* Add stream file to stream class */
1301 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1302 &file_stream
->parent
);
1306 if (file_stream
->parent
.trace_packet_header
)
1307 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1309 ctf_fini_pos(&file_stream
->pos
);
1310 close(file_stream
->pos
.fd
);
1311 g_free(file_stream
);
1317 int ctf_open_trace_read(struct ctf_trace
*td
,
1318 const char *path
, int flags
,
1319 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1320 int whence
), FILE *metadata_fp
)
1323 struct dirent
*dirent
;
1324 struct dirent
*diriter
;
1329 /* Open trace directory */
1330 td
->dir
= opendir(path
);
1332 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1337 td
->dirfd
= open(path
, 0);
1338 if (td
->dirfd
< 0) {
1339 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1340 perror("Trace directory open");
1344 strncpy(td
->path
, path
, sizeof(td
->path
));
1345 td
->path
[sizeof(td
->path
) - 1] = '\0';
1348 * Keep the metadata file separate.
1351 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1353 goto error_metadata
;
1357 * Open each stream: for each file, try to open, check magic
1358 * number, and get the stream ID to add to the right location in
1362 dirent_len
= offsetof(struct dirent
, d_name
) +
1363 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1365 dirent
= malloc(dirent_len
);
1368 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1370 fprintf(stderr
, "[error] Readdir error.\n");
1375 /* Ignore hidden files, ., .. and metadata. */
1376 if (!strncmp(diriter
->d_name
, ".", 1)
1377 || !strcmp(diriter
->d_name
, "..")
1378 || !strcmp(diriter
->d_name
, "metadata"))
1380 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1381 flags
, packet_seek
);
1383 fprintf(stderr
, "[error] Open file stream error.\n");
1402 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1403 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1404 int whence
), FILE *metadata_fp
)
1406 struct ctf_trace
*td
;
1410 * If packet_seek is NULL, we provide our default version.
1413 packet_seek
= ctf_packet_seek
;
1415 td
= g_new0(struct ctf_trace
, 1);
1417 switch (flags
& O_ACCMODE
) {
1420 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1423 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1428 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1431 fprintf(stderr
, "[error] Incorrect open flags.\n");
1442 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1443 struct mmap_stream
*mmap_info
)
1445 pos
->mmap_offset
= 0;
1446 pos
->packet_size
= 0;
1447 pos
->content_size
= 0;
1448 pos
->content_size_loc
= NULL
;
1449 pos
->fd
= mmap_info
->fd
;
1454 pos
->packet_index
= NULL
;
1455 pos
->prot
= PROT_READ
;
1456 pos
->flags
= MAP_PRIVATE
;
1457 pos
->parent
.rw_table
= read_dispatch_table
;
1458 pos
->parent
.event_cb
= ctf_read_event
;
1462 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1463 struct ctf_file_stream
*file_stream
)
1465 struct ctf_stream_declaration
*stream
;
1466 uint64_t stream_id
= 0;
1469 file_stream
->parent
.stream_id
= stream_id
;
1470 if (stream_id
>= td
->streams
->len
) {
1471 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1472 "in metadata.\n", stream_id
);
1476 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1478 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1479 "in metadata.\n", stream_id
);
1483 file_stream
->parent
.stream_class
= stream
;
1484 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1490 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1491 struct mmap_stream
*mmap_info
,
1492 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1496 struct ctf_file_stream
*file_stream
;
1498 file_stream
= g_new0(struct ctf_file_stream
, 1);
1499 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1501 file_stream
->pos
.packet_seek
= packet_seek
;
1503 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1508 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1512 /* Add stream file to stream class */
1513 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1514 &file_stream
->parent
);
1518 if (file_stream
->parent
.trace_packet_header
)
1519 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1521 g_free(file_stream
);
1525 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1526 struct mmap_stream_list
*mmap_list
,
1527 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1532 struct mmap_stream
*mmap_info
;
1534 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1540 * for each stream, try to open, check magic number, and get the
1541 * stream ID to add to the right location in the stream array.
1543 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1544 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1546 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1558 struct trace_descriptor
*ctf_open_mmap_trace(
1559 struct mmap_stream_list
*mmap_list
,
1560 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1564 struct ctf_trace
*td
;
1568 fprintf(stderr
, "[error] No metadata file pointer associated, "
1569 "required for mmap parsing\n");
1573 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1576 td
= g_new0(struct ctf_trace
, 1);
1577 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1590 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1592 ctf_fini_pos(&file_stream
->pos
);
1593 close(file_stream
->pos
.fd
);
1597 void ctf_close_trace(struct trace_descriptor
*tdp
)
1599 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1603 for (i
= 0; i
< td
->streams
->len
; i
++) {
1604 struct ctf_stream_declaration
*stream
;
1607 stream
= g_ptr_array_index(td
->streams
, i
);
1610 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1611 struct ctf_file_stream
*file_stream
;
1612 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1613 ctf_close_file_stream(file_stream
);
1617 g_ptr_array_free(td
->streams
, TRUE
);
1624 void ctf_set_context(struct trace_descriptor
*descriptor
,
1625 struct bt_context
*ctx
)
1627 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1634 void ctf_set_handle(struct trace_descriptor
*descriptor
,
1635 struct bt_trace_handle
*handle
)
1637 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1640 td
->handle
= handle
;
1643 void __attribute__((constructor
)) ctf_init(void)
1647 ctf_format
.name
= g_quark_from_static_string("ctf");
1648 ret
= bt_register_format(&ctf_format
);
1652 /* TODO: finalize */