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 /* Print lost 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 if (pos
->cur_index
> 0) {
404 index
= &g_array_index(pos
->packet_index
,
407 events_discarded_diff
-= index
->events_discarded
;
409 file_stream
->parent
.events_discarded
= events_discarded_diff
;
410 if (pos
->offset
== EOF
)
412 /* The reader will expect us to skip padding */
413 assert(pos
->offset
+ offset
== pos
->content_size
);
415 file_stream
->parent
.prev_timestamp
= file_stream
->parent
.timestamp
;
419 assert(offset
== 0); /* only seek supported for now */
421 file_stream
->parent
.prev_timestamp
= 0;
426 if (pos
->cur_index
>= pos
->packet_index
->len
) {
430 index
= &g_array_index(pos
->packet_index
, struct packet_index
,
432 pos
->mmap_offset
= index
->offset
;
434 /* Lookup context/packet size in index */
435 file_stream
->parent
.timestamp
= index
->timestamp_begin
;
436 pos
->content_size
= index
->content_size
;
437 pos
->packet_size
= index
->packet_size
;
438 if (index
->data_offset
< index
->content_size
) {
439 pos
->offset
= 0; /* will read headers */
440 } else if (index
->data_offset
== index
->content_size
) {
442 pos
->offset
= index
->data_offset
;
445 goto read_next_packet
;
451 /* map new base. Need mapping length from header. */
452 pos
->base
= mmap(NULL
, pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
453 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
454 if (pos
->base
== MAP_FAILED
) {
455 fprintf(stderr
, "[error] mmap error %s.\n",
460 /* update trace_packet_header and stream_packet_context */
461 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
462 /* Read packet header */
463 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
466 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
467 /* Read packet context */
468 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
474 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
480 len
= fread(&magic
, sizeof(magic
), 1, fp
);
484 if (magic
== TSDL_MAGIC
) {
486 td
->byte_order
= BYTE_ORDER
;
487 CTF_TRACE_SET_FIELD(td
, byte_order
);
488 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
490 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
491 LITTLE_ENDIAN
: BIG_ENDIAN
;
492 CTF_TRACE_SET_FIELD(td
, byte_order
);
500 * Returns 0 on success, -1 on error.
503 int check_version(unsigned int major
, unsigned int minor
)
518 /* eventually return an error instead of warning */
520 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
526 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
529 struct metadata_packet_header header
;
530 size_t readlen
, writelen
, toread
;
531 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
534 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
538 if (td
->byte_order
!= BYTE_ORDER
) {
539 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
540 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
541 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
542 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
545 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
546 if (header
.compression_scheme
) {
547 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
548 header
.compression_scheme
);
551 if (header
.encryption_scheme
) {
552 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
553 header
.encryption_scheme
);
556 if (header
.checksum_scheme
) {
557 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
558 header
.checksum_scheme
);
561 if (check_version(header
.major
, header
.minor
) < 0)
563 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
564 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
565 CTF_TRACE_SET_FIELD(td
, uuid
);
567 if (uuid_compare(header
.uuid
, td
->uuid
))
571 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
574 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
579 if (babeltrace_debug
) {
581 fprintf(stderr
, "[debug] metadata packet read: %s\n",
585 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
586 if (writelen
< readlen
) {
596 ret
= 0; /* continue reading next packet */
603 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
604 ret
= fseek(in
, toread
, SEEK_CUR
);
606 fprintf(stderr
, "[warning] Missing padding at end of file\n");
613 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
622 * Using strlen on *buf instead of size of open_memstream
623 * because its size includes garbage at the end (after final
624 * \0). This is the allocated size, not the actual string size.
626 out
= open_memstream(buf
, &size
);
628 perror("Metadata open_memstream");
632 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
641 fclose(out
); /* flush the buffer */
643 /* open for reading */
644 *fp
= fmemopen(*buf
, strlen(*buf
), "rb");
646 perror("Metadata fmemopen");
653 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
654 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
655 int whence
), FILE *metadata_fp
)
657 struct ctf_scanner
*scanner
;
658 struct ctf_file_stream
*metadata_stream
;
663 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
666 metadata_stream
->pos
.move_pos_slow
= move_pos_slow
;
668 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
676 td
->metadata
= &metadata_stream
->parent
;
677 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
678 if (metadata_stream
->pos
.fd
< 0) {
679 fprintf(stderr
, "Unable to open metadata.\n");
680 return metadata_stream
->pos
.fd
;
683 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
685 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
686 perror("Metadata stream open");
691 if (babeltrace_debug
)
694 if (packet_metadata(td
, fp
)) {
695 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
697 goto end_packet_read
;
699 unsigned int major
, minor
;
702 td
->byte_order
= BYTE_ORDER
;
704 /* Check text-only metadata header and version */
705 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
707 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
708 if (check_version(major
, minor
) < 0) {
710 goto end_packet_read
;
715 scanner
= ctf_scanner_alloc(fp
);
717 fprintf(stderr
, "[error] Error allocating scanner\n");
719 goto end_scanner_alloc
;
721 ret
= ctf_scanner_append_ast(scanner
);
723 fprintf(stderr
, "[error] Error creating AST\n");
727 if (babeltrace_debug
) {
728 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
730 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
735 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
737 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
740 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
743 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
747 ctf_scanner_free(scanner
);
753 close(metadata_stream
->pos
.fd
);
755 g_free(metadata_stream
);
760 struct ctf_stream_event
*create_event_definitions(struct ctf_trace
*td
,
761 struct ctf_stream
*stream
,
762 struct ctf_event
*event
)
764 struct ctf_stream_event
*stream_event
= g_new0(struct ctf_stream_event
, 1);
766 if (event
->context_decl
) {
767 struct definition
*definition
=
768 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
769 stream
->parent_def_scope
, 0, 0, "event.context");
773 stream_event
->event_context
= container_of(definition
,
774 struct definition_struct
, p
);
775 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
777 if (event
->fields_decl
) {
778 struct definition
*definition
=
779 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
780 stream
->parent_def_scope
, 0, 0, "event.fields");
784 stream_event
->event_fields
= container_of(definition
,
785 struct definition_struct
, p
);
786 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
791 if (stream_event
->event_fields
)
792 definition_unref(&stream_event
->event_fields
->p
);
793 if (stream_event
->event_context
)
794 definition_unref(&stream_event
->event_context
->p
);
799 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
801 struct ctf_stream_class
*stream_class
;
805 if (stream
->stream_definitions_created
)
808 stream_class
= stream
->stream_class
;
810 if (stream_class
->packet_context_decl
) {
811 struct definition
*definition
=
812 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
813 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
818 stream
->stream_packet_context
= container_of(definition
,
819 struct definition_struct
, p
);
820 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
822 if (stream_class
->event_header_decl
) {
823 struct definition
*definition
=
824 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
825 stream
->parent_def_scope
, 0, 0, "stream.event.header");
830 stream
->stream_event_header
=
831 container_of(definition
, struct definition_struct
, p
);
832 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
834 if (stream_class
->event_context_decl
) {
835 struct definition
*definition
=
836 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
837 stream
->parent_def_scope
, 0, 0, "stream.event.context");
842 stream
->stream_event_context
=
843 container_of(definition
, struct definition_struct
, p
);
844 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
846 stream
->events_by_id
= g_ptr_array_new();
847 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
848 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
849 struct ctf_event
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
850 struct ctf_stream_event
*stream_event
;
854 stream_event
= create_event_definitions(td
, stream
, event
);
857 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
862 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
863 struct ctf_stream_event
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
865 g_free(stream_event
);
867 g_ptr_array_free(stream
->events_by_id
, TRUE
);
869 if (stream
->stream_event_context
)
870 definition_unref(&stream
->stream_event_context
->p
);
871 if (stream
->stream_event_header
)
872 definition_unref(&stream
->stream_event_header
->p
);
873 if (stream
->stream_packet_context
)
874 definition_unref(&stream
->stream_packet_context
->p
);
880 int create_stream_packet_index(struct ctf_trace
*td
,
881 struct ctf_file_stream
*file_stream
)
883 struct ctf_stream_class
*stream
;
885 struct ctf_stream_pos
*pos
;
886 struct stat filestats
;
887 struct packet_index packet_index
;
888 int first_packet
= 1;
891 pos
= &file_stream
->pos
;
893 ret
= fstat(pos
->fd
, &filestats
);
897 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
900 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
901 uint64_t stream_id
= 0;
905 ret
= munmap(pos
->base
, pos
->packet_size
/ CHAR_BIT
);
907 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
913 /* map new base. Need mapping length from header. */
914 pos
->base
= mmap(NULL
, MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
915 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
916 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
917 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
918 pos
->offset
= 0; /* Position of the packet header */
920 packet_index
.offset
= pos
->mmap_offset
;
921 packet_index
.content_size
= 0;
922 packet_index
.packet_size
= 0;
923 packet_index
.timestamp_begin
= 0;
924 packet_index
.timestamp_end
= 0;
925 packet_index
.events_discarded
= 0;
927 /* read and check header, set stream id (and check) */
928 if (file_stream
->parent
.trace_packet_header
) {
929 /* Read packet header */
930 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
933 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
934 if (len_index
>= 0) {
935 struct definition
*field
;
938 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
939 magic
= get_unsigned_int(field
);
940 if (magic
!= CTF_MAGIC
) {
941 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
943 file_stream
->pos
.packet_index
->len
,
944 (ssize_t
) pos
->mmap_offset
);
950 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
951 if (len_index
>= 0) {
952 struct definition_array
*defarray
;
953 struct definition
*field
;
955 uint8_t uuidval
[UUID_LEN
];
957 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
958 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
959 defarray
= container_of(field
, struct definition_array
, p
);
960 assert(array_len(defarray
) == UUID_LEN
);
962 for (i
= 0; i
< UUID_LEN
; i
++) {
963 struct definition
*elem
;
965 elem
= array_index(defarray
, i
);
966 uuidval
[i
] = get_unsigned_int(elem
);
968 ret
= uuid_compare(td
->uuid
, uuidval
);
970 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
976 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
977 if (len_index
>= 0) {
978 struct definition
*field
;
980 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
981 stream_id
= get_unsigned_int(field
);
985 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
986 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
990 file_stream
->parent
.stream_id
= stream_id
;
991 if (stream_id
>= td
->streams
->len
) {
992 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
995 stream
= g_ptr_array_index(td
->streams
, stream_id
);
997 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1000 file_stream
->parent
.stream_class
= stream
;
1001 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1007 if (file_stream
->parent
.stream_packet_context
) {
1008 /* Read packet context */
1009 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1012 /* read content size from header */
1013 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1014 if (len_index
>= 0) {
1015 struct definition
*field
;
1017 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1018 packet_index
.content_size
= get_unsigned_int(field
);
1020 /* Use file size for packet size */
1021 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1024 /* read packet size from header */
1025 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1026 if (len_index
>= 0) {
1027 struct definition
*field
;
1029 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1030 packet_index
.packet_size
= get_unsigned_int(field
);
1032 /* Use content size if non-zero, else file size */
1033 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1036 /* read timestamp begin from header */
1037 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1038 if (len_index
>= 0) {
1039 struct definition
*field
;
1041 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1042 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1045 /* read timestamp end from header */
1046 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1047 if (len_index
>= 0) {
1048 struct definition
*field
;
1050 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1051 packet_index
.timestamp_end
= get_unsigned_int(field
);
1054 /* read events discarded from header */
1055 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1056 if (len_index
>= 0) {
1057 struct definition
*field
;
1059 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1060 packet_index
.events_discarded
= get_unsigned_int(field
);
1063 /* Use file size for packet size */
1064 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1065 /* Use content size if non-zero, else file size */
1066 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1069 /* Validate content size and packet size values */
1070 if (packet_index
.content_size
> packet_index
.packet_size
) {
1071 fprintf(stderr
, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1072 packet_index
.content_size
, packet_index
.packet_size
);
1076 if (packet_index
.packet_size
> (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1077 fprintf(stderr
, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1078 packet_index
.content_size
, (size_t) (filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1082 /* Save position after header and context */
1083 packet_index
.data_offset
= pos
->offset
;
1085 /* add index to packet array */
1086 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1088 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1091 /* Move pos back to beginning of file */
1092 ctf_move_pos_slow(pos
, 0, SEEK_SET
); /* position for write */
1098 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream
*stream
)
1102 if (td
->packet_header_decl
) {
1103 struct definition
*definition
=
1104 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1105 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1110 stream
->trace_packet_header
=
1111 container_of(definition
, struct definition_struct
, p
);
1112 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1122 * Note: many file streams can inherit from the same stream class
1123 * description (metadata).
1126 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1127 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1131 struct ctf_file_stream
*file_stream
;
1133 ret
= openat(td
->dirfd
, path
, flags
);
1135 perror("File stream openat()");
1138 file_stream
= g_new0(struct ctf_file_stream
, 1);
1140 if (move_pos_slow
) {
1141 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1143 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1148 ctf_init_pos(&file_stream
->pos
, ret
, flags
);
1149 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1152 ret
= create_stream_packet_index(td
, file_stream
);
1155 /* Add stream file to stream class */
1156 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1157 &file_stream
->parent
);
1161 if (file_stream
->parent
.trace_packet_header
)
1162 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1164 ctf_fini_pos(&file_stream
->pos
);
1165 close(file_stream
->pos
.fd
);
1166 g_free(file_stream
);
1172 init_domain_name(struct ctf_trace
*td
)
1176 start
= td
->path
+ strlen(td
->collection_path
);
1177 while (start
[0] == '/')
1178 start
++; /* skip / */
1179 end
= strchr(start
, '/');
1181 end
= start
+ strlen(start
);
1182 memcpy(td
->domain
, start
, end
- start
);
1183 td
->domain
[end
- start
] = '\0';
1187 init_proc_name(struct ctf_trace
*td
)
1192 if (td
->domain
[0] == '\0')
1194 memcpy(buf
, td
->path
, PATH_MAX
);
1195 start
= buf
+ strlen(td
->collection_path
);
1196 while (start
[0] == '/')
1197 start
++; /* skip / */
1198 start
= strchr(start
, '/'); /* get begin of domain content */
1201 while (start
[0] == '/')
1202 start
++; /* skip / */
1203 /* find last -, skips time */
1204 end
= strrchr(start
, '-');
1208 /* find previous -, skips date */
1209 end
= strrchr(start
, '-');
1213 /* find previous -, skips pid */
1214 end
= strrchr(start
, '-');
1219 memcpy(td
->procname
, start
, end
- start
);
1220 td
->procname
[end
- start
] = '\0';
1224 init_vpid(struct ctf_trace
*td
)
1229 if (td
->domain
[0] == '\0')
1231 memcpy(buf
, td
->path
, PATH_MAX
);
1232 start
= buf
+ strlen(td
->collection_path
);
1233 while (start
[0] == '/')
1234 start
++; /* skip / */
1235 start
= strchr(start
, '/'); /* get begin of domain content */
1238 while (start
[0] == '/')
1239 start
++; /* skip / */
1240 /* find last -, skips time */
1241 end
= strrchr(start
, '-');
1245 /* find previous -, skips date */
1246 end
= strrchr(start
, '-');
1250 /* find previous -, skips pid */
1251 start
= strrchr(start
, '-');
1254 start
++; /* skip - */
1256 memcpy(td
->vpid
, start
, end
- start
);
1257 td
->vpid
[end
- start
] = '\0';
1261 int ctf_open_trace_read(struct ctf_trace
*td
, const char *collection_path
,
1262 const char *path
, int flags
,
1263 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1264 int whence
), FILE *metadata_fp
)
1267 struct dirent
*dirent
;
1268 struct dirent
*diriter
;
1270 char *respath
, *rescolpath
;
1274 /* Open trace directory */
1275 td
->dir
= opendir(path
);
1277 fprintf(stderr
, "[error] Unable to open trace directory.\n");
1282 td
->dirfd
= open(path
, 0);
1283 if (td
->dirfd
< 0) {
1284 fprintf(stderr
, "[error] Unable to open trace directory file descriptor.\n");
1285 perror("Trace directory open");
1289 rescolpath
= realpath(collection_path
, td
->collection_path
);
1291 fprintf(stderr
, "[error] collection path resolution failure\n");
1294 respath
= realpath(path
, td
->path
);
1296 fprintf(stderr
, "[error] path resolution failure\n");
1299 init_domain_name(td
);
1304 * Keep the metadata file separate.
1307 ret
= ctf_open_trace_metadata_read(td
, move_pos_slow
, metadata_fp
);
1309 goto error_metadata
;
1313 * Open each stream: for each file, try to open, check magic
1314 * number, and get the stream ID to add to the right location in
1318 dirent_len
= offsetof(struct dirent
, d_name
) +
1319 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1321 dirent
= malloc(dirent_len
);
1324 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1326 fprintf(stderr
, "[error] Readdir error.\n");
1331 /* Ignore hidden files, ., .. and metadata. */
1332 if (!strncmp(diriter
->d_name
, ".", 1)
1333 || !strcmp(diriter
->d_name
, "..")
1334 || !strcmp(diriter
->d_name
, "metadata"))
1336 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
, flags
, move_pos_slow
);
1338 fprintf(stderr
, "[error] Open file stream error.\n");
1357 struct trace_descriptor
*ctf_open_trace(const char *collection_path
, const char *path
, int flags
,
1358 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1359 int whence
), FILE *metadata_fp
)
1361 struct ctf_trace
*td
;
1364 td
= g_new0(struct ctf_trace
, 1);
1366 switch (flags
& O_ACCMODE
) {
1369 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1372 ret
= ctf_open_trace_read(td
, collection_path
, path
, flags
, move_pos_slow
, metadata_fp
);
1377 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1380 fprintf(stderr
, "[error] Incorrect open flags.\n");
1391 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1392 struct mmap_stream
*mmap_info
)
1394 pos
->mmap_offset
= 0;
1395 pos
->packet_size
= 0;
1396 pos
->content_size
= 0;
1397 pos
->content_size_loc
= NULL
;
1398 pos
->fd
= mmap_info
->fd
;
1403 pos
->packet_index
= NULL
;
1404 pos
->prot
= PROT_READ
;
1405 pos
->flags
= MAP_PRIVATE
;
1406 pos
->parent
.rw_table
= read_dispatch_table
;
1407 pos
->parent
.event_cb
= ctf_read_event
;
1411 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1412 struct ctf_file_stream
*file_stream
)
1414 struct ctf_stream_class
*stream
;
1415 uint64_t stream_id
= 0;
1418 file_stream
->parent
.stream_id
= stream_id
;
1419 if (stream_id
>= td
->streams
->len
) {
1420 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1421 "in metadata.\n", stream_id
);
1425 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1427 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1428 "in metadata.\n", stream_id
);
1432 file_stream
->parent
.stream_class
= stream
;
1433 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1439 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1440 struct mmap_stream
*mmap_info
,
1441 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1445 struct ctf_file_stream
*file_stream
;
1447 file_stream
= g_new0(struct ctf_file_stream
, 1);
1448 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1450 file_stream
->pos
.move_pos_slow
= move_pos_slow
;
1452 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1457 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1461 /* Add stream file to stream class */
1462 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1463 &file_stream
->parent
);
1467 if (file_stream
->parent
.trace_packet_header
)
1468 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1470 g_free(file_stream
);
1474 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1475 struct mmap_stream_list
*mmap_list
,
1476 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
1481 struct mmap_stream
*mmap_info
;
1483 ret
= ctf_open_trace_metadata_read(td
, ctf_move_pos_slow
, metadata_fp
);
1489 * for each stream, try to open, check magic number, and get the
1490 * stream ID to add to the right location in the stream array.
1492 cds_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1493 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, move_pos_slow
);
1495 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1507 struct trace_descriptor
*ctf_open_mmap_trace(
1508 struct mmap_stream_list
*mmap_list
,
1509 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
, int whence
),
1512 struct ctf_trace
*td
;
1516 fprintf(stderr
, "[error] No metadata file pointer associated, "
1517 "required for mmap parsing\n");
1520 if (!move_pos_slow
) {
1521 fprintf(stderr
, "[error] move_pos_slow function undefined.\n");
1524 td
= g_new0(struct ctf_trace
, 1);
1525 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, move_pos_slow
, metadata_fp
);
1538 void ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1540 ctf_fini_pos(&file_stream
->pos
);
1541 close(file_stream
->pos
.fd
);
1545 void ctf_close_trace(struct trace_descriptor
*tdp
)
1547 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1551 for (i
= 0; i
< td
->streams
->len
; i
++) {
1552 struct ctf_stream_class
*stream
;
1555 stream
= g_ptr_array_index(td
->streams
, i
);
1558 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1559 struct ctf_file_stream
*file_stream
;
1560 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
), struct ctf_file_stream
, parent
);
1561 ctf_close_file_stream(file_stream
);
1565 g_ptr_array_free(td
->streams
, TRUE
);
1571 void __attribute__((constructor
)) ctf_init(void)
1575 ctf_format
.name
= g_quark_from_static_string("ctf");
1576 ret
= bt_register_format(&ctf_format
);
1580 /* TODO: finalize */