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/compat/uuid.h>
37 #include <babeltrace/endian.h>
38 #include <babeltrace/ctf/ctf-index.h>
43 #include <sys/types.h>
51 #include "metadata/ctf-scanner.h"
52 #include "metadata/ctf-parser.h"
53 #include "metadata/ctf-ast.h"
54 #include "events-private.h"
55 #include <babeltrace/compat/memstream.h>
57 #define LOG2_CHAR_BIT 3
60 * Length of first attempt at mapping a packet header, in bits.
62 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
65 * Lenght of packet to write, in bits.
67 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
70 #define min(a, b) (((a) < (b)) ? (a) : (b))
73 #define NSEC_PER_SEC 1000000000ULL
75 #define INDEX_PATH "./index/%s.idx"
82 uint64_t opt_clock_offset
;
83 uint64_t opt_clock_offset_ns
;
88 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
89 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
93 struct bt_trace_descriptor
*ctf_open_mmap_trace(
94 struct bt_mmap_stream_list
*mmap_list
,
95 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
99 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
100 struct bt_context
*ctx
);
102 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
103 struct bt_trace_handle
*handle
);
106 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
108 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
109 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
111 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
112 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
114 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
117 rw_dispatch read_dispatch_table
[] = {
118 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
119 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
120 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
121 [ CTF_TYPE_STRING
] = ctf_string_read
,
122 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
123 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
124 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
125 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
129 rw_dispatch write_dispatch_table
[] = {
130 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
131 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
132 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
133 [ CTF_TYPE_STRING
] = ctf_string_write
,
134 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
135 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
136 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
137 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
141 struct bt_format ctf_format
= {
142 .open_trace
= ctf_open_trace
,
143 .open_mmap_trace
= ctf_open_mmap_trace
,
144 .close_trace
= ctf_close_trace
,
145 .set_context
= ctf_set_context
,
146 .set_handle
= ctf_set_handle
,
147 .timestamp_begin
= ctf_timestamp_begin
,
148 .timestamp_end
= ctf_timestamp_end
,
149 .convert_index_timestamp
= ctf_convert_index_timestamp
,
153 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
154 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
156 struct ctf_trace
*tin
;
157 uint64_t begin
= ULLONG_MAX
;
160 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
165 /* for each stream_class */
166 for (i
= 0; i
< tin
->streams
->len
; i
++) {
167 struct ctf_stream_declaration
*stream_class
;
169 stream_class
= g_ptr_array_index(tin
->streams
, i
);
172 /* for each file_stream */
173 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
174 struct ctf_stream_definition
*stream
;
175 struct ctf_file_stream
*cfs
;
176 struct ctf_stream_pos
*stream_pos
;
177 struct packet_index
*index
;
179 stream
= g_ptr_array_index(stream_class
->streams
, j
);
180 cfs
= container_of(stream
, struct ctf_file_stream
,
182 stream_pos
= &cfs
->pos
;
184 if (!stream_pos
->packet_real_index
)
187 if (stream_pos
->packet_real_index
->len
<= 0)
190 if (type
== BT_CLOCK_REAL
) {
191 index
= &g_array_index(stream_pos
->packet_real_index
,
193 stream_pos
->packet_real_index
->len
- 1);
194 } else if (type
== BT_CLOCK_CYCLES
) {
195 index
= &g_array_index(stream_pos
->packet_cycles_index
,
197 stream_pos
->packet_real_index
->len
- 1);
202 if (index
->timestamp_begin
< begin
)
203 begin
= index
->timestamp_begin
;
214 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
215 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
217 struct ctf_trace
*tin
;
221 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
226 /* for each stream_class */
227 for (i
= 0; i
< tin
->streams
->len
; i
++) {
228 struct ctf_stream_declaration
*stream_class
;
230 stream_class
= g_ptr_array_index(tin
->streams
, i
);
233 /* for each file_stream */
234 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
235 struct ctf_stream_definition
*stream
;
236 struct ctf_file_stream
*cfs
;
237 struct ctf_stream_pos
*stream_pos
;
238 struct packet_index
*index
;
240 stream
= g_ptr_array_index(stream_class
->streams
, j
);
241 cfs
= container_of(stream
, struct ctf_file_stream
,
243 stream_pos
= &cfs
->pos
;
245 if (!stream_pos
->packet_real_index
)
248 if (stream_pos
->packet_real_index
->len
<= 0)
251 if (type
== BT_CLOCK_REAL
) {
252 index
= &g_array_index(stream_pos
->packet_real_index
,
254 stream_pos
->packet_real_index
->len
- 1);
255 } else if (type
== BT_CLOCK_CYCLES
) {
256 index
= &g_array_index(stream_pos
->packet_cycles_index
,
258 stream_pos
->packet_real_index
->len
- 1);
263 if (index
->timestamp_end
> end
)
264 end
= index
->timestamp_end
;
275 * Update stream current timestamp
278 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
279 struct definition_integer
*integer_definition
)
281 struct declaration_integer
*integer_declaration
=
282 integer_definition
->declaration
;
283 uint64_t oldval
, newval
, updateval
;
285 if (unlikely(integer_declaration
->len
== 64)) {
286 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
287 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
288 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
289 stream
->prev_cycles_timestamp
);
290 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
291 stream
->cycles_timestamp
);
295 oldval
= stream
->cycles_timestamp
;
296 oldval
&= (1ULL << integer_declaration
->len
) - 1;
297 newval
= integer_definition
->value
._unsigned
;
298 /* Test for overflow by comparing low bits */
300 newval
+= 1ULL << integer_declaration
->len
;
301 /* updateval contains old high bits, and new low bits (sum) */
302 updateval
= stream
->cycles_timestamp
;
303 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
305 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
306 stream
->cycles_timestamp
= updateval
;
308 /* convert to real timestamp */
309 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
310 stream
->prev_cycles_timestamp
);
311 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
312 stream
->cycles_timestamp
);
316 * Print timestamp, rescaling clock frequency to nanoseconds and
317 * applying offsets as needed (unix time).
320 void ctf_print_timestamp_real(FILE *fp
,
321 struct ctf_stream_definition
*stream
,
324 uint64_t ts_sec
= 0, ts_nsec
;
328 /* Add command-line offset in ns*/
329 ts_nsec
+= opt_clock_offset_ns
;
331 /* Add command-line offset */
332 ts_sec
+= opt_clock_offset
;
334 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
335 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
337 if (!opt_clock_seconds
) {
339 time_t time_s
= (time_t) ts_sec
;
341 if (!opt_clock_gmt
) {
344 res
= localtime_r(&time_s
, &tm
);
346 fprintf(stderr
, "[warning] Unable to get localtime.\n");
352 res
= gmtime_r(&time_s
, &tm
);
354 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
358 if (opt_clock_date
) {
362 /* Print date and time */
363 res
= strftime(timestr
, sizeof(timestr
),
366 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
369 fprintf(fp
, "%s", timestr
);
371 /* Print time in HH:MM:SS.ns */
372 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
373 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
377 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
385 * Print timestamp, in cycles
388 void ctf_print_timestamp_cycles(FILE *fp
,
389 struct ctf_stream_definition
*stream
,
392 fprintf(fp
, "%020" PRIu64
, timestamp
);
395 void ctf_print_timestamp(FILE *fp
,
396 struct ctf_stream_definition
*stream
,
399 if (opt_clock_cycles
) {
400 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
402 ctf_print_timestamp_real(fp
, stream
, timestamp
);
407 void print_uuid(FILE *fp
, unsigned char *uuid
)
411 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
412 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
415 void ctf_print_discarded(FILE *fp
, struct ctf_stream_definition
*stream
,
418 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events %sbetween [",
419 stream
->events_discarded
,
420 end_stream
? "at end of stream " : "");
421 if (opt_clock_cycles
) {
422 ctf_print_timestamp(fp
, stream
,
423 stream
->prev_cycles_timestamp
);
424 fprintf(fp
, "] and [");
425 ctf_print_timestamp(fp
, stream
,
426 stream
->prev_cycles_timestamp_end
);
428 ctf_print_timestamp(fp
, stream
,
429 stream
->prev_real_timestamp
);
430 fprintf(fp
, "] and [");
431 ctf_print_timestamp(fp
, stream
,
432 stream
->prev_real_timestamp_end
);
434 fprintf(fp
, "] in trace UUID ");
435 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
436 if (stream
->stream_class
->trace
->parent
.path
[0])
437 fprintf(fp
, ", at path: \"%s\"",
438 stream
->stream_class
->trace
->parent
.path
);
440 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
442 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
444 fprintf(fp
, "You should consider recording a new trace with larger "
445 "buffers or with fewer events enabled.\n");
450 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
452 struct ctf_stream_pos
*pos
=
453 container_of(ppos
, struct ctf_stream_pos
, parent
);
454 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
455 struct ctf_event_definition
*event
;
459 /* We need to check for EOF here for empty files. */
460 if (unlikely(pos
->offset
== EOF
))
463 ctf_pos_get_event(pos
);
465 /* save the current position as a restore point */
466 pos
->last_offset
= pos
->offset
;
469 * This is the EOF check after we've advanced the position in
472 if (unlikely(pos
->offset
== EOF
))
475 if (pos
->content_size
== 0) {
476 /* Stream is inactive for now (live reading). */
479 /* Packet only contains headers */
480 if (pos
->offset
== pos
->content_size
)
483 assert(pos
->offset
< pos
->content_size
);
485 /* Read event header */
486 if (likely(stream
->stream_event_header
)) {
487 struct definition_integer
*integer_definition
;
488 struct bt_definition
*variant
;
490 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
493 /* lookup event id */
494 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
495 if (integer_definition
) {
496 id
= integer_definition
->value
._unsigned
;
498 struct definition_enum
*enum_definition
;
500 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
501 if (enum_definition
) {
502 id
= enum_definition
->integer
->value
._unsigned
;
506 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
508 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
509 if (integer_definition
) {
510 id
= integer_definition
->value
._unsigned
;
513 stream
->event_id
= id
;
515 /* lookup timestamp */
516 stream
->has_timestamp
= 0;
517 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
518 if (integer_definition
) {
519 ctf_update_timestamp(stream
, integer_definition
);
520 stream
->has_timestamp
= 1;
523 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
524 if (integer_definition
) {
525 ctf_update_timestamp(stream
, integer_definition
);
526 stream
->has_timestamp
= 1;
532 /* Read stream-declared event context */
533 if (stream
->stream_event_context
) {
534 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
539 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
540 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
543 event
= g_ptr_array_index(stream
->events_by_id
, id
);
544 if (unlikely(!event
)) {
545 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
549 /* Read event-declared event context */
550 if (event
->event_context
) {
551 ret
= generic_rw(ppos
, &event
->event_context
->p
);
556 /* Read event payload */
557 if (likely(event
->event_fields
)) {
558 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
563 if (pos
->last_offset
== pos
->offset
) {
564 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
571 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
576 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
578 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
579 struct ctf_event_definition
*event
;
583 id
= stream
->event_id
;
585 /* print event header */
586 if (likely(stream
->stream_event_header
)) {
587 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
592 /* print stream-declared event context */
593 if (stream
->stream_event_context
) {
594 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
599 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
600 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
603 event
= g_ptr_array_index(stream
->events_by_id
, id
);
604 if (unlikely(!event
)) {
605 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
609 /* print event-declared event context */
610 if (event
->event_context
) {
611 ret
= generic_rw(pos
, &event
->event_context
->p
);
616 /* Read and print event payload */
617 if (likely(event
->event_fields
)) {
618 ret
= generic_rw(pos
, &event
->event_fields
->p
);
626 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
630 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
631 int fd
, int open_flags
)
635 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
636 sizeof(struct packet_index
));
637 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
638 sizeof(struct packet_index
));
640 pos
->packet_cycles_index
= NULL
;
641 pos
->packet_real_index
= NULL
;
643 switch (open_flags
& O_ACCMODE
) {
645 pos
->prot
= PROT_READ
;
646 pos
->flags
= MAP_PRIVATE
;
647 pos
->parent
.rw_table
= read_dispatch_table
;
648 pos
->parent
.event_cb
= ctf_read_event
;
649 pos
->parent
.trace
= trace
;
652 pos
->prot
= PROT_WRITE
; /* Write has priority */
653 pos
->flags
= MAP_SHARED
;
654 pos
->parent
.rw_table
= write_dispatch_table
;
655 pos
->parent
.event_cb
= ctf_write_event
;
656 pos
->parent
.trace
= trace
;
658 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
666 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
668 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
669 *pos
->content_size_loc
= pos
->offset
;
674 ret
= munmap_align(pos
->base_mma
);
676 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
681 if (pos
->packet_cycles_index
)
682 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
683 if (pos
->packet_real_index
)
684 (void) g_array_free(pos
->packet_real_index
, TRUE
);
689 * for SEEK_CUR: go to next packet.
690 * for SEEK_SET: go to packet numer (index).
692 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
694 struct ctf_stream_pos
*pos
=
695 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
696 struct ctf_file_stream
*file_stream
=
697 container_of(pos
, struct ctf_file_stream
, pos
);
700 struct packet_index
*packet_index
;
704 case SEEK_SET
: /* Fall-through */
710 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
711 *pos
->content_size_loc
= pos
->offset
;
715 ret
= munmap_align(pos
->base_mma
);
717 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
721 pos
->base_mma
= NULL
;
725 * The caller should never ask for ctf_move_pos across packets,
726 * except to get exactly at the beginning of the next packet.
728 if (pos
->prot
== PROT_WRITE
) {
731 /* The writer will add padding */
732 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
735 assert(index
== 0); /* only seek supported for now */
741 pos
->content_size
= -1U; /* Unknown at this point */
742 pos
->packet_size
= WRITE_PACKET_LEN
;
743 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
744 pos
->packet_size
/ CHAR_BIT
);
752 uint64_t events_discarded_diff
;
754 if (pos
->offset
== EOF
) {
757 assert(pos
->cur_index
< pos
->packet_cycles_index
->len
);
758 assert(pos
->cur_index
< pos
->packet_real_index
->len
);
760 /* For printing discarded event count */
761 packet_index
= &g_array_index(pos
->packet_cycles_index
,
762 struct packet_index
, pos
->cur_index
);
763 file_stream
->parent
.prev_cycles_timestamp_end
=
764 packet_index
->timestamp_end
;
765 file_stream
->parent
.prev_cycles_timestamp
=
766 packet_index
->timestamp_begin
;
768 packet_index
= &g_array_index(pos
->packet_real_index
,
769 struct packet_index
, pos
->cur_index
);
770 file_stream
->parent
.prev_real_timestamp_end
=
771 packet_index
->timestamp_end
;
772 file_stream
->parent
.prev_real_timestamp
=
773 packet_index
->timestamp_begin
;
775 events_discarded_diff
= packet_index
->events_discarded
;
776 if (pos
->cur_index
> 0) {
777 packet_index
= &g_array_index(pos
->packet_real_index
,
780 events_discarded_diff
-= packet_index
->events_discarded
;
782 * Deal with 32-bit wrap-around if the
783 * tracer provided a 32-bit field.
785 if (packet_index
->events_discarded_len
== 32) {
786 events_discarded_diff
= (uint32_t) events_discarded_diff
;
789 file_stream
->parent
.events_discarded
= events_discarded_diff
;
790 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
791 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
792 /* The reader will expect us to skip padding */
797 if (index
>= pos
->packet_cycles_index
->len
) {
801 packet_index
= &g_array_index(pos
->packet_cycles_index
,
802 struct packet_index
, index
);
803 pos
->last_events_discarded
= packet_index
->events_discarded
;
804 pos
->cur_index
= index
;
805 file_stream
->parent
.prev_real_timestamp
= 0;
806 file_stream
->parent
.prev_real_timestamp_end
= 0;
807 file_stream
->parent
.prev_cycles_timestamp
= 0;
808 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
813 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
815 * We need to check if we are in trace read or
816 * called from packet indexing. In this last
817 * case, the collection is not there, so we
818 * cannot print the timestamps.
820 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
822 * When a stream reaches the end of the
823 * file, we need to show the number of
824 * events discarded ourselves, because
825 * there is no next event scheduled to
826 * be printed in the output.
828 if (file_stream
->parent
.events_discarded
) {
830 ctf_print_discarded(stderr
,
831 &file_stream
->parent
,
833 file_stream
->parent
.events_discarded
= 0;
839 packet_index
= &g_array_index(pos
->packet_cycles_index
,
842 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
844 packet_index
= &g_array_index(pos
->packet_real_index
,
847 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
848 pos
->mmap_offset
= packet_index
->offset
;
850 /* Lookup context/packet size in index */
851 pos
->content_size
= packet_index
->content_size
;
852 pos
->packet_size
= packet_index
->packet_size
;
853 if (packet_index
->data_offset
< packet_index
->content_size
) {
854 pos
->offset
= 0; /* will read headers */
855 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
857 pos
->offset
= packet_index
->data_offset
;
859 goto read_next_packet
;
865 /* map new base. Need mapping length from header. */
866 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
867 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
868 if (pos
->base_mma
== MAP_FAILED
) {
869 fprintf(stderr
, "[error] mmap error %s.\n",
874 /* update trace_packet_header and stream_packet_context */
875 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
876 /* Read packet header */
877 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
880 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
881 /* Read packet context */
882 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
888 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
894 len
= fread(&magic
, sizeof(magic
), 1, fp
);
898 if (magic
== TSDL_MAGIC
) {
900 td
->byte_order
= BYTE_ORDER
;
901 CTF_TRACE_SET_FIELD(td
, byte_order
);
902 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
904 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
905 LITTLE_ENDIAN
: BIG_ENDIAN
;
906 CTF_TRACE_SET_FIELD(td
, byte_order
);
914 * Returns 0 on success, -1 on error.
917 int check_version(unsigned int major
, unsigned int minor
)
932 /* eventually return an error instead of warning */
934 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
940 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
943 struct metadata_packet_header header
;
944 size_t readlen
, writelen
, toread
;
945 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
948 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
952 if (td
->byte_order
!= BYTE_ORDER
) {
953 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
954 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
955 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
956 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
959 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
960 if (header
.compression_scheme
) {
961 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
962 header
.compression_scheme
);
965 if (header
.encryption_scheme
) {
966 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
967 header
.encryption_scheme
);
970 if (header
.checksum_scheme
) {
971 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
972 header
.checksum_scheme
);
975 if (check_version(header
.major
, header
.minor
) < 0)
977 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
978 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
979 CTF_TRACE_SET_FIELD(td
, uuid
);
981 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
985 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
988 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
991 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
996 if (babeltrace_debug
) {
998 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1002 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1003 if (writelen
< readlen
) {
1013 ret
= 0; /* continue reading next packet */
1020 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1021 ret
= fseek(in
, toread
, SEEK_CUR
);
1023 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1030 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1034 size_t size
, buflen
;
1039 * Using strlen on *buf instead of size of open_memstream
1040 * because its size includes garbage at the end (after final
1041 * \0). This is the allocated size, not the actual string size.
1043 out
= babeltrace_open_memstream(buf
, &size
);
1045 perror("Metadata open_memstream");
1049 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
1058 /* close to flush the buffer */
1059 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1063 perror("babeltrace_flush_memstream");
1065 closeret
= fclose(in
);
1067 perror("Error in fclose");
1073 perror("Error in fclose");
1075 /* open for reading */
1076 buflen
= strlen(*buf
);
1081 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1083 perror("Metadata fmemopen");
1090 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1091 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1092 int whence
), FILE *metadata_fp
)
1094 struct ctf_scanner
*scanner
;
1095 struct ctf_file_stream
*metadata_stream
;
1098 int ret
= 0, closeret
;
1100 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1101 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1104 metadata_stream
->pos
.packet_seek
= packet_seek
;
1106 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1113 metadata_stream
->pos
.fd
= -1;
1115 td
->metadata
= &metadata_stream
->parent
;
1116 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1117 if (metadata_stream
->pos
.fd
< 0) {
1118 fprintf(stderr
, "Unable to open metadata.\n");
1123 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1125 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1126 perror("Metadata stream open");
1130 /* fd now belongs to fp */
1131 metadata_stream
->pos
.fd
= -1;
1133 if (babeltrace_debug
)
1136 if (packet_metadata(td
, fp
)) {
1137 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1139 /* Warn about empty metadata */
1140 fprintf(stderr
, "[warning] Empty metadata.\n");
1141 goto end_packet_read
;
1143 td
->metadata_string
= buf
;
1144 td
->metadata_packetized
= 1;
1146 unsigned int major
, minor
;
1149 td
->byte_order
= BYTE_ORDER
;
1151 /* Check text-only metadata header and version */
1152 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1154 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1155 if (check_version(major
, minor
) < 0) {
1157 goto end_packet_read
;
1162 scanner
= ctf_scanner_alloc(fp
);
1164 fprintf(stderr
, "[error] Error allocating scanner\n");
1166 goto end_scanner_alloc
;
1168 ret
= ctf_scanner_append_ast(scanner
);
1170 fprintf(stderr
, "[error] Error creating AST\n");
1174 if (babeltrace_debug
) {
1175 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1177 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1182 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1184 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1187 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1188 td
, td
->byte_order
);
1190 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1194 ctf_scanner_free(scanner
);
1198 closeret
= fclose(fp
);
1200 perror("Error on fclose");
1204 if (metadata_stream
->pos
.fd
>= 0) {
1205 closeret
= close(metadata_stream
->pos
.fd
);
1207 perror("Error on metadata stream fd close");
1212 g_free(metadata_stream
);
1217 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1218 struct ctf_stream_definition
*stream
,
1219 struct ctf_event_declaration
*event
)
1221 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1223 if (event
->context_decl
) {
1224 struct bt_definition
*definition
=
1225 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1226 stream
->parent_def_scope
, 0, 0, "event.context");
1230 stream_event
->event_context
= container_of(definition
,
1231 struct definition_struct
, p
);
1232 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1234 if (event
->fields_decl
) {
1235 struct bt_definition
*definition
=
1236 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1237 stream
->parent_def_scope
, 0, 0, "event.fields");
1241 stream_event
->event_fields
= container_of(definition
,
1242 struct definition_struct
, p
);
1243 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1245 stream_event
->stream
= stream
;
1246 return stream_event
;
1249 if (stream_event
->event_fields
)
1250 bt_definition_unref(&stream_event
->event_fields
->p
);
1251 if (stream_event
->event_context
)
1252 bt_definition_unref(&stream_event
->event_context
->p
);
1253 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1254 g_quark_to_string(event
->name
));
1259 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1261 struct ctf_stream_declaration
*stream_class
;
1265 if (stream
->stream_definitions_created
)
1268 stream_class
= stream
->stream_class
;
1270 if (stream_class
->packet_context_decl
) {
1271 struct bt_definition
*definition
=
1272 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1273 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1278 stream
->stream_packet_context
= container_of(definition
,
1279 struct definition_struct
, p
);
1280 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1282 if (stream_class
->event_header_decl
) {
1283 struct bt_definition
*definition
=
1284 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1285 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1290 stream
->stream_event_header
=
1291 container_of(definition
, struct definition_struct
, p
);
1292 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1294 if (stream_class
->event_context_decl
) {
1295 struct bt_definition
*definition
=
1296 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1297 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1302 stream
->stream_event_context
=
1303 container_of(definition
, struct definition_struct
, p
);
1304 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1306 stream
->events_by_id
= g_ptr_array_new();
1307 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1308 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1309 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1310 struct ctf_event_definition
*stream_event
;
1314 stream_event
= create_event_definitions(td
, stream
, event
);
1315 if (!stream_event
) {
1319 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1324 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1325 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1327 g_free(stream_event
);
1329 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1331 if (stream
->stream_event_context
)
1332 bt_definition_unref(&stream
->stream_event_context
->p
);
1333 if (stream
->stream_event_header
)
1334 bt_definition_unref(&stream
->stream_event_header
->p
);
1335 if (stream
->stream_packet_context
)
1336 bt_definition_unref(&stream
->stream_packet_context
->p
);
1337 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1338 stream_class
->stream_id
, strerror(-ret
));
1343 int stream_assign_class(struct ctf_trace
*td
,
1344 struct ctf_file_stream
*file_stream
,
1347 struct ctf_stream_declaration
*stream
;
1350 file_stream
->parent
.stream_id
= stream_id
;
1351 if (stream_id
>= td
->streams
->len
) {
1352 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1355 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1357 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1360 file_stream
->parent
.stream_class
= stream
;
1361 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1368 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1369 struct ctf_trace
*td
,
1370 struct ctf_file_stream
*file_stream
,
1373 struct packet_index packet_index
;
1374 uint64_t stream_id
= 0;
1375 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1376 int first_packet
= 0;
1381 if (!pos
->mmap_offset
) {
1385 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1386 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1389 if (pos
->base_mma
) {
1390 /* unmap old base */
1391 ret
= munmap_align(pos
->base_mma
);
1393 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1397 pos
->base_mma
= NULL
;
1399 /* map new base. Need mapping length from header. */
1400 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1401 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1402 assert(pos
->base_mma
!= MAP_FAILED
);
1404 * Use current mapping size as temporary content and packet
1407 pos
->content_size
= packet_map_len
;
1408 pos
->packet_size
= packet_map_len
;
1409 pos
->offset
= 0; /* Position of the packet header */
1411 packet_index
.offset
= pos
->mmap_offset
;
1412 packet_index
.content_size
= 0;
1413 packet_index
.packet_size
= 0;
1414 packet_index
.timestamp_begin
= 0;
1415 packet_index
.timestamp_end
= 0;
1416 packet_index
.events_discarded
= 0;
1417 packet_index
.events_discarded_len
= 0;
1419 /* read and check header, set stream id (and check) */
1420 if (file_stream
->parent
.trace_packet_header
) {
1421 /* Read packet header */
1422 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1426 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1429 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1430 if (len_index
>= 0) {
1431 struct bt_definition
*field
;
1434 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1435 magic
= bt_get_unsigned_int(field
);
1436 if (magic
!= CTF_MAGIC
) {
1437 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1439 file_stream
->pos
.packet_cycles_index
->len
,
1440 (ssize_t
) pos
->mmap_offset
);
1446 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1447 if (len_index
>= 0) {
1448 struct definition_array
*defarray
;
1449 struct bt_definition
*field
;
1451 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1453 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1454 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1455 defarray
= container_of(field
, struct definition_array
, p
);
1456 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1458 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1459 struct bt_definition
*elem
;
1461 elem
= bt_array_index(defarray
, i
);
1462 uuidval
[i
] = bt_get_unsigned_int(elem
);
1464 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1466 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1471 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1472 if (len_index
>= 0) {
1473 struct bt_definition
*field
;
1475 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1476 stream_id
= bt_get_unsigned_int(field
);
1480 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1481 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1483 file_stream
->parent
.stream_id
);
1487 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1492 if (file_stream
->parent
.stream_packet_context
) {
1493 /* Read packet context */
1494 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1498 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1501 /* read packet size from header */
1502 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1503 if (len_index
>= 0) {
1504 struct bt_definition
*field
;
1506 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1507 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1509 /* Use file size for packet size */
1510 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1513 /* read content size from header */
1514 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1515 if (len_index
>= 0) {
1516 struct bt_definition
*field
;
1518 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1519 packet_index
.content_size
= bt_get_unsigned_int(field
);
1521 /* Use packet size if non-zero, else file size */
1522 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1525 /* read timestamp begin from header */
1526 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1527 if (len_index
>= 0) {
1528 struct bt_definition
*field
;
1530 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1531 packet_index
.timestamp_begin
= bt_get_unsigned_int(field
);
1532 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1533 packet_index
.timestamp_begin
=
1534 ctf_get_real_timestamp(
1535 &file_stream
->parent
,
1536 packet_index
.timestamp_begin
);
1540 /* read timestamp end from header */
1541 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1542 if (len_index
>= 0) {
1543 struct bt_definition
*field
;
1545 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1546 packet_index
.timestamp_end
= bt_get_unsigned_int(field
);
1547 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1548 packet_index
.timestamp_end
=
1549 ctf_get_real_timestamp(
1550 &file_stream
->parent
,
1551 packet_index
.timestamp_end
);
1555 /* read events discarded from header */
1556 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1557 if (len_index
>= 0) {
1558 struct bt_definition
*field
;
1560 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1561 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1562 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1565 /* Use file size for packet size */
1566 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1567 /* Use packet size if non-zero, else file size */
1568 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1571 /* Validate content size and packet size values */
1572 if (packet_index
.content_size
> packet_index
.packet_size
) {
1573 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1574 packet_index
.content_size
, packet_index
.packet_size
);
1578 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1579 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1580 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1584 if (packet_index
.content_size
< pos
->offset
) {
1585 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1589 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1590 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1594 /* Save position after header and context */
1595 packet_index
.data_offset
= pos
->offset
;
1597 /* add index to packet array */
1598 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1600 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1604 /* Retry with larger mapping */
1606 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1608 * Reached EOF, but still expecting header/context data.
1610 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1613 /* Double the mapping len, and retry */
1614 tmp_map_len
= packet_map_len
<< 1;
1615 if (tmp_map_len
>> 1 != packet_map_len
) {
1617 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1620 packet_map_len
= tmp_map_len
;
1625 int create_stream_packet_index(struct ctf_trace
*td
,
1626 struct ctf_file_stream
*file_stream
)
1628 struct ctf_stream_pos
*pos
;
1629 struct stat filestats
;
1632 pos
= &file_stream
->pos
;
1634 ret
= fstat(pos
->fd
, &filestats
);
1638 /* Deal with empty files */
1639 if (!filestats
.st_size
) {
1640 if (file_stream
->parent
.trace_packet_header
1641 || file_stream
->parent
.stream_packet_context
) {
1643 * We expect a trace packet header and/or stream packet
1644 * context. Since a trace needs to have at least one
1645 * packet, empty files are therefore not accepted.
1647 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1651 * Without trace packet header nor stream packet
1652 * context, a one-packet trace can indeed be empty. This
1653 * is only valid if there is only one stream class: 0.
1655 ret
= stream_assign_class(td
, file_stream
, 0);
1662 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1663 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1672 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1676 if (td
->packet_header_decl
) {
1677 struct bt_definition
*definition
=
1678 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1679 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1684 stream
->trace_packet_header
=
1685 container_of(definition
, struct definition_struct
, p
);
1686 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1692 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1697 int import_stream_packet_index(struct ctf_trace
*td
,
1698 struct ctf_file_stream
*file_stream
)
1700 struct ctf_stream_declaration
*stream
;
1701 struct ctf_stream_pos
*pos
;
1702 struct ctf_packet_index ctf_index
;
1703 struct ctf_packet_index_file_hdr index_hdr
;
1704 struct packet_index index
;
1707 int first_packet
= 1;
1710 pos
= &file_stream
->pos
;
1712 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
1714 perror("read index file header");
1718 /* Check the index header */
1719 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
1720 fprintf(stderr
, "[error] wrong index magic\n");
1724 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
1725 fprintf(stderr
, "[error] Incompatible index file %" PRIu64
1726 ".%" PRIu64
", supported %d.%d\n",
1727 be64toh(index_hdr
.index_major
),
1728 be64toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
1733 if (index_hdr
.packet_index_len
== 0) {
1734 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
1739 while ((index_read
= fread(&ctf_index
, index_hdr
.packet_index_len
, 1,
1740 pos
->index_fp
)) == 1) {
1743 memset(&index
, 0, sizeof(index
));
1744 index
.offset
= be64toh(ctf_index
.offset
);
1745 index
.packet_size
= be64toh(ctf_index
.packet_size
);
1746 index
.content_size
= be64toh(ctf_index
.content_size
);
1747 index
.timestamp_begin
= be64toh(ctf_index
.timestamp_begin
);
1748 index
.timestamp_end
= be64toh(ctf_index
.timestamp_end
);
1749 index
.events_discarded
= be64toh(ctf_index
.events_discarded
);
1750 index
.events_discarded_len
= 64;
1751 stream_id
= be64toh(ctf_index
.stream_id
);
1753 if (!first_packet
) {
1754 /* add index to packet array */
1755 g_array_append_val(file_stream
->pos
.packet_cycles_index
, index
);
1759 file_stream
->parent
.stream_id
= stream_id
;
1760 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1762 fprintf(stderr
, "[error] Stream %" PRIu64
1763 " is not declared in metadata.\n",
1768 file_stream
->parent
.stream_class
= stream
;
1769 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1773 /* add index to packet array */
1774 g_array_append_val(file_stream
->pos
.packet_cycles_index
, index
);
1784 * Note: many file streams can inherit from the same stream class
1785 * description (metadata).
1788 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1789 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1792 int ret
, fd
, closeret
;
1793 struct ctf_file_stream
*file_stream
;
1794 struct stat statbuf
;
1797 fd
= openat(td
->dirfd
, path
, flags
);
1799 perror("File stream openat()");
1804 /* Don't try to mmap subdirectories. Skip them, return success. */
1805 ret
= fstat(fd
, &statbuf
);
1807 perror("File stream fstat()");
1810 if (S_ISDIR(statbuf
.st_mode
)) {
1811 if (strncmp(path
, "index", 5) != 0) {
1812 fprintf(stderr
, "[warning] Skipping directory '%s' "
1813 "found in trace\n", path
);
1819 file_stream
= g_new0(struct ctf_file_stream
, 1);
1820 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1821 file_stream
->pos
.fd
= -1;
1822 file_stream
->pos
.index_fp
= NULL
;
1824 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
1825 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
1828 file_stream
->pos
.packet_seek
= packet_seek
;
1830 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1835 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
1838 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1842 * For now, only a single clock per trace is supported.
1844 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
1847 * Allocate the index name for this stream and try to open it.
1849 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
1851 fprintf(stderr
, "[error] Cannot allocate index filename\n");
1854 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
1857 if (faccessat(td
->dirfd
, index_name
, O_RDONLY
, flags
) < 0) {
1858 ret
= create_stream_packet_index(td
, file_stream
);
1860 fprintf(stderr
, "[error] Stream index creation error.\n");
1864 ret
= openat(td
->dirfd
, index_name
, flags
);
1866 perror("Index file openat()");
1870 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
1871 if (!file_stream
->pos
.index_fp
) {
1872 perror("fdopen() error");
1875 ret
= import_stream_packet_index(td
, file_stream
);
1880 ret
= fclose(file_stream
->pos
.index_fp
);
1882 perror("close index");
1888 /* Add stream file to stream class */
1889 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1890 &file_stream
->parent
);
1894 if (file_stream
->pos
.index_fp
) {
1895 ret
= fclose(file_stream
->pos
.index_fp
);
1897 perror("close index");
1900 if (file_stream
->parent
.trace_packet_header
)
1901 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1905 closeret
= ctf_fini_pos(&file_stream
->pos
);
1907 fprintf(stderr
, "Error on ctf_fini_pos\n");
1909 g_free(file_stream
);
1912 closeret
= close(fd
);
1914 perror("Error on fd close");
1921 int ctf_open_trace_read(struct ctf_trace
*td
,
1922 const char *path
, int flags
,
1923 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1924 int whence
), FILE *metadata_fp
)
1927 struct dirent
*dirent
;
1928 struct dirent
*diriter
;
1934 /* Open trace directory */
1935 td
->dir
= opendir(path
);
1937 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1942 td
->dirfd
= open(path
, 0);
1943 if (td
->dirfd
< 0) {
1944 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1945 perror("Trace directory open");
1949 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
1950 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
1953 * Keep the metadata file separate.
1956 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1958 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1959 goto error_metadata
;
1963 * Open each stream: for each file, try to open, check magic
1964 * number, and get the stream ID to add to the right location in
1968 dirent_len
= offsetof(struct dirent
, d_name
) +
1969 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1971 dirent
= malloc(dirent_len
);
1974 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1976 fprintf(stderr
, "[error] Readdir error.\n");
1981 /* Ignore hidden files, ., .. and metadata. */
1982 if (!strncmp(diriter
->d_name
, ".", 1)
1983 || !strcmp(diriter
->d_name
, "..")
1984 || !strcmp(diriter
->d_name
, "metadata"))
1987 /* Ignore index files : *.idx */
1988 ext
= strrchr(diriter
->d_name
, '.');
1989 if (ext
&& (!strcmp(ext
, ".idx"))) {
1993 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1994 flags
, packet_seek
);
1996 fprintf(stderr
, "[error] Open file stream error.\n");
2007 closeret
= close(td
->dirfd
);
2009 perror("Error on fd close");
2012 closeret
= closedir(td
->dir
);
2014 perror("Error on closedir");
2021 * ctf_open_trace: Open a CTF trace and index it.
2022 * Note that the user must seek the trace after the open (using the iterator)
2023 * since the index creation read it entirely.
2026 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2027 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2028 int whence
), FILE *metadata_fp
)
2030 struct ctf_trace
*td
;
2034 * If packet_seek is NULL, we provide our default version.
2037 packet_seek
= ctf_packet_seek
;
2039 td
= g_new0(struct ctf_trace
, 1);
2041 switch (flags
& O_ACCMODE
) {
2044 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2047 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2052 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2055 fprintf(stderr
, "[error] Incorrect open flags.\n");
2066 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2067 struct bt_mmap_stream
*mmap_info
)
2069 pos
->mmap_offset
= 0;
2070 pos
->packet_size
= 0;
2071 pos
->content_size
= 0;
2072 pos
->content_size_loc
= NULL
;
2073 pos
->fd
= mmap_info
->fd
;
2074 pos
->base_mma
= NULL
;
2078 pos
->packet_cycles_index
= NULL
;
2079 pos
->packet_real_index
= NULL
;
2080 pos
->prot
= PROT_READ
;
2081 pos
->flags
= MAP_PRIVATE
;
2082 pos
->parent
.rw_table
= read_dispatch_table
;
2083 pos
->parent
.event_cb
= ctf_read_event
;
2087 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2088 struct ctf_file_stream
*file_stream
)
2090 struct ctf_stream_declaration
*stream
;
2091 uint64_t stream_id
= 0;
2094 file_stream
->parent
.stream_id
= stream_id
;
2095 if (stream_id
>= td
->streams
->len
) {
2096 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2097 "in metadata.\n", stream_id
);
2101 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2103 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2104 "in metadata.\n", stream_id
);
2108 file_stream
->parent
.stream_class
= stream
;
2109 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2115 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2116 struct bt_mmap_stream
*mmap_info
,
2117 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2121 struct ctf_file_stream
*file_stream
;
2123 file_stream
= g_new0(struct ctf_file_stream
, 1);
2124 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2125 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2127 file_stream
->pos
.packet_seek
= packet_seek
;
2129 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2134 ret
= prepare_mmap_stream_definition(td
, file_stream
);
2139 * For now, only a single clock per trace is supported.
2141 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2143 /* Add stream file to stream class */
2144 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2145 &file_stream
->parent
);
2149 if (file_stream
->parent
.trace_packet_header
)
2150 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2152 g_free(file_stream
);
2157 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2158 struct bt_mmap_stream_list
*mmap_list
,
2159 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2164 struct bt_mmap_stream
*mmap_info
;
2166 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
2172 * for each stream, try to open, check magic number, and get the
2173 * stream ID to add to the right location in the stream array.
2175 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2176 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2178 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2190 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2191 struct bt_mmap_stream_list
*mmap_list
,
2192 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2196 struct ctf_trace
*td
;
2200 fprintf(stderr
, "[error] No metadata file pointer associated, "
2201 "required for mmap parsing\n");
2205 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2208 td
= g_new0(struct ctf_trace
, 1);
2209 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2222 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2225 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2227 /* for each stream_class */
2228 for (i
= 0; i
< td
->streams
->len
; i
++) {
2229 struct ctf_stream_declaration
*stream_class
;
2231 stream_class
= g_ptr_array_index(td
->streams
, i
);
2234 /* for each file_stream */
2235 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2236 struct ctf_stream_definition
*stream
;
2237 struct ctf_stream_pos
*stream_pos
;
2238 struct ctf_file_stream
*cfs
;
2240 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2243 cfs
= container_of(stream
, struct ctf_file_stream
,
2245 stream_pos
= &cfs
->pos
;
2246 if (!stream_pos
->packet_cycles_index
)
2249 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
2250 struct packet_index
*index
;
2251 struct packet_index new_index
;
2253 index
= &g_array_index(stream_pos
->packet_cycles_index
,
2254 struct packet_index
, k
);
2255 memcpy(&new_index
, index
,
2256 sizeof(struct packet_index
));
2257 new_index
.timestamp_begin
=
2258 ctf_get_real_timestamp(stream
,
2259 index
->timestamp_begin
);
2260 new_index
.timestamp_end
=
2261 ctf_get_real_timestamp(stream
,
2262 index
->timestamp_end
);
2263 g_array_append_val(stream_pos
->packet_real_index
,
2272 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2276 ret
= ctf_fini_pos(&file_stream
->pos
);
2278 fprintf(stderr
, "Error on ctf_fini_pos\n");
2281 ret
= close(file_stream
->pos
.fd
);
2283 perror("Error closing file fd");
2290 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2292 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2298 for (i
= 0; i
< td
->streams
->len
; i
++) {
2299 struct ctf_stream_declaration
*stream
;
2302 stream
= g_ptr_array_index(td
->streams
, i
);
2305 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2306 struct ctf_file_stream
*file_stream
;
2307 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2308 struct ctf_file_stream
, parent
);
2309 ret
= ctf_close_file_stream(file_stream
);
2315 ctf_destroy_metadata(td
);
2316 ret
= close(td
->dirfd
);
2318 perror("Error closing dirfd");
2321 ret
= closedir(td
->dir
);
2323 perror("Error closedir");
2326 free(td
->metadata_string
);
2332 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2333 struct bt_context
*ctx
)
2335 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2338 td
->parent
.ctx
= ctx
;
2342 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2343 struct bt_trace_handle
*handle
)
2345 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2348 td
->parent
.handle
= handle
;
2352 void __attribute__((constructor
)) ctf_init(void)
2356 ctf_format
.name
= g_quark_from_static_string("ctf");
2357 ret
= bt_register_format(&ctf_format
);
2362 void __attribute__((destructor
)) ctf_exit(void)
2364 bt_unregister_format(&ctf_format
);