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
->timestamp
= updateval
;
128 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream
*stream
)
130 struct ctf_stream_pos
*pos
=
131 container_of(ppos
, struct ctf_stream_pos
, parent
);
132 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
133 struct ctf_stream_event
*event
;
137 /* We need to check for EOF here for empty files. */
138 if (unlikely(pos
->offset
== EOF
))
141 ctf_pos_get_event(pos
);
144 * This is the EOF check after we've advanced the position in
147 if (unlikely(pos
->offset
== EOF
))
149 assert(pos
->offset
< pos
->content_size
);
151 /* Read event header */
152 if (likely(stream
->stream_event_header
)) {
153 struct definition_integer
*integer_definition
;
154 struct definition
*variant
;
156 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
159 /* lookup event id */
160 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
161 if (integer_definition
) {
162 id
= integer_definition
->value
._unsigned
;
164 struct definition_enum
*enum_definition
;
166 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
167 if (enum_definition
) {
168 id
= enum_definition
->integer
->value
._unsigned
;
172 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
174 integer_definition
= lookup_integer(variant
, "id", FALSE
);
175 if (integer_definition
) {
176 id
= integer_definition
->value
._unsigned
;
179 stream
->event_id
= id
;
181 /* lookup timestamp */
182 stream
->has_timestamp
= 0;
183 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
184 if (integer_definition
) {
185 ctf_update_timestamp(stream
, integer_definition
);
186 stream
->has_timestamp
= 1;
189 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
190 if (integer_definition
) {
191 ctf_update_timestamp(stream
, integer_definition
);
192 stream
->has_timestamp
= 1;
198 /* Read stream-declared event context */
199 if (stream
->stream_event_context
) {
200 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
205 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
206 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
209 event
= g_ptr_array_index(stream
->events_by_id
, id
);
210 if (unlikely(!event
)) {
211 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
215 /* Read event-declared event context */
216 if (event
->event_context
) {
217 ret
= generic_rw(ppos
, &event
->event_context
->p
);
222 /* Read event payload */
223 if (likely(event
->event_fields
)) {
224 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
232 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
237 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream
*stream
)
239 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
240 struct ctf_stream_event
*event
;
244 id
= stream
->event_id
;
246 /* print event header */
247 if (likely(stream
->stream_event_header
)) {
248 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
253 /* print stream-declared event context */
254 if (stream
->stream_event_context
) {
255 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
260 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
261 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
264 event
= g_ptr_array_index(stream
->events_by_id
, id
);
265 if (unlikely(!event
)) {
266 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
270 /* print event-declared event context */
271 if (event
->event_context
) {
272 ret
= generic_rw(pos
, &event
->event_context
->p
);
277 /* Read and print event payload */
278 if (likely(event
->event_fields
)) {
279 ret
= generic_rw(pos
, &event
->event_fields
->p
);
287 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
291 void ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
294 pos
->mmap_offset
= 0;
295 pos
->packet_size
= 0;
296 pos
->content_size
= 0;
297 pos
->content_size_loc
= NULL
;
303 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
304 sizeof(struct packet_index
));
306 pos
->packet_index
= NULL
;
307 switch (open_flags
& O_ACCMODE
) {
309 pos
->prot
= PROT_READ
;
310 pos
->flags
= MAP_PRIVATE
;
311 pos
->parent
.rw_table
= read_dispatch_table
;
312 pos
->parent
.event_cb
= ctf_read_event
;
315 pos
->prot
= PROT_WRITE
; /* Write has priority */
316 pos
->flags
= MAP_SHARED
;
317 pos
->parent
.rw_table
= write_dispatch_table
;
318 pos
->parent
.event_cb
= ctf_write_event
;
320 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
327 void ctf_fini_pos(struct ctf_stream_pos
*pos
)
331 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
332 *pos
->content_size_loc
= pos
->offset
;
335 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
337 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
342 (void) g_array_free(pos
->packet_index
, TRUE
);
345 void ctf_move_pos_slow(struct ctf_stream_pos
*pos
, size_t offset
, int whence
)
347 struct ctf_file_stream
*file_stream
=
348 container_of(pos
, struct ctf_file_stream
, pos
);
351 struct packet_index
*index
;
353 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
354 *pos
->content_size_loc
= pos
->offset
;
358 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
360 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
368 * The caller should never ask for ctf_move_pos across packets,
369 * except to get exactly at the beginning of the next packet.
371 if (pos
->prot
== PROT_WRITE
) {
374 /* The writer will add padding */
375 assert(pos
->offset
+ offset
== pos
->packet_size
);
376 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
379 assert(offset
== 0); /* only seek supported for now */
385 pos
->content_size
= -1U; /* Unknown at this point */
386 pos
->packet_size
= WRITE_PACKET_LEN
;
387 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
388 pos
->packet_size
/ CHAR_BIT
);
395 if (pos
->offset
== EOF
)
397 /* The reader will expect us to skip padding */
398 assert(pos
->offset
+ offset
== pos
->content_size
);
402 assert(offset
== 0); /* only seek supported for now */
408 if (pos
->cur_index
>= pos
->packet_index
->len
) {
412 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
414 pos
->mmap_offset
= index
->offset
;
416 /* Lookup context/packet size in index */
417 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
418 pos
->content_size
= index
->content_size
;
419 pos
->packet_size
= index
->packet_size
;
420 if (index
->data_offset
< index
->content_size
) {
421 pos
->offset
= 0; /* will read headers */
422 } else if (index
->data_offset
== index
->content_size
) {
424 pos
->offset
= index
->data_offset
;
427 goto read_next_packet
;
433 /* map new base. Need mapping length from header. */
434 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
435 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
436 if (pos
->base
== MAP_FAILED
) {
437 fprintf(stdout
, "[error] mmap error %s.\n",
442 /* update trace_packet_header and stream_packet_context */
443 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
444 /* Read packet header */
445 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
448 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
449 /* Read packet context */
450 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
456 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
462 len
= fread(&magic
, sizeof(magic
), 1, fp
);
466 if (magic
== TSDL_MAGIC
) {
468 td
->byte_order
= BYTE_ORDER
;
469 CTF_TRACE_SET_FIELD(td
, byte_order
);
470 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
472 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
473 LITTLE_ENDIAN
: BIG_ENDIAN
;
474 CTF_TRACE_SET_FIELD(td
, byte_order
);
482 * Returns 0 on success, -1 on error.
485 int check_version(unsigned int major
, unsigned int minor
)
500 /* eventually return an error instead of warning */
502 fprintf(stdout
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
508 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
511 struct metadata_packet_header header
;
512 size_t readlen
, writelen
, toread
;
513 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
516 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
520 if (td
->byte_order
!= BYTE_ORDER
) {
521 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
522 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
523 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
524 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
527 fprintf(stdout
, "[warning] checksum verification not supported yet.\n");
528 if (header
.compression_scheme
) {
529 fprintf(stdout
, "[error] compression (%u) not supported yet.\n",
530 header
.compression_scheme
);
533 if (header
.encryption_scheme
) {
534 fprintf(stdout
, "[error] encryption (%u) not supported yet.\n",
535 header
.encryption_scheme
);
538 if (header
.checksum_scheme
) {
539 fprintf(stdout
, "[error] checksum (%u) not supported yet.\n",
540 header
.checksum_scheme
);
543 if (check_version(header
.major
, header
.minor
) < 0)
545 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
546 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
547 CTF_TRACE_SET_FIELD(td
, uuid
);
549 if (uuid_compare(header
.uuid
, td
->uuid
))
553 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
556 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
561 if (babeltrace_debug
) {
563 fprintf(stdout
, "[debug] metadata packet read: %s\n",
567 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
568 if (writelen
< readlen
) {
578 ret
= 0; /* continue reading next packet */
585 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
586 ret
= fseek(in
, toread
, SEEK_CUR
);
588 fprintf(stdout
, "[warning] Missing padding at end of file\n");
595 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
604 * Using strlen on *buf instead of size of open_memstream
605 * because its size includes garbage at the end (after final
606 * \0). This is the allocated size, not the actual string size.
608 out
= open_memstream(buf
, &size
);
610 perror("Metadata open_memstream");
614 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
623 fclose(out
); /* flush the buffer */
625 /* open for reading */
626 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
628 perror("Metadata fmemopen");
635 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
636 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
637 int whence
), FILE *metadata_fp
)
639 struct ctf_scanner
*scanner
;
640 struct ctf_file_stream
*metadata_stream
;
645 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
648 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
650 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
658 td
->metadata
= &metadata_stream
->parent
;
659 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
660 if (metadata_stream
->pos
.fd
< 0) {
661 fprintf(stdout
, "Unable to open metadata.\n");
662 return metadata_stream
->pos
.fd
;
665 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
667 fprintf(stdout
, "[error] Unable to open metadata stream.\n");
668 perror("Metadata stream open");
673 if (babeltrace_debug
)
676 if (packet_metadata(td
, fp
)) {
677 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
679 goto end_packet_read
;
681 unsigned int major
, minor
;
684 td
->byte_order
= BYTE_ORDER
;
686 /* Check text-only metadata header and version */
687 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
689 fprintf(stdout
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
690 if (check_version(major
, minor
) < 0) {
692 goto end_packet_read
;
697 scanner
= ctf_scanner_alloc(fp
);
699 fprintf(stdout
, "[error] Error allocating scanner\n");
701 goto end_scanner_alloc
;
703 ret
= ctf_scanner_append_ast(scanner
);
705 fprintf(stdout
, "[error] Error creating AST\n");
709 if (babeltrace_debug
) {
710 ret
= ctf_visitor_print_xml(stdout
, 0, &scanner
->ast
->root
);
712 fprintf(stdout
, "[error] Error visiting AST for XML output\n");
717 ret
= ctf_visitor_semantic_check(stdout
, 0, &scanner
->ast
->root
);
719 fprintf(stdout
, "[error] Error in CTF semantic validation %d\n", ret
);
722 ret
= ctf_visitor_construct_metadata(stdout
, 0, &scanner
->ast
->root
,
725 fprintf(stdout
, "[error] Error in CTF metadata constructor %d\n", ret
);
729 ctf_scanner_free(scanner
);
735 close(metadata_stream
->pos
.fd
);
737 g_free(metadata_stream
);
742 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
743 struct ctf_stream
*stream
,
744 struct ctf_event
*event
)
746 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
748 if (event
->context_decl
) {
749 struct definition
*definition
=
750 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
751 stream
->parent_def_scope
, 0, 0, "event.context");
755 stream_event
->event_context
= container_of(definition
,
756 struct definition_struct
, p
);
757 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
759 if (event
->fields_decl
) {
760 struct definition
*definition
=
761 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
762 stream
->parent_def_scope
, 0, 0, "event.fields");
766 stream_event
->event_fields
= container_of(definition
,
767 struct definition_struct
, p
);
768 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
773 if (stream_event
->event_fields
)
774 definition_unref(&stream_event
->event_fields
->p
);
775 if (stream_event
->event_context
)
776 definition_unref(&stream_event
->event_context
->p
);
781 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
783 struct ctf_stream_class
*stream_class
;
787 if (stream
->stream_definitions_created
)
790 stream_class
= stream
->stream_class
;
792 if (stream_class
->packet_context_decl
) {
793 struct definition
*definition
=
794 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
795 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
800 stream
->stream_packet_context
= container_of(definition
,
801 struct definition_struct
, p
);
802 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
804 if (stream_class
->event_header_decl
) {
805 struct definition
*definition
=
806 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
807 stream
->parent_def_scope
, 0, 0, "stream.event.header");
812 stream
->stream_event_header
=
813 container_of(definition
, struct definition_struct
, p
);
814 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
816 if (stream_class
->event_context_decl
) {
817 struct definition
*definition
=
818 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
819 stream
->parent_def_scope
, 0, 0, "stream.event.context");
824 stream
->stream_event_context
=
825 container_of(definition
, struct definition_struct
, p
);
826 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
828 stream
->events_by_id
= g_ptr_array_new();
829 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
830 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
831 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
832 struct ctf_stream_event
*stream_event
;
836 stream_event
= create_event_definitions(td
, stream
, event
);
839 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
844 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
845 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
847 g_free(stream_event
);
849 g_ptr_array_free(stream
->events_by_id
, TRUE
);
851 if (stream
->stream_event_context
)
852 definition_unref(&stream
->stream_event_context
->p
);
853 if (stream
->stream_event_header
)
854 definition_unref(&stream
->stream_event_header
->p
);
855 if (stream
->stream_packet_context
)
856 definition_unref(&stream
->stream_packet_context
->p
);
862 int create_stream_packet_index(struct ctf_trace
*td
,
863 struct ctf_file_stream
*file_stream
)
865 struct ctf_stream_class
*stream
;
867 struct ctf_stream_pos
*pos
;
868 struct stat filestats
;
869 struct packet_index packet_index
;
870 int first_packet
= 1;
873 pos
= &file_stream
->pos
;
875 ret
= fstat(pos
->fd
, &filestats
);
879 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
882 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
883 uint64_t stream_id
= 0;
887 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
889 fprintf(stdout
, "[error] Unable to unmap old base: %s.\n",
895 /* map new base. Need mapping length from header. */
896 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
897 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
898 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
899 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
900 pos
->offset
= 0; /* Position of the packet header */
902 packet_index
.offset
= pos
->mmap_offset
;
903 packet_index
.content_size
= 0;
904 packet_index
.packet_size
= 0;
905 packet_index
.timestamp_begin
= 0;
906 packet_index
.timestamp_end
= 0;
908 /* read and check header, set stream id (and check) */
909 if (file_stream
->parent
.trace_packet_header
) {
910 /* Read packet header */
911 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
914 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
915 if (len_index
>= 0) {
916 struct definition
*field
;
919 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
920 magic
= get_unsigned_int(field
);
921 if (magic
!= CTF_MAGIC
) {
922 fprintf(stdout
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
924 file_stream
->pos
.packet_index
->len
,
925 (ssize_t
) pos
->mmap_offset
);
931 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
932 if (len_index
>= 0) {
933 struct definition_array
*defarray
;
934 struct definition
*field
;
936 uint8_t uuidval
[UUID_LEN
];
938 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
939 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
940 defarray
= container_of(field
, struct definition_array
, p
);
941 assert(array_len(defarray
) == UUID_LEN
);
943 for (i
= 0; i
< UUID_LEN
; i
++) {
944 struct definition
*elem
;
946 elem
= array_index(defarray
, i
);
947 uuidval
[i
] = get_unsigned_int(elem
);
949 ret
= uuid_compare(td
->uuid
, uuidval
);
951 fprintf(stdout
, "[error] Unique Universal Identifiers do not match.\n");
957 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
958 if (len_index
>= 0) {
959 struct definition
*field
;
961 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
962 stream_id
= get_unsigned_int(field
);
966 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
967 fprintf(stdout
, "[error] Stream ID is changing within a stream.\n");
971 file_stream
->parent
.stream_id
= stream_id
;
972 if (stream_id
>= td
->streams
->len
) {
973 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
976 stream
= g_ptr_array_index(td
->streams
, stream_id
);
978 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
981 file_stream
->parent
.stream_class
= stream
;
982 ret
= create_stream_definitions(td
, &file_stream
->parent
);
988 if (file_stream
->parent
.stream_packet_context
) {
989 /* Read packet context */
990 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
993 /* read content size from header */
994 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
995 if (len_index
>= 0) {
996 struct definition
*field
;
998 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
999 packet_index
.content_size
= get_unsigned_int(field
);
1001 /* Use file size for packet size */
1002 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1005 /* read packet size from header */
1006 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1007 if (len_index
>= 0) {
1008 struct definition
*field
;
1010 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1011 packet_index
.packet_size
= get_unsigned_int(field
);
1013 /* Use content size if non-zero, else file size */
1014 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1017 /* read timestamp begin from header */
1018 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1019 if (len_index
>= 0) {
1020 struct definition
*field
;
1022 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1023 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1026 /* read timestamp end from header */
1027 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1028 if (len_index
>= 0) {
1029 struct definition
*field
;
1031 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1032 packet_index
.timestamp_end
= get_unsigned_int(field
);
1035 /* Use file size for packet size */
1036 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1037 /* Use content size if non-zero, else file size */
1038 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1041 /* Validate content size and packet size values */
1042 if (packet_index
.content_size
> packet_index
.packet_size
) {
1043 fprintf(stdout
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1044 packet_index
.content_size
, packet_index
.packet_size
);
1048 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1049 fprintf(stdout
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1050 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1054 /* Save position after header and context */
1055 packet_index
.data_offset
= pos
->offset
;
1057 /* add index to packet array */
1058 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1060 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1063 /* Move pos back to beginning of file */
1064 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1070 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1074 if (td
->packet_header_decl
) {
1075 struct definition
*definition
=
1076 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1077 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1082 stream
->trace_packet_header
=
1083 container_of(definition
, struct definition_struct
, p
);
1084 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1094 * Note: many file streams can inherit from the same stream class
1095 * description (metadata).
1098 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1099 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1103 struct ctf_file_stream
*file_stream
;
1105 ret
= openat(td
->dirfd
, path
, flags
);
1107 perror("File stream openat()");
1110 file_stream
= g_new0(struct ctf_file_stream
, 1);
1112 if (move_pos_slow
) {
1113 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1115 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1120 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1121 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1124 ret
= create_stream_packet_index(td
, file_stream
);
1127 /* Add stream file to stream class */
1128 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1129 &file_stream
->parent
);
1133 if (file_stream
->parent
.trace_packet_header
)
1134 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1136 ctf_fini_pos(&file_stream
->pos
);
1137 close(file_stream
->pos
.fd
);
1138 g_free(file_stream
);
1144 init_domain_name(struct ctf_trace
*td
)
1148 start
= td
->path
+ strlen(td
->collection_path
);
1149 start
++; /* skip / */
1150 end
= strchr(start
, '/');
1153 memcpy(td
->domain
, start
, end
- start
);
1154 td
->domain
[end
- start
] = '\0';
1158 init_proc_name(struct ctf_trace
*td
)
1163 if (td
->domain
[0] == '\0')
1165 memcpy(buf
, td
->path
, PATH_MAX
);
1166 start
= buf
+ strlen(td
->collection_path
);
1167 start
++; /* skip / */
1168 start
= strchr(start
, '/'); /* get begin of domain content */
1171 start
++; /* skip / */
1172 /* find last -, skips time */
1173 end
= strrchr(start
, '-');
1177 /* find previous -, skips date */
1178 end
= strrchr(start
, '-');
1182 /* find previous -, skips pid */
1183 end
= strrchr(start
, '-');
1188 memcpy(td
->procname
, start
, end
- start
);
1189 td
->procname
[end
- start
] = '\0';
1193 init_vpid(struct ctf_trace
*td
)
1198 if (td
->domain
[0] == '\0')
1200 memcpy(buf
, td
->path
, PATH_MAX
);
1201 start
= buf
+ strlen(td
->collection_path
);
1202 start
++; /* skip / */
1203 start
= strchr(start
, '/'); /* get begin of domain content */
1206 start
++; /* skip / */
1207 /* find last -, skips time */
1208 end
= strrchr(start
, '-');
1212 /* find previous -, skips date */
1213 end
= strrchr(start
, '-');
1217 /* find previous -, skips pid */
1218 start
= strrchr(start
, '-');
1221 start
++; /* skip - */
1223 memcpy(td
->vpid
, start
, end
- start
);
1224 td
->vpid
[end
- start
] = '\0';
1228 int ctf_open_trace_read(struct ctf_trace
*td
, const char *collection_path
,
1229 const char *path
, int flags
,
1230 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1231 int whence
), FILE *metadata_fp
)
1234 struct dirent
*dirent
;
1235 struct dirent
*diriter
;
1240 /* Open trace directory */
1241 td
->dir
= opendir(path
);
1243 fprintf(stdout
, "[error] Unable to open trace directory.\n");
1248 td
->dirfd
= open(path
, 0);
1249 if (td
->dirfd
< 0) {
1250 fprintf(stdout
, "[error] Unable to open trace directory file descriptor.\n");
1251 perror("Trace directory open");
1255 strncpy(td
->collection_path
, collection_path
, PATH_MAX
);
1256 td
->collection_path
[PATH_MAX
- 1] = '\0';
1257 strncpy(td
->path
, path
, PATH_MAX
);
1258 td
->path
[PATH_MAX
- 1] = '\0';
1259 init_domain_name(td
);
1264 * Keep the metadata file separate.
1267 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1269 goto error_metadata
;
1273 * Open each stream: for each file, try to open, check magic
1274 * number, and get the stream ID to add to the right location in
1278 dirent_len
= offsetof(struct dirent
, d_name
) +
1279 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1281 dirent
= malloc(dirent_len
);
1284 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1286 fprintf(stdout
, "[error] Readdir error.\n");
1291 /* Ignore hidden files, ., .. and metadata. */
1292 if (!strncmp(diriter
->d_name
, ".", 1)
1293 || !strcmp(diriter
->d_name
, "..")
1294 || !strcmp(diriter
->d_name
, "metadata"))
1296 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1298 fprintf(stdout
, "[error] Open file stream error.\n");
1317 struct trace_descriptor
*ctf_open_trace(const char *collection_path
, const char *path
, int flags
,
1318 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1319 int whence
), FILE *metadata_fp
)
1321 struct ctf_trace
*td
;
1324 td
= g_new0(struct ctf_trace
, 1);
1326 switch (flags
& O_ACCMODE
) {
1329 fprintf(stdout
, "[error] Path missing for input CTF trace.\n");
1332 ret
= ctf_open_trace_read(td
, collection_path
, path
, flags
, move_pos_slow
, metadata_fp
);
1337 fprintf(stdout
, "[error] Opening CTF traces for output is not supported yet.\n");
1340 fprintf(stdout
, "[error] Incorrect open flags.\n");
1351 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1352 struct mmap_stream
*mmap_info
)
1354 pos
->mmap_offset
= 0;
1355 pos
->packet_size
= 0;
1356 pos
->content_size
= 0;
1357 pos
->content_size_loc
= NULL
;
1358 pos
->fd
= mmap_info
->fd
;
1363 pos
->packet_index
= NULL
;
1364 pos
->prot
= PROT_READ
;
1365 pos
->flags
= MAP_PRIVATE
;
1366 pos
->parent
.rw_table
= read_dispatch_table
;
1367 pos
->parent
.event_cb
= ctf_read_event
;
1371 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1372 struct ctf_file_stream
*file_stream
)
1374 struct ctf_stream_class
*stream
;
1375 uint64_t stream_id
= 0;
1378 file_stream
->parent
.stream_id
= stream_id
;
1379 if (stream_id
>= td
->streams
->len
) {
1380 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared "
1381 "in metadata.\n", stream_id
);
1385 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1387 fprintf(stdout
, "[error] Stream %" PRIu64
" is not declared "
1388 "in metadata.\n", stream_id
);
1392 file_stream
->parent
.stream_class
= stream
;
1393 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1399 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1400 struct mmap_stream
*mmap_info
,
1401 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1405 struct ctf_file_stream
*file_stream
;
1407 file_stream
= g_new0(struct ctf_file_stream
, 1);
1408 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1410 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1412 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1417 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1421 /* Add stream file to stream class */
1422 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1423 &file_stream
->parent
);
1427 if (file_stream
->parent
.trace_packet_header
)
1428 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1430 g_free(file_stream
);
1434 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1435 struct mmap_stream_list
*mmap_list
,
1436 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1441 struct mmap_stream
*mmap_info
;
1443 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1449 * for each stream, try to open, check magic number, and get the
1450 * stream ID to add to the right location in the stream array.
1452 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1453 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1455 fprintf(stdout
, "[error] Open file mmap stream error.\n");
1467 struct trace_descriptor
*ctf_open_mmap_trace(
1468 struct mmap_stream_list
*mmap_list
,
1469 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1472 struct ctf_trace
*td
;
1476 fprintf(stderr
, "[error] No metadata file pointer associated, "
1477 "required for mmap parsing\n");
1480 if (!move_pos_slow
) {
1481 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1484 td
= g_new0(struct ctf_trace
, 1);
1485 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1498 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1500 ctf_fini_pos(&file_stream
->pos
);
1501 close(file_stream
->pos
.fd
);
1505 void ctf_close_trace(struct trace_descriptor
*tdp
)
1507 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1511 for (i
= 0; i
< td
->streams
->len
; i
++) {
1512 struct ctf_stream_class
*stream
;
1515 stream
= g_ptr_array_index(td
->streams
, i
);
1518 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1519 struct ctf_file_stream
*file_stream
;
1520 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1521 ctf_close_file_stream(file_stream
);
1525 g_ptr_array_free(td
->streams
, TRUE
);
1531 void __attribute__((constructor
)) ctf_init(void)
1535 ctf_format
.name
= g_quark_from_static_string("ctf");
1536 ret
= bt_register_format(&ctf_format
);
1540 /* TODO: finalize */