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.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <babeltrace/trace-handle-internal.h>
35 #include <babeltrace/context-internal.h>
36 #include <babeltrace/uuid.h>
37 #include <babeltrace/endian.h>
42 #include <sys/types.h>
50 #include "metadata/ctf-scanner.h"
51 #include "metadata/ctf-parser.h"
52 #include "metadata/ctf-ast.h"
53 #include "events-private.h"
54 #include "memstream.h"
56 #define LOG2_CHAR_BIT 3
59 * Length of first attempt at mapping a packet header, in bits.
61 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
64 * Lenght of packet to write, in bits.
66 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
69 #define min(a, b) (((a) < (b)) ? (a) : (b))
72 #define NSEC_PER_SEC 1000000000ULL
79 uint64_t opt_clock_offset
;
80 uint64_t opt_clock_offset_ns
;
85 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
86 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
90 struct bt_trace_descriptor
*ctf_open_mmap_trace(
91 struct bt_mmap_stream_list
*mmap_list
,
92 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
96 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
97 struct bt_context
*ctx
);
99 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
100 struct bt_trace_handle
*handle
);
103 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
105 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
106 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
108 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
109 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
111 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
114 rw_dispatch read_dispatch_table
[] = {
115 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
116 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
117 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
118 [ CTF_TYPE_STRING
] = ctf_string_read
,
119 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
120 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
121 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
122 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
126 rw_dispatch write_dispatch_table
[] = {
127 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
128 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
129 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
130 [ CTF_TYPE_STRING
] = ctf_string_write
,
131 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
132 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
133 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
134 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
138 struct bt_format ctf_format
= {
139 .open_trace
= ctf_open_trace
,
140 .open_mmap_trace
= ctf_open_mmap_trace
,
141 .close_trace
= ctf_close_trace
,
142 .set_context
= ctf_set_context
,
143 .set_handle
= ctf_set_handle
,
144 .timestamp_begin
= ctf_timestamp_begin
,
145 .timestamp_end
= ctf_timestamp_end
,
146 .convert_index_timestamp
= ctf_convert_index_timestamp
,
150 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
151 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
153 struct ctf_trace
*tin
;
154 uint64_t begin
= ULLONG_MAX
;
157 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
162 /* for each stream_class */
163 for (i
= 0; i
< tin
->streams
->len
; i
++) {
164 struct ctf_stream_declaration
*stream_class
;
166 stream_class
= g_ptr_array_index(tin
->streams
, i
);
169 /* for each file_stream */
170 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
171 struct ctf_stream_definition
*stream
;
172 struct ctf_file_stream
*cfs
;
173 struct ctf_stream_pos
*stream_pos
;
174 struct packet_index
*index
;
176 stream
= g_ptr_array_index(stream_class
->streams
, j
);
177 cfs
= container_of(stream
, struct ctf_file_stream
,
179 stream_pos
= &cfs
->pos
;
181 if (!stream_pos
->packet_real_index
)
184 if (stream_pos
->packet_real_index
->len
<= 0)
187 if (type
== BT_CLOCK_REAL
) {
188 index
= &g_array_index(stream_pos
->packet_real_index
,
190 stream_pos
->packet_real_index
->len
- 1);
191 } else if (type
== BT_CLOCK_CYCLES
) {
192 index
= &g_array_index(stream_pos
->packet_cycles_index
,
194 stream_pos
->packet_real_index
->len
- 1);
199 if (index
->timestamp_begin
< begin
)
200 begin
= index
->timestamp_begin
;
211 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
212 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
214 struct ctf_trace
*tin
;
218 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
223 /* for each stream_class */
224 for (i
= 0; i
< tin
->streams
->len
; i
++) {
225 struct ctf_stream_declaration
*stream_class
;
227 stream_class
= g_ptr_array_index(tin
->streams
, i
);
230 /* for each file_stream */
231 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
232 struct ctf_stream_definition
*stream
;
233 struct ctf_file_stream
*cfs
;
234 struct ctf_stream_pos
*stream_pos
;
235 struct packet_index
*index
;
237 stream
= g_ptr_array_index(stream_class
->streams
, j
);
238 cfs
= container_of(stream
, struct ctf_file_stream
,
240 stream_pos
= &cfs
->pos
;
242 if (!stream_pos
->packet_real_index
)
245 if (stream_pos
->packet_real_index
->len
<= 0)
248 if (type
== BT_CLOCK_REAL
) {
249 index
= &g_array_index(stream_pos
->packet_real_index
,
251 stream_pos
->packet_real_index
->len
- 1);
252 } else if (type
== BT_CLOCK_CYCLES
) {
253 index
= &g_array_index(stream_pos
->packet_cycles_index
,
255 stream_pos
->packet_real_index
->len
- 1);
260 if (index
->timestamp_end
> end
)
261 end
= index
->timestamp_end
;
272 * Update stream current timestamp
275 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
276 struct definition_integer
*integer_definition
)
278 struct declaration_integer
*integer_declaration
=
279 integer_definition
->declaration
;
280 uint64_t oldval
, newval
, updateval
;
282 if (unlikely(integer_declaration
->len
== 64)) {
283 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
284 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
285 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
286 stream
->prev_cycles_timestamp
);
287 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
288 stream
->cycles_timestamp
);
292 oldval
= stream
->cycles_timestamp
;
293 oldval
&= (1ULL << integer_declaration
->len
) - 1;
294 newval
= integer_definition
->value
._unsigned
;
295 /* Test for overflow by comparing low bits */
297 newval
+= 1ULL << integer_declaration
->len
;
298 /* updateval contains old high bits, and new low bits (sum) */
299 updateval
= stream
->cycles_timestamp
;
300 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
302 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
303 stream
->cycles_timestamp
= updateval
;
305 /* convert to real timestamp */
306 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
307 stream
->prev_cycles_timestamp
);
308 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
309 stream
->cycles_timestamp
);
313 * Print timestamp, rescaling clock frequency to nanoseconds and
314 * applying offsets as needed (unix time).
317 void ctf_print_timestamp_real(FILE *fp
,
318 struct ctf_stream_definition
*stream
,
321 uint64_t ts_sec
= 0, ts_nsec
;
325 /* Add command-line offset in ns*/
326 ts_nsec
+= opt_clock_offset_ns
;
328 /* Add command-line offset */
329 ts_sec
+= opt_clock_offset
;
331 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
332 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
334 if (!opt_clock_seconds
) {
336 time_t time_s
= (time_t) ts_sec
;
338 if (!opt_clock_gmt
) {
341 res
= localtime_r(&time_s
, &tm
);
343 fprintf(stderr
, "[warning] Unable to get localtime.\n");
349 res
= gmtime_r(&time_s
, &tm
);
351 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
355 if (opt_clock_date
) {
359 /* Print date and time */
360 res
= strftime(timestr
, sizeof(timestr
),
363 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
366 fprintf(fp
, "%s", timestr
);
368 /* Print time in HH:MM:SS.ns */
369 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
370 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
374 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
382 * Print timestamp, in cycles
385 void ctf_print_timestamp_cycles(FILE *fp
,
386 struct ctf_stream_definition
*stream
,
389 fprintf(fp
, "%020" PRIu64
, timestamp
);
392 void ctf_print_timestamp(FILE *fp
,
393 struct ctf_stream_definition
*stream
,
396 if (opt_clock_cycles
) {
397 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
399 ctf_print_timestamp_real(fp
, stream
, timestamp
);
404 void print_uuid(FILE *fp
, unsigned char *uuid
)
408 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
409 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
412 void ctf_print_discarded(FILE *fp
, struct ctf_stream_definition
*stream
,
415 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events %sbetween [",
416 stream
->events_discarded
,
417 end_stream
? "at end of stream " : "");
418 if (opt_clock_cycles
) {
419 ctf_print_timestamp(fp
, stream
,
420 stream
->prev_cycles_timestamp
);
421 fprintf(fp
, "] and [");
422 ctf_print_timestamp(fp
, stream
,
423 stream
->prev_cycles_timestamp_end
);
425 ctf_print_timestamp(fp
, stream
,
426 stream
->prev_real_timestamp
);
427 fprintf(fp
, "] and [");
428 ctf_print_timestamp(fp
, stream
,
429 stream
->prev_real_timestamp_end
);
431 fprintf(fp
, "] in trace UUID ");
432 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
433 if (stream
->stream_class
->trace
->parent
.path
[0])
434 fprintf(fp
, ", at path: \"%s\"",
435 stream
->stream_class
->trace
->parent
.path
);
437 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
439 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
441 fprintf(fp
, "You should consider recording a new trace with larger "
442 "buffers or with fewer events enabled.\n");
447 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
449 struct ctf_stream_pos
*pos
=
450 container_of(ppos
, struct ctf_stream_pos
, parent
);
451 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
452 struct ctf_event_definition
*event
;
456 /* We need to check for EOF here for empty files. */
457 if (unlikely(pos
->offset
== EOF
))
460 ctf_pos_get_event(pos
);
462 /* save the current position as a restore point */
463 pos
->last_offset
= pos
->offset
;
466 * This is the EOF check after we've advanced the position in
469 if (unlikely(pos
->offset
== EOF
))
471 assert(pos
->offset
< pos
->content_size
);
473 /* Read event header */
474 if (likely(stream
->stream_event_header
)) {
475 struct definition_integer
*integer_definition
;
476 struct bt_definition
*variant
;
478 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
481 /* lookup event id */
482 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
483 if (integer_definition
) {
484 id
= integer_definition
->value
._unsigned
;
486 struct definition_enum
*enum_definition
;
488 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
489 if (enum_definition
) {
490 id
= enum_definition
->integer
->value
._unsigned
;
494 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
496 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
497 if (integer_definition
) {
498 id
= integer_definition
->value
._unsigned
;
501 stream
->event_id
= id
;
503 /* lookup timestamp */
504 stream
->has_timestamp
= 0;
505 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
506 if (integer_definition
) {
507 ctf_update_timestamp(stream
, integer_definition
);
508 stream
->has_timestamp
= 1;
511 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
512 if (integer_definition
) {
513 ctf_update_timestamp(stream
, integer_definition
);
514 stream
->has_timestamp
= 1;
520 /* Read stream-declared event context */
521 if (stream
->stream_event_context
) {
522 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
527 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
528 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
531 event
= g_ptr_array_index(stream
->events_by_id
, id
);
532 if (unlikely(!event
)) {
533 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
537 /* Read event-declared event context */
538 if (event
->event_context
) {
539 ret
= generic_rw(ppos
, &event
->event_context
->p
);
544 /* Read event payload */
545 if (likely(event
->event_fields
)) {
546 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
554 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
559 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
561 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
562 struct ctf_event_definition
*event
;
566 id
= stream
->event_id
;
568 /* print event header */
569 if (likely(stream
->stream_event_header
)) {
570 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
575 /* print stream-declared event context */
576 if (stream
->stream_event_context
) {
577 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
582 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
583 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
586 event
= g_ptr_array_index(stream
->events_by_id
, id
);
587 if (unlikely(!event
)) {
588 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
592 /* print event-declared event context */
593 if (event
->event_context
) {
594 ret
= generic_rw(pos
, &event
->event_context
->p
);
599 /* Read and print event payload */
600 if (likely(event
->event_fields
)) {
601 ret
= generic_rw(pos
, &event
->event_fields
->p
);
609 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
613 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
614 int fd
, int open_flags
)
618 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
619 sizeof(struct packet_index
));
620 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
621 sizeof(struct packet_index
));
623 pos
->packet_cycles_index
= NULL
;
624 pos
->packet_real_index
= NULL
;
626 switch (open_flags
& O_ACCMODE
) {
628 pos
->prot
= PROT_READ
;
629 pos
->flags
= MAP_PRIVATE
;
630 pos
->parent
.rw_table
= read_dispatch_table
;
631 pos
->parent
.event_cb
= ctf_read_event
;
632 pos
->parent
.trace
= trace
;
635 pos
->prot
= PROT_WRITE
; /* Write has priority */
636 pos
->flags
= MAP_SHARED
;
637 pos
->parent
.rw_table
= write_dispatch_table
;
638 pos
->parent
.event_cb
= ctf_write_event
;
639 pos
->parent
.trace
= trace
;
641 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
649 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
651 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
652 *pos
->content_size_loc
= pos
->offset
;
657 ret
= munmap_align(pos
->base_mma
);
659 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
664 if (pos
->packet_cycles_index
)
665 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
666 if (pos
->packet_real_index
)
667 (void) g_array_free(pos
->packet_real_index
, TRUE
);
672 * for SEEK_CUR: go to next packet.
673 * for SEEK_POS: go to packet numer (index).
675 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
677 struct ctf_stream_pos
*pos
=
678 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
679 struct ctf_file_stream
*file_stream
=
680 container_of(pos
, struct ctf_file_stream
, pos
);
683 struct packet_index
*packet_index
;
685 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
686 *pos
->content_size_loc
= pos
->offset
;
690 ret
= munmap_align(pos
->base_mma
);
692 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
696 pos
->base_mma
= NULL
;
700 * The caller should never ask for ctf_move_pos across packets,
701 * except to get exactly at the beginning of the next packet.
703 if (pos
->prot
== PROT_WRITE
) {
706 /* The writer will add padding */
707 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
710 assert(index
== 0); /* only seek supported for now */
716 pos
->content_size
= -1U; /* Unknown at this point */
717 pos
->packet_size
= WRITE_PACKET_LEN
;
718 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
719 pos
->packet_size
/ CHAR_BIT
);
727 uint64_t events_discarded_diff
;
729 if (pos
->offset
== EOF
) {
732 /* For printing discarded event count */
733 packet_index
= &g_array_index(pos
->packet_cycles_index
,
734 struct packet_index
, pos
->cur_index
);
735 file_stream
->parent
.prev_cycles_timestamp_end
=
736 packet_index
->timestamp_end
;
737 file_stream
->parent
.prev_cycles_timestamp
=
738 packet_index
->timestamp_begin
;
740 packet_index
= &g_array_index(pos
->packet_real_index
,
741 struct packet_index
, pos
->cur_index
);
742 file_stream
->parent
.prev_real_timestamp_end
=
743 packet_index
->timestamp_end
;
744 file_stream
->parent
.prev_real_timestamp
=
745 packet_index
->timestamp_begin
;
747 events_discarded_diff
= packet_index
->events_discarded
;
748 if (pos
->cur_index
> 0) {
749 packet_index
= &g_array_index(pos
->packet_real_index
,
752 events_discarded_diff
-= packet_index
->events_discarded
;
754 * Deal with 32-bit wrap-around if the
755 * tracer provided a 32-bit field.
757 if (packet_index
->events_discarded_len
== 32) {
758 events_discarded_diff
= (uint32_t) events_discarded_diff
;
761 file_stream
->parent
.events_discarded
= events_discarded_diff
;
762 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
763 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
764 /* The reader will expect us to skip padding */
769 packet_index
= &g_array_index(pos
->packet_cycles_index
,
770 struct packet_index
, index
);
771 pos
->last_events_discarded
= packet_index
->events_discarded
;
772 pos
->cur_index
= index
;
773 file_stream
->parent
.prev_real_timestamp
= 0;
774 file_stream
->parent
.prev_real_timestamp_end
= 0;
775 file_stream
->parent
.prev_cycles_timestamp
= 0;
776 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
781 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
783 * We need to check if we are in trace read or
784 * called from packet indexing. In this last
785 * case, the collection is not there, so we
786 * cannot print the timestamps.
788 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
790 * When a stream reaches the end of the
791 * file, we need to show the number of
792 * events discarded ourselves, because
793 * there is no next event scheduled to
794 * be printed in the output.
796 if (file_stream
->parent
.events_discarded
) {
798 ctf_print_discarded(stderr
,
799 &file_stream
->parent
,
801 file_stream
->parent
.events_discarded
= 0;
807 packet_index
= &g_array_index(pos
->packet_cycles_index
,
810 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
812 packet_index
= &g_array_index(pos
->packet_real_index
,
815 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
816 pos
->mmap_offset
= packet_index
->offset
;
818 /* Lookup context/packet size in index */
819 pos
->content_size
= packet_index
->content_size
;
820 pos
->packet_size
= packet_index
->packet_size
;
821 if (packet_index
->data_offset
< packet_index
->content_size
) {
822 pos
->offset
= 0; /* will read headers */
823 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
825 pos
->offset
= packet_index
->data_offset
;
827 goto read_next_packet
;
833 /* map new base. Need mapping length from header. */
834 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
835 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
836 if (pos
->base_mma
== MAP_FAILED
) {
837 fprintf(stderr
, "[error] mmap error %s.\n",
842 /* update trace_packet_header and stream_packet_context */
843 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
844 /* Read packet header */
845 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
848 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
849 /* Read packet context */
850 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
856 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
862 len
= fread(&magic
, sizeof(magic
), 1, fp
);
866 if (magic
== TSDL_MAGIC
) {
868 td
->byte_order
= BYTE_ORDER
;
869 CTF_TRACE_SET_FIELD(td
, byte_order
);
870 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
872 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
873 LITTLE_ENDIAN
: BIG_ENDIAN
;
874 CTF_TRACE_SET_FIELD(td
, byte_order
);
882 * Returns 0 on success, -1 on error.
885 int check_version(unsigned int major
, unsigned int minor
)
900 /* eventually return an error instead of warning */
902 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
908 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
911 struct metadata_packet_header header
;
912 size_t readlen
, writelen
, toread
;
913 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
916 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
920 if (td
->byte_order
!= BYTE_ORDER
) {
921 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
922 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
923 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
924 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
927 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
928 if (header
.compression_scheme
) {
929 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
930 header
.compression_scheme
);
933 if (header
.encryption_scheme
) {
934 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
935 header
.encryption_scheme
);
938 if (header
.checksum_scheme
) {
939 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
940 header
.checksum_scheme
);
943 if (check_version(header
.major
, header
.minor
) < 0)
945 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
946 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
947 CTF_TRACE_SET_FIELD(td
, uuid
);
949 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
953 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
956 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
959 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
964 if (babeltrace_debug
) {
966 fprintf(stderr
, "[debug] metadata packet read: %s\n",
970 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
971 if (writelen
< readlen
) {
981 ret
= 0; /* continue reading next packet */
988 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
989 ret
= fseek(in
, toread
, SEEK_CUR
);
991 fprintf(stderr
, "[warning] Missing padding at end of file\n");
998 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1002 size_t size
, buflen
;
1007 * Using strlen on *buf instead of size of open_memstream
1008 * because its size includes garbage at the end (after final
1009 * \0). This is the allocated size, not the actual string size.
1011 out
= babeltrace_open_memstream(buf
, &size
);
1013 perror("Metadata open_memstream");
1017 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
1026 /* close to flush the buffer */
1027 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1031 perror("babeltrace_flush_memstream");
1033 closeret
= fclose(in
);
1035 perror("Error in fclose");
1041 perror("Error in fclose");
1043 /* open for reading */
1044 buflen
= strlen(*buf
);
1049 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1051 perror("Metadata fmemopen");
1058 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1059 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1060 int whence
), FILE *metadata_fp
)
1062 struct ctf_scanner
*scanner
;
1063 struct ctf_file_stream
*metadata_stream
;
1066 int ret
= 0, closeret
;
1068 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1069 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1072 metadata_stream
->pos
.packet_seek
= packet_seek
;
1074 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1081 metadata_stream
->pos
.fd
= -1;
1083 td
->metadata
= &metadata_stream
->parent
;
1084 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1085 if (metadata_stream
->pos
.fd
< 0) {
1086 fprintf(stderr
, "Unable to open metadata.\n");
1091 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1093 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1094 perror("Metadata stream open");
1098 /* fd now belongs to fp */
1099 metadata_stream
->pos
.fd
= -1;
1101 if (babeltrace_debug
)
1104 if (packet_metadata(td
, fp
)) {
1105 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1107 /* Warn about empty metadata */
1108 fprintf(stderr
, "[warning] Empty metadata.\n");
1109 goto end_packet_read
;
1111 td
->metadata_string
= buf
;
1112 td
->metadata_packetized
= 1;
1114 unsigned int major
, minor
;
1117 td
->byte_order
= BYTE_ORDER
;
1119 /* Check text-only metadata header and version */
1120 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1122 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1123 if (check_version(major
, minor
) < 0) {
1125 goto end_packet_read
;
1130 scanner
= ctf_scanner_alloc(fp
);
1132 fprintf(stderr
, "[error] Error allocating scanner\n");
1134 goto end_scanner_alloc
;
1136 ret
= ctf_scanner_append_ast(scanner
);
1138 fprintf(stderr
, "[error] Error creating AST\n");
1142 if (babeltrace_debug
) {
1143 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1145 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1150 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1152 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1155 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1156 td
, td
->byte_order
);
1158 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1162 ctf_scanner_free(scanner
);
1166 closeret
= fclose(fp
);
1168 perror("Error on fclose");
1172 if (metadata_stream
->pos
.fd
>= 0) {
1173 closeret
= close(metadata_stream
->pos
.fd
);
1175 perror("Error on metadata stream fd close");
1180 g_free(metadata_stream
);
1185 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1186 struct ctf_stream_definition
*stream
,
1187 struct ctf_event_declaration
*event
)
1189 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1191 if (event
->context_decl
) {
1192 struct bt_definition
*definition
=
1193 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1194 stream
->parent_def_scope
, 0, 0, "event.context");
1198 stream_event
->event_context
= container_of(definition
,
1199 struct definition_struct
, p
);
1200 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1202 if (event
->fields_decl
) {
1203 struct bt_definition
*definition
=
1204 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1205 stream
->parent_def_scope
, 0, 0, "event.fields");
1209 stream_event
->event_fields
= container_of(definition
,
1210 struct definition_struct
, p
);
1211 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1213 stream_event
->stream
= stream
;
1214 return stream_event
;
1217 if (stream_event
->event_fields
)
1218 bt_definition_unref(&stream_event
->event_fields
->p
);
1219 if (stream_event
->event_context
)
1220 bt_definition_unref(&stream_event
->event_context
->p
);
1221 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1222 g_quark_to_string(event
->name
));
1227 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1229 struct ctf_stream_declaration
*stream_class
;
1233 if (stream
->stream_definitions_created
)
1236 stream_class
= stream
->stream_class
;
1238 if (stream_class
->packet_context_decl
) {
1239 struct bt_definition
*definition
=
1240 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1241 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1246 stream
->stream_packet_context
= container_of(definition
,
1247 struct definition_struct
, p
);
1248 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1250 if (stream_class
->event_header_decl
) {
1251 struct bt_definition
*definition
=
1252 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1253 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1258 stream
->stream_event_header
=
1259 container_of(definition
, struct definition_struct
, p
);
1260 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1262 if (stream_class
->event_context_decl
) {
1263 struct bt_definition
*definition
=
1264 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1265 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1270 stream
->stream_event_context
=
1271 container_of(definition
, struct definition_struct
, p
);
1272 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1274 stream
->events_by_id
= g_ptr_array_new();
1275 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1276 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1277 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1278 struct ctf_event_definition
*stream_event
;
1282 stream_event
= create_event_definitions(td
, stream
, event
);
1283 if (!stream_event
) {
1287 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1292 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1293 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1295 g_free(stream_event
);
1297 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1299 if (stream
->stream_event_context
)
1300 bt_definition_unref(&stream
->stream_event_context
->p
);
1301 if (stream
->stream_event_header
)
1302 bt_definition_unref(&stream
->stream_event_header
->p
);
1303 if (stream
->stream_packet_context
)
1304 bt_definition_unref(&stream
->stream_packet_context
->p
);
1305 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1306 stream_class
->stream_id
, strerror(-ret
));
1311 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1312 struct ctf_trace
*td
,
1313 struct ctf_file_stream
*file_stream
,
1316 struct packet_index packet_index
;
1317 struct ctf_stream_declaration
*stream
;
1318 uint64_t stream_id
= 0;
1319 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1320 int first_packet
= 0;
1325 if (!pos
->mmap_offset
) {
1329 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1330 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1333 if (pos
->base_mma
) {
1334 /* unmap old base */
1335 ret
= munmap_align(pos
->base_mma
);
1337 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1341 pos
->base_mma
= NULL
;
1343 /* map new base. Need mapping length from header. */
1344 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1345 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1346 assert(pos
->base_mma
!= MAP_FAILED
);
1348 * Use current mapping size as temporary content and packet
1351 pos
->content_size
= packet_map_len
;
1352 pos
->packet_size
= packet_map_len
;
1353 pos
->offset
= 0; /* Position of the packet header */
1355 packet_index
.offset
= pos
->mmap_offset
;
1356 packet_index
.content_size
= 0;
1357 packet_index
.packet_size
= 0;
1358 packet_index
.timestamp_begin
= 0;
1359 packet_index
.timestamp_end
= 0;
1360 packet_index
.events_discarded
= 0;
1361 packet_index
.events_discarded_len
= 0;
1363 /* read and check header, set stream id (and check) */
1364 if (file_stream
->parent
.trace_packet_header
) {
1365 /* Read packet header */
1366 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1370 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1373 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1374 if (len_index
>= 0) {
1375 struct bt_definition
*field
;
1378 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1379 magic
= bt_get_unsigned_int(field
);
1380 if (magic
!= CTF_MAGIC
) {
1381 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1383 file_stream
->pos
.packet_cycles_index
->len
,
1384 (ssize_t
) pos
->mmap_offset
);
1390 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1391 if (len_index
>= 0) {
1392 struct definition_array
*defarray
;
1393 struct bt_definition
*field
;
1395 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1397 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1398 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1399 defarray
= container_of(field
, struct definition_array
, p
);
1400 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1402 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1403 struct bt_definition
*elem
;
1405 elem
= bt_array_index(defarray
, i
);
1406 uuidval
[i
] = bt_get_unsigned_int(elem
);
1408 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1410 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1415 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1416 if (len_index
>= 0) {
1417 struct bt_definition
*field
;
1419 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1420 stream_id
= bt_get_unsigned_int(field
);
1424 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1425 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1427 file_stream
->parent
.stream_id
);
1431 file_stream
->parent
.stream_id
= stream_id
;
1432 if (stream_id
>= td
->streams
->len
) {
1433 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1436 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1438 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1441 file_stream
->parent
.stream_class
= stream
;
1442 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1447 if (file_stream
->parent
.stream_packet_context
) {
1448 /* Read packet context */
1449 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1453 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1456 /* read content size from header */
1457 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1458 if (len_index
>= 0) {
1459 struct bt_definition
*field
;
1461 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1462 packet_index
.content_size
= bt_get_unsigned_int(field
);
1464 /* Use file size for packet size */
1465 packet_index
.content_size
= filesize
* CHAR_BIT
;
1468 /* read packet size from header */
1469 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1470 if (len_index
>= 0) {
1471 struct bt_definition
*field
;
1473 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1474 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1476 /* Use content size if non-zero, else file size */
1477 packet_index
.packet_size
= packet_index
.content_size
? : filesize
* CHAR_BIT
;
1480 /* read timestamp begin from header */
1481 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1482 if (len_index
>= 0) {
1483 struct bt_definition
*field
;
1485 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1486 packet_index
.timestamp_begin
= bt_get_unsigned_int(field
);
1487 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1488 packet_index
.timestamp_begin
=
1489 ctf_get_real_timestamp(
1490 &file_stream
->parent
,
1491 packet_index
.timestamp_begin
);
1495 /* read timestamp end from header */
1496 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1497 if (len_index
>= 0) {
1498 struct bt_definition
*field
;
1500 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1501 packet_index
.timestamp_end
= bt_get_unsigned_int(field
);
1502 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1503 packet_index
.timestamp_end
=
1504 ctf_get_real_timestamp(
1505 &file_stream
->parent
,
1506 packet_index
.timestamp_end
);
1510 /* read events discarded from header */
1511 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1512 if (len_index
>= 0) {
1513 struct bt_definition
*field
;
1515 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1516 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1517 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1520 /* Use file size for packet size */
1521 packet_index
.content_size
= filesize
* CHAR_BIT
;
1522 /* Use content size if non-zero, else file size */
1523 packet_index
.packet_size
= packet_index
.content_size
? : filesize
* CHAR_BIT
;
1526 /* Validate content size and packet size values */
1527 if (packet_index
.content_size
> packet_index
.packet_size
) {
1528 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1529 packet_index
.content_size
, packet_index
.packet_size
);
1533 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1534 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1535 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1539 /* Save position after header and context */
1540 packet_index
.data_offset
= pos
->offset
;
1542 /* add index to packet array */
1543 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1545 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1549 /* Retry with larger mapping */
1551 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1553 * Reached EOF, but still expecting header/context data.
1555 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1558 /* Double the mapping len, and retry */
1559 tmp_map_len
= packet_map_len
<< 1;
1560 if (tmp_map_len
>> 1 != packet_map_len
) {
1562 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1565 packet_map_len
= tmp_map_len
;
1570 int create_stream_packet_index(struct ctf_trace
*td
,
1571 struct ctf_file_stream
*file_stream
)
1573 struct ctf_stream_pos
*pos
;
1574 struct stat filestats
;
1577 pos
= &file_stream
->pos
;
1579 ret
= fstat(pos
->fd
, &filestats
);
1583 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1584 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1593 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1597 if (td
->packet_header_decl
) {
1598 struct bt_definition
*definition
=
1599 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1600 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1605 stream
->trace_packet_header
=
1606 container_of(definition
, struct definition_struct
, p
);
1607 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1613 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1618 * Note: many file streams can inherit from the same stream class
1619 * description (metadata).
1622 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1623 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1626 int ret
, fd
, closeret
;
1627 struct ctf_file_stream
*file_stream
;
1628 struct stat statbuf
;
1630 fd
= openat(td
->dirfd
, path
, flags
);
1632 perror("File stream openat()");
1637 /* Don't try to mmap subdirectories. Skip them, return success. */
1638 ret
= fstat(fd
, &statbuf
);
1640 perror("File stream fstat()");
1643 if (S_ISDIR(statbuf
.st_mode
)) {
1644 fprintf(stderr
, "[warning] Skipping directory '%s' found in trace\n", path
);
1649 file_stream
= g_new0(struct ctf_file_stream
, 1);
1650 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1652 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
1653 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
1656 file_stream
->pos
.packet_seek
= packet_seek
;
1658 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1663 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
1666 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1670 * For now, only a single clock per trace is supported.
1672 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
1673 ret
= create_stream_packet_index(td
, file_stream
);
1675 fprintf(stderr
, "[error] Stream index creation error.\n");
1678 /* Add stream file to stream class */
1679 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1680 &file_stream
->parent
);
1684 if (file_stream
->parent
.trace_packet_header
)
1685 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1687 closeret
= ctf_fini_pos(&file_stream
->pos
);
1689 fprintf(stderr
, "Error on ctf_fini_pos\n");
1691 g_free(file_stream
);
1694 closeret
= close(fd
);
1696 perror("Error on fd close");
1703 int ctf_open_trace_read(struct ctf_trace
*td
,
1704 const char *path
, int flags
,
1705 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1706 int whence
), FILE *metadata_fp
)
1709 struct dirent
*dirent
;
1710 struct dirent
*diriter
;
1715 /* Open trace directory */
1716 td
->dir
= opendir(path
);
1718 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1723 td
->dirfd
= open(path
, 0);
1724 if (td
->dirfd
< 0) {
1725 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1726 perror("Trace directory open");
1730 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
1731 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
1734 * Keep the metadata file separate.
1737 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1739 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1740 goto error_metadata
;
1744 * Open each stream: for each file, try to open, check magic
1745 * number, and get the stream ID to add to the right location in
1749 dirent_len
= offsetof(struct dirent
, d_name
) +
1750 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1752 dirent
= malloc(dirent_len
);
1755 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1757 fprintf(stderr
, "[error] Readdir error.\n");
1762 /* Ignore hidden files, ., .. and metadata. */
1763 if (!strncmp(diriter
->d_name
, ".", 1)
1764 || !strcmp(diriter
->d_name
, "..")
1765 || !strcmp(diriter
->d_name
, "metadata"))
1767 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1768 flags
, packet_seek
);
1770 fprintf(stderr
, "[error] Open file stream error.\n");
1781 closeret
= close(td
->dirfd
);
1783 perror("Error on fd close");
1786 closeret
= closedir(td
->dir
);
1788 perror("Error on closedir");
1795 * ctf_open_trace: Open a CTF trace and index it.
1796 * Note that the user must seek the trace after the open (using the iterator)
1797 * since the index creation read it entirely.
1800 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1801 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1802 int whence
), FILE *metadata_fp
)
1804 struct ctf_trace
*td
;
1808 * If packet_seek is NULL, we provide our default version.
1811 packet_seek
= ctf_packet_seek
;
1813 td
= g_new0(struct ctf_trace
, 1);
1815 switch (flags
& O_ACCMODE
) {
1818 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1821 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1826 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1829 fprintf(stderr
, "[error] Incorrect open flags.\n");
1840 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1841 struct bt_mmap_stream
*mmap_info
)
1843 pos
->mmap_offset
= 0;
1844 pos
->packet_size
= 0;
1845 pos
->content_size
= 0;
1846 pos
->content_size_loc
= NULL
;
1847 pos
->fd
= mmap_info
->fd
;
1848 pos
->base_mma
= NULL
;
1852 pos
->packet_cycles_index
= NULL
;
1853 pos
->packet_real_index
= NULL
;
1854 pos
->prot
= PROT_READ
;
1855 pos
->flags
= MAP_PRIVATE
;
1856 pos
->parent
.rw_table
= read_dispatch_table
;
1857 pos
->parent
.event_cb
= ctf_read_event
;
1861 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1862 struct ctf_file_stream
*file_stream
)
1864 struct ctf_stream_declaration
*stream
;
1865 uint64_t stream_id
= 0;
1868 file_stream
->parent
.stream_id
= stream_id
;
1869 if (stream_id
>= td
->streams
->len
) {
1870 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1871 "in metadata.\n", stream_id
);
1875 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1877 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1878 "in metadata.\n", stream_id
);
1882 file_stream
->parent
.stream_class
= stream
;
1883 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1889 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1890 struct bt_mmap_stream
*mmap_info
,
1891 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1895 struct ctf_file_stream
*file_stream
;
1897 file_stream
= g_new0(struct ctf_file_stream
, 1);
1898 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1899 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1901 file_stream
->pos
.packet_seek
= packet_seek
;
1903 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1908 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1913 * For now, only a single clock per trace is supported.
1915 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
1917 /* Add stream file to stream class */
1918 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1919 &file_stream
->parent
);
1923 if (file_stream
->parent
.trace_packet_header
)
1924 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1926 g_free(file_stream
);
1931 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1932 struct bt_mmap_stream_list
*mmap_list
,
1933 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1938 struct bt_mmap_stream
*mmap_info
;
1940 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1946 * for each stream, try to open, check magic number, and get the
1947 * stream ID to add to the right location in the stream array.
1949 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1950 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1952 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1964 struct bt_trace_descriptor
*ctf_open_mmap_trace(
1965 struct bt_mmap_stream_list
*mmap_list
,
1966 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1970 struct ctf_trace
*td
;
1974 fprintf(stderr
, "[error] No metadata file pointer associated, "
1975 "required for mmap parsing\n");
1979 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1982 td
= g_new0(struct ctf_trace
, 1);
1983 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1996 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
1999 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2001 /* for each stream_class */
2002 for (i
= 0; i
< td
->streams
->len
; i
++) {
2003 struct ctf_stream_declaration
*stream_class
;
2005 stream_class
= g_ptr_array_index(td
->streams
, i
);
2008 /* for each file_stream */
2009 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2010 struct ctf_stream_definition
*stream
;
2011 struct ctf_stream_pos
*stream_pos
;
2012 struct ctf_file_stream
*cfs
;
2014 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2017 cfs
= container_of(stream
, struct ctf_file_stream
,
2019 stream_pos
= &cfs
->pos
;
2020 if (!stream_pos
->packet_cycles_index
)
2023 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
2024 struct packet_index
*index
;
2025 struct packet_index new_index
;
2027 index
= &g_array_index(stream_pos
->packet_cycles_index
,
2028 struct packet_index
, k
);
2029 memcpy(&new_index
, index
,
2030 sizeof(struct packet_index
));
2031 new_index
.timestamp_begin
=
2032 ctf_get_real_timestamp(stream
,
2033 index
->timestamp_begin
);
2034 new_index
.timestamp_end
=
2035 ctf_get_real_timestamp(stream
,
2036 index
->timestamp_end
);
2037 g_array_append_val(stream_pos
->packet_real_index
,
2046 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2050 ret
= ctf_fini_pos(&file_stream
->pos
);
2052 fprintf(stderr
, "Error on ctf_fini_pos\n");
2055 ret
= close(file_stream
->pos
.fd
);
2057 perror("Error closing file fd");
2064 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2066 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2072 for (i
= 0; i
< td
->streams
->len
; i
++) {
2073 struct ctf_stream_declaration
*stream
;
2076 stream
= g_ptr_array_index(td
->streams
, i
);
2079 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2080 struct ctf_file_stream
*file_stream
;
2081 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2082 struct ctf_file_stream
, parent
);
2083 ret
= ctf_close_file_stream(file_stream
);
2089 ctf_destroy_metadata(td
);
2090 ret
= close(td
->dirfd
);
2092 perror("Error closing dirfd");
2095 ret
= closedir(td
->dir
);
2097 perror("Error closedir");
2100 free(td
->metadata_string
);
2106 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2107 struct bt_context
*ctx
)
2109 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2112 td
->parent
.ctx
= ctx
;
2116 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2117 struct bt_trace_handle
*handle
)
2119 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2122 td
->parent
.handle
= handle
;
2126 void __attribute__((constructor
)) ctf_init(void)
2130 ctf_format
.name
= g_quark_from_static_string("ctf");
2131 ret
= bt_register_format(&ctf_format
);
2136 void __attribute__((destructor
)) ctf_exit(void)
2138 bt_unregister_format(&ctf_format
);