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))
58 struct trace_descriptor
*ctf_open_trace(const char *collection_path
, const char *path
, int flags
,
59 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
60 int whence
), FILE *metadata_fp
);
62 struct trace_descriptor
*ctf_open_mmap_trace(
63 struct mmap_stream_list
*mmap_list
,
64 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
68 void ctf_close_trace(struct trace_descriptor
*descriptor
);
71 rw_dispatch read_dispatch_table
[] = {
72 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
73 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
74 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
75 [ CTF_TYPE_STRING
] = ctf_string_read
,
76 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
77 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
78 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
79 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
83 rw_dispatch write_dispatch_table
[] = {
84 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
85 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
86 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
87 [ CTF_TYPE_STRING
] = ctf_string_write
,
88 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
89 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
90 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
91 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
95 struct format ctf_format
= {
96 .open_trace
= ctf_open_trace
,
97 .open_mmap_trace
= ctf_open_mmap_trace
,
98 .close_trace
= ctf_close_trace
,
102 void ctf_update_timestamp(struct ctf_stream
*stream
,
103 struct definition_integer
*integer_definition
)
105 struct declaration_integer
*integer_declaration
=
106 integer_definition
->declaration
;
107 uint64_t oldval
, newval
, updateval
;
109 if (unlikely(integer_declaration
->len
== 64)) {
110 stream
->timestamp
= integer_definition
->value
._unsigned
;
114 oldval
= stream
->timestamp
;
115 oldval
&= (1ULL << integer_declaration
->len
) - 1;
116 newval
= integer_definition
->value
._unsigned
;
117 /* Test for overflow by comparing low bits */
119 newval
+= 1ULL << integer_declaration
->len
;
120 /* updateval contains old high bits, and new low bits (sum) */
121 updateval
= stream
->timestamp
;
122 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
124 stream
->prev_timestamp
= stream
->timestamp
;
125 stream
->timestamp
= updateval
;
129 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
131 struct ctf_stream_pos
*pos
=
132 container_of(ppos
, struct ctf_stream_pos
, parent
);
133 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
134 struct ctf_stream_event
*event
;
138 /* We need to check for EOF here for empty files. */
139 if (unlikely(pos
->offset
== EOF
))
142 ctf_pos_get_event(pos
);
145 * This is the EOF check after we've advanced the position in
148 if (unlikely(pos
->offset
== EOF
))
150 assert(pos
->offset
< pos
->content_size
);
152 /* Read event header */
153 if (likely(stream
->stream_event_header
)) {
154 struct definition_integer
*integer_definition
;
155 struct definition
*variant
;
157 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
160 /* lookup event id */
161 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
162 if (integer_definition
) {
163 id
= integer_definition
->value
._unsigned
;
165 struct definition_enum
*enum_definition
;
167 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
168 if (enum_definition
) {
169 id
= enum_definition
->integer
->value
._unsigned
;
173 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
175 integer_definition
= lookup_integer(variant
, "id", FALSE
);
176 if (integer_definition
) {
177 id
= integer_definition
->value
._unsigned
;
180 stream
->event_id
= id
;
182 /* lookup timestamp */
183 stream
->has_timestamp
= 0;
184 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
185 if (integer_definition
) {
186 ctf_update_timestamp(stream
, integer_definition
);
187 stream
->has_timestamp
= 1;
190 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
191 if (integer_definition
) {
192 ctf_update_timestamp(stream
, integer_definition
);
193 stream
->has_timestamp
= 1;
199 /* Read stream-declared event context */
200 if (stream
->stream_event_context
) {
201 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
206 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
207 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
210 event
= g_ptr_array_index(stream
->events_by_id
, id
);
211 if (unlikely(!event
)) {
212 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
216 /* Read event-declared event context */
217 if (event
->event_context
) {
218 ret
= generic_rw(ppos
, &event
->event_context
->p
);
223 /* Read event payload */
224 if (likely(event
->event_fields
)) {
225 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
233 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
238 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
240 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
241 struct ctf_stream_event
*event
;
245 id
= stream
->event_id
;
247 /* print event header */
248 if (likely(stream
->stream_event_header
)) {
249 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
254 /* print stream-declared event context */
255 if (stream
->stream_event_context
) {
256 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
261 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
262 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
265 event
= g_ptr_array_index(stream
->events_by_id
, id
);
266 if (unlikely(!event
)) {
267 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
271 /* print event-declared event context */
272 if (event
->event_context
) {
273 ret
= generic_rw(pos
, &event
->event_context
->p
);
278 /* Read and print event payload */
279 if (likely(event
->event_fields
)) {
280 ret
= generic_rw(pos
, &event
->event_fields
->p
);
288 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
292 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
295 pos
->mmap_offset
= 0;
296 pos
->packet_size
= 0;
297 pos
->content_size
= 0;
298 pos
->content_size_loc
= NULL
;
304 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
305 sizeof(struct packet_index
));
307 pos
->packet_index
= NULL
;
308 switch (open_flags
& O_ACCMODE
) {
310 pos
->prot
= PROT_READ
;
311 pos
->flags
= MAP_PRIVATE
;
312 pos
->parent
.rw_table
= read_dispatch_table
;
313 pos
->parent
.event_cb
= ctf_read_event
;
316 pos
->prot
= PROT_WRITE
; /* Write has priority */
317 pos
->flags
= MAP_SHARED
;
318 pos
->parent
.rw_table
= write_dispatch_table
;
319 pos
->parent
.event_cb
= ctf_write_event
;
321 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
328 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
332 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
333 *pos
->content_size_loc
= pos
->offset
;
336 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
338 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
343 (void) g_array_free(pos
->packet_index
, TRUE
);
346 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
348 struct ctf_file_stream
*file_stream
=
349 container_of(pos
, struct ctf_file_stream
, pos
);
352 struct packet_index
*index
;
354 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
355 *pos
->content_size_loc
= pos
->offset
;
359 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
361 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
369 * The caller should never ask for ctf_move_pos across packets,
370 * except to get exactly at the beginning of the next packet.
372 if (pos
->prot
== PROT_WRITE
) {
375 /* The writer will add padding */
376 assert(pos
->offset
+ offset
== pos
->packet_size
);
377 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
380 assert(offset
== 0); /* only seek supported for now */
386 pos
->content_size
= -1U; /* Unknown at this point */
387 pos
->packet_size
= WRITE_PACKET_LEN
;
388 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
389 pos
->packet_size
/ CHAR_BIT
);
397 uint32_t events_discarded_diff
;
399 /* For printing discarded event count */
400 index
= &g_array_index(pos
->packet_index
,
401 struct packet_index
, pos
->cur_index
);
402 events_discarded_diff
= index
->events_discarded
;
403 file_stream
->parent
.prev_timestamp_end
=
404 index
->timestamp_end
;
405 if (pos
->cur_index
> 0) {
406 index
= &g_array_index(pos
->packet_index
,
409 events_discarded_diff
-= index
->events_discarded
;
411 file_stream
->parent
.events_discarded
= events_discarded_diff
;
412 if (pos
->offset
== EOF
)
414 /* The reader will expect us to skip padding */
415 assert(pos
->offset
+ offset
== pos
->content_size
);
417 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
421 assert(offset
== 0); /* only seek supported for now */
423 file_stream
->parent
.prev_timestamp
= 0;
424 file_stream
->parent
.prev_timestamp_end
= 0;
429 if (pos
->cur_index
>= pos
->packet_index
->len
) {
433 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
435 pos
->mmap_offset
= index
->offset
;
437 /* Lookup context/packet size in index */
438 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
439 pos
->content_size
= index
->content_size
;
440 pos
->packet_size
= index
->packet_size
;
441 if (index
->data_offset
< index
->content_size
) {
442 pos
->offset
= 0; /* will read headers */
443 } else if (index
->data_offset
== index
->content_size
) {
445 pos
->offset
= index
->data_offset
;
448 goto read_next_packet
;
454 /* map new base. Need mapping length from header. */
455 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
456 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
457 if (pos
->base
== MAP_FAILED
) {
458 fprintf(stderr
, "[error] mmap error %s.\n",
463 /* update trace_packet_header and stream_packet_context */
464 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
465 /* Read packet header */
466 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
469 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
470 /* Read packet context */
471 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
477 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
483 len
= fread(&magic
, sizeof(magic
), 1, fp
);
487 if (magic
== TSDL_MAGIC
) {
489 td
->byte_order
= BYTE_ORDER
;
490 CTF_TRACE_SET_FIELD(td
, byte_order
);
491 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
493 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
494 LITTLE_ENDIAN
: BIG_ENDIAN
;
495 CTF_TRACE_SET_FIELD(td
, byte_order
);
503 * Returns 0 on success, -1 on error.
506 int check_version(unsigned int major
, unsigned int minor
)
521 /* eventually return an error instead of warning */
523 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
529 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
532 struct metadata_packet_header header
;
533 size_t readlen
, writelen
, toread
;
534 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
537 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
541 if (td
->byte_order
!= BYTE_ORDER
) {
542 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
543 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
544 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
545 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
548 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
549 if (header
.compression_scheme
) {
550 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
551 header
.compression_scheme
);
554 if (header
.encryption_scheme
) {
555 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
556 header
.encryption_scheme
);
559 if (header
.checksum_scheme
) {
560 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
561 header
.checksum_scheme
);
564 if (check_version(header
.major
, header
.minor
) < 0)
566 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
567 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
568 CTF_TRACE_SET_FIELD(td
, uuid
);
570 if (uuid_compare(header
.uuid
, td
->uuid
))
574 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
577 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
582 if (babeltrace_debug
) {
584 fprintf(stderr
, "[debug] metadata packet read: %s\n",
588 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
589 if (writelen
< readlen
) {
599 ret
= 0; /* continue reading next packet */
606 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
607 ret
= fseek(in
, toread
, SEEK_CUR
);
609 fprintf(stderr
, "[warning] Missing padding at end of file\n");
616 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
625 * Using strlen on *buf instead of size of open_memstream
626 * because its size includes garbage at the end (after final
627 * \0). This is the allocated size, not the actual string size.
629 out
= open_memstream(buf
, &size
);
631 perror("Metadata open_memstream");
635 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
644 fclose(out
); /* flush the buffer */
646 /* open for reading */
647 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
649 perror("Metadata fmemopen");
656 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
657 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
658 int whence
), FILE *metadata_fp
)
660 struct ctf_scanner
*scanner
;
661 struct ctf_file_stream
*metadata_stream
;
666 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
669 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
671 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
679 td
->metadata
= &metadata_stream
->parent
;
680 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
681 if (metadata_stream
->pos
.fd
< 0) {
682 fprintf(stderr
, "Unable to open metadata.\n");
683 return metadata_stream
->pos
.fd
;
686 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
688 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
689 perror("Metadata stream open");
694 if (babeltrace_debug
)
697 if (packet_metadata(td
, fp
)) {
698 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
700 goto end_packet_read
;
702 unsigned int major
, minor
;
705 td
->byte_order
= BYTE_ORDER
;
707 /* Check text-only metadata header and version */
708 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
710 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
711 if (check_version(major
, minor
) < 0) {
713 goto end_packet_read
;
718 scanner
= ctf_scanner_alloc(fp
);
720 fprintf(stderr
, "[error] Error allocating scanner\n");
722 goto end_scanner_alloc
;
724 ret
= ctf_scanner_append_ast(scanner
);
726 fprintf(stderr
, "[error] Error creating AST\n");
730 if (babeltrace_debug
) {
731 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
733 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
738 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
740 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
743 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
746 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
750 ctf_scanner_free(scanner
);
756 close(metadata_stream
->pos
.fd
);
758 g_free(metadata_stream
);
763 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
764 struct ctf_stream
*stream
,
765 struct ctf_event
*event
)
767 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
769 if (event
->context_decl
) {
770 struct definition
*definition
=
771 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
772 stream
->parent_def_scope
, 0, 0, "event.context");
776 stream_event
->event_context
= container_of(definition
,
777 struct definition_struct
, p
);
778 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
780 if (event
->fields_decl
) {
781 struct definition
*definition
=
782 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
783 stream
->parent_def_scope
, 0, 0, "event.fields");
787 stream_event
->event_fields
= container_of(definition
,
788 struct definition_struct
, p
);
789 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
794 if (stream_event
->event_fields
)
795 definition_unref(&stream_event
->event_fields
->p
);
796 if (stream_event
->event_context
)
797 definition_unref(&stream_event
->event_context
->p
);
802 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
804 struct ctf_stream_class
*stream_class
;
808 if (stream
->stream_definitions_created
)
811 stream_class
= stream
->stream_class
;
813 if (stream_class
->packet_context_decl
) {
814 struct definition
*definition
=
815 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
816 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
821 stream
->stream_packet_context
= container_of(definition
,
822 struct definition_struct
, p
);
823 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
825 if (stream_class
->event_header_decl
) {
826 struct definition
*definition
=
827 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
828 stream
->parent_def_scope
, 0, 0, "stream.event.header");
833 stream
->stream_event_header
=
834 container_of(definition
, struct definition_struct
, p
);
835 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
837 if (stream_class
->event_context_decl
) {
838 struct definition
*definition
=
839 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
840 stream
->parent_def_scope
, 0, 0, "stream.event.context");
845 stream
->stream_event_context
=
846 container_of(definition
, struct definition_struct
, p
);
847 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
849 stream
->events_by_id
= g_ptr_array_new();
850 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
851 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
852 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
853 struct ctf_stream_event
*stream_event
;
857 stream_event
= create_event_definitions(td
, stream
, event
);
860 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
865 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
866 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
868 g_free(stream_event
);
870 g_ptr_array_free(stream
->events_by_id
, TRUE
);
872 if (stream
->stream_event_context
)
873 definition_unref(&stream
->stream_event_context
->p
);
874 if (stream
->stream_event_header
)
875 definition_unref(&stream
->stream_event_header
->p
);
876 if (stream
->stream_packet_context
)
877 definition_unref(&stream
->stream_packet_context
->p
);
883 int create_stream_packet_index(struct ctf_trace
*td
,
884 struct ctf_file_stream
*file_stream
)
886 struct ctf_stream_class
*stream
;
888 struct ctf_stream_pos
*pos
;
889 struct stat filestats
;
890 struct packet_index packet_index
;
891 int first_packet
= 1;
894 pos
= &file_stream
->pos
;
896 ret
= fstat(pos
->fd
, &filestats
);
900 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
903 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
904 uint64_t stream_id
= 0;
908 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
910 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
916 /* map new base. Need mapping length from header. */
917 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
918 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
919 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
920 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
921 pos
->offset
= 0; /* Position of the packet header */
923 packet_index
.offset
= pos
->mmap_offset
;
924 packet_index
.content_size
= 0;
925 packet_index
.packet_size
= 0;
926 packet_index
.timestamp_begin
= 0;
927 packet_index
.timestamp_end
= 0;
928 packet_index
.events_discarded
= 0;
930 /* read and check header, set stream id (and check) */
931 if (file_stream
->parent
.trace_packet_header
) {
932 /* Read packet header */
933 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
936 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
937 if (len_index
>= 0) {
938 struct definition
*field
;
941 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
942 magic
= get_unsigned_int(field
);
943 if (magic
!= CTF_MAGIC
) {
944 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
946 file_stream
->pos
.packet_index
->len
,
947 (ssize_t
) pos
->mmap_offset
);
953 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
954 if (len_index
>= 0) {
955 struct definition_array
*defarray
;
956 struct definition
*field
;
958 uint8_t uuidval
[UUID_LEN
];
960 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
961 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
962 defarray
= container_of(field
, struct definition_array
, p
);
963 assert(array_len(defarray
) == UUID_LEN
);
965 for (i
= 0; i
< UUID_LEN
; i
++) {
966 struct definition
*elem
;
968 elem
= array_index(defarray
, i
);
969 uuidval
[i
] = get_unsigned_int(elem
);
971 ret
= uuid_compare(td
->uuid
, uuidval
);
973 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
979 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
980 if (len_index
>= 0) {
981 struct definition
*field
;
983 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
984 stream_id
= get_unsigned_int(field
);
988 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
989 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
993 file_stream
->parent
.stream_id
= stream_id
;
994 if (stream_id
>= td
->streams
->len
) {
995 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
998 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1000 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1003 file_stream
->parent
.stream_class
= stream
;
1004 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1010 if (file_stream
->parent
.stream_packet_context
) {
1011 /* Read packet context */
1012 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1015 /* read content size from header */
1016 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1017 if (len_index
>= 0) {
1018 struct definition
*field
;
1020 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1021 packet_index
.content_size
= get_unsigned_int(field
);
1023 /* Use file size for packet size */
1024 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1027 /* read packet size from header */
1028 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1029 if (len_index
>= 0) {
1030 struct definition
*field
;
1032 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1033 packet_index
.packet_size
= get_unsigned_int(field
);
1035 /* Use content size if non-zero, else file size */
1036 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1039 /* read timestamp begin from header */
1040 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1041 if (len_index
>= 0) {
1042 struct definition
*field
;
1044 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1045 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1048 /* read timestamp end from header */
1049 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1050 if (len_index
>= 0) {
1051 struct definition
*field
;
1053 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1054 packet_index
.timestamp_end
= get_unsigned_int(field
);
1057 /* read events discarded from header */
1058 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1059 if (len_index
>= 0) {
1060 struct definition
*field
;
1062 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1063 packet_index
.events_discarded
= get_unsigned_int(field
);
1066 /* Use file size for packet size */
1067 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1068 /* Use content size if non-zero, else file size */
1069 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1072 /* Validate content size and packet size values */
1073 if (packet_index
.content_size
> packet_index
.packet_size
) {
1074 fprintf(stderr
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1075 packet_index
.content_size
, packet_index
.packet_size
);
1079 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1080 fprintf(stderr
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1081 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1085 /* Save position after header and context */
1086 packet_index
.data_offset
= pos
->offset
;
1088 /* add index to packet array */
1089 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1091 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1094 /* Move pos back to beginning of file */
1095 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1101 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1105 if (td
->packet_header_decl
) {
1106 struct definition
*definition
=
1107 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1108 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1113 stream
->trace_packet_header
=
1114 container_of(definition
, struct definition_struct
, p
);
1115 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1125 * Note: many file streams can inherit from the same stream class
1126 * description (metadata).
1129 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1130 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1134 struct ctf_file_stream
*file_stream
;
1136 ret
= openat(td
->dirfd
, path
, flags
);
1138 perror("File stream openat()");
1141 file_stream
= g_new0(struct ctf_file_stream
, 1);
1143 if (move_pos_slow
) {
1144 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1146 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1151 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1152 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1155 ret
= create_stream_packet_index(td
, file_stream
);
1158 /* Add stream file to stream class */
1159 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1160 &file_stream
->parent
);
1164 if (file_stream
->parent
.trace_packet_header
)
1165 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1167 ctf_fini_pos(&file_stream
->pos
);
1168 close(file_stream
->pos
.fd
);
1169 g_free(file_stream
);
1175 init_domain_name(struct ctf_trace
*td
)
1179 start
= td
->path
+ strlen(td
->collection_path
);
1180 while (start
[0] == '/')
1181 start
++; /* skip / */
1182 end
= strchr(start
, '/');
1184 end
= start
+ strlen(start
);
1185 memcpy(td
->domain
, start
, end
- start
);
1186 td
->domain
[end
- start
] = '\0';
1190 init_proc_name(struct ctf_trace
*td
)
1195 if (td
->domain
[0] == '\0')
1197 memcpy(buf
, td
->path
, PATH_MAX
);
1198 start
= buf
+ strlen(td
->collection_path
);
1199 while (start
[0] == '/')
1200 start
++; /* skip / */
1201 start
= strchr(start
, '/'); /* get begin of domain content */
1204 while (start
[0] == '/')
1205 start
++; /* skip / */
1206 /* find last -, skips time */
1207 end
= strrchr(start
, '-');
1211 /* find previous -, skips date */
1212 end
= strrchr(start
, '-');
1216 /* find previous -, skips pid */
1217 end
= strrchr(start
, '-');
1222 memcpy(td
->procname
, start
, end
- start
);
1223 td
->procname
[end
- start
] = '\0';
1227 init_vpid(struct ctf_trace
*td
)
1232 if (td
->domain
[0] == '\0')
1234 memcpy(buf
, td
->path
, PATH_MAX
);
1235 start
= buf
+ strlen(td
->collection_path
);
1236 while (start
[0] == '/')
1237 start
++; /* skip / */
1238 start
= strchr(start
, '/'); /* get begin of domain content */
1241 while (start
[0] == '/')
1242 start
++; /* skip / */
1243 /* find last -, skips time */
1244 end
= strrchr(start
, '-');
1248 /* find previous -, skips date */
1249 end
= strrchr(start
, '-');
1253 /* find previous -, skips pid */
1254 start
= strrchr(start
, '-');
1257 start
++; /* skip - */
1259 memcpy(td
->vpid
, start
, end
- start
);
1260 td
->vpid
[end
- start
] = '\0';
1264 int ctf_open_trace_read(struct ctf_trace
*td
, const char *collection_path
,
1265 const char *path
, int flags
,
1266 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1267 int whence
), FILE *metadata_fp
)
1270 struct dirent
*dirent
;
1271 struct dirent
*diriter
;
1273 char *respath
, *rescolpath
;
1277 /* Open trace directory */
1278 td
->dir
= opendir(path
);
1280 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1285 td
->dirfd
= open(path
, 0);
1286 if (td
->dirfd
< 0) {
1287 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1288 perror("Trace directory open");
1292 rescolpath
= realpath(collection_path
, td
->collection_path
);
1294 fprintf(stderr
, "[error] collection path resolution failure\n");
1297 respath
= realpath(path
, td
->path
);
1299 fprintf(stderr
, "[error] path resolution failure\n");
1302 init_domain_name(td
);
1307 * Keep the metadata file separate.
1310 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1312 goto error_metadata
;
1316 * Open each stream: for each file, try to open, check magic
1317 * number, and get the stream ID to add to the right location in
1321 dirent_len
= offsetof(struct dirent
, d_name
) +
1322 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1324 dirent
= malloc(dirent_len
);
1327 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1329 fprintf(stderr
, "[error] Readdir error.\n");
1334 /* Ignore hidden files, ., .. and metadata. */
1335 if (!strncmp(diriter
->d_name
, ".", 1)
1336 || !strcmp(diriter
->d_name
, "..")
1337 || !strcmp(diriter
->d_name
, "metadata"))
1339 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1341 fprintf(stderr
, "[error] Open file stream error.\n");
1360 struct trace_descriptor
*ctf_open_trace(const char *collection_path
, const char *path
, int flags
,
1361 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1362 int whence
), FILE *metadata_fp
)
1364 struct ctf_trace
*td
;
1367 td
= g_new0(struct ctf_trace
, 1);
1369 switch (flags
& O_ACCMODE
) {
1372 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1375 ret
= ctf_open_trace_read(td
, collection_path
, path
, flags
, move_pos_slow
, metadata_fp
);
1380 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1383 fprintf(stderr
, "[error] Incorrect open flags.\n");
1394 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1395 struct mmap_stream
*mmap_info
)
1397 pos
->mmap_offset
= 0;
1398 pos
->packet_size
= 0;
1399 pos
->content_size
= 0;
1400 pos
->content_size_loc
= NULL
;
1401 pos
->fd
= mmap_info
->fd
;
1406 pos
->packet_index
= NULL
;
1407 pos
->prot
= PROT_READ
;
1408 pos
->flags
= MAP_PRIVATE
;
1409 pos
->parent
.rw_table
= read_dispatch_table
;
1410 pos
->parent
.event_cb
= ctf_read_event
;
1414 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1415 struct ctf_file_stream
*file_stream
)
1417 struct ctf_stream_class
*stream
;
1418 uint64_t stream_id
= 0;
1421 file_stream
->parent
.stream_id
= stream_id
;
1422 if (stream_id
>= td
->streams
->len
) {
1423 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1424 "in metadata.\n", stream_id
);
1428 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1430 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1431 "in metadata.\n", stream_id
);
1435 file_stream
->parent
.stream_class
= stream
;
1436 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1442 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1443 struct mmap_stream
*mmap_info
,
1444 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1448 struct ctf_file_stream
*file_stream
;
1450 file_stream
= g_new0(struct ctf_file_stream
, 1);
1451 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1453 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1455 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1460 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1464 /* Add stream file to stream class */
1465 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1466 &file_stream
->parent
);
1470 if (file_stream
->parent
.trace_packet_header
)
1471 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1473 g_free(file_stream
);
1477 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1478 struct mmap_stream_list
*mmap_list
,
1479 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1484 struct mmap_stream
*mmap_info
;
1486 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1492 * for each stream, try to open, check magic number, and get the
1493 * stream ID to add to the right location in the stream array.
1495 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1496 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1498 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1510 struct trace_descriptor
*ctf_open_mmap_trace(
1511 struct mmap_stream_list
*mmap_list
,
1512 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1515 struct ctf_trace
*td
;
1519 fprintf(stderr
, "[error] No metadata file pointer associated, "
1520 "required for mmap parsing\n");
1523 if (!move_pos_slow
) {
1524 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1527 td
= g_new0(struct ctf_trace
, 1);
1528 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1541 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1543 ctf_fini_pos(&file_stream
->pos
);
1544 close(file_stream
->pos
.fd
);
1548 void ctf_close_trace(struct trace_descriptor
*tdp
)
1550 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1554 for (i
= 0; i
< td
->streams
->len
; i
++) {
1555 struct ctf_stream_class
*stream
;
1558 stream
= g_ptr_array_index(td
->streams
, i
);
1561 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1562 struct ctf_file_stream
*file_stream
;
1563 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1564 ctf_close_file_stream(file_stream
);
1568 g_ptr_array_free(td
->streams
, TRUE
);
1574 void __attribute__((constructor
)) ctf_init(void)
1578 ctf_format
.name
= g_quark_from_static_string("ctf");
1579 ret
= bt_register_format(&ctf_format
);
1583 /* TODO: finalize */