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");
631 * One side-effect of this function is to unmap pos mmap base if one is
635 int find_data_offset(struct ctf_stream_pos
*pos
,
636 struct ctf_file_stream
*file_stream
,
637 struct packet_index
*packet_index
)
639 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
640 struct stat filestats
;
644 pos
= &file_stream
->pos
;
646 ret
= fstat(pos
->fd
, &filestats
);
649 filesize
= filestats
.st_size
;
651 /* Deal with empty files */
657 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
658 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
663 ret
= munmap_align(pos
->base_mma
);
665 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
669 pos
->base_mma
= NULL
;
671 /* map new base. Need mapping length from header. */
672 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
673 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
674 assert(pos
->base_mma
!= MAP_FAILED
);
676 pos
->content_size
= packet_map_len
;
677 pos
->packet_size
= packet_map_len
;
678 pos
->offset
= 0; /* Position of the packet header */
680 /* update trace_packet_header and stream_packet_context */
681 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
682 /* Read packet header */
683 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
689 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
690 /* Read packet context */
691 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
697 packet_index
->data_offset
= pos
->offset
;
700 ret
= munmap_align(pos
->base_mma
);
702 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
706 pos
->base_mma
= NULL
;
710 /* Retry with larger mapping */
712 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
714 * Reached EOF, but still expecting header/context data.
716 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
719 /* Double the mapping len, and retry */
720 tmp_map_len
= packet_map_len
<< 1;
721 if (tmp_map_len
>> 1 != packet_map_len
) {
723 fprintf(stderr
, "[error] Packet mapping length overflow\n");
726 packet_map_len
= tmp_map_len
;
731 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
732 int fd
, int open_flags
)
736 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
737 sizeof(struct packet_index
));
738 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
739 sizeof(struct packet_index
));
741 pos
->packet_cycles_index
= NULL
;
742 pos
->packet_real_index
= NULL
;
744 switch (open_flags
& O_ACCMODE
) {
746 pos
->prot
= PROT_READ
;
747 pos
->flags
= MAP_PRIVATE
;
748 pos
->parent
.rw_table
= read_dispatch_table
;
749 pos
->parent
.event_cb
= ctf_read_event
;
750 pos
->parent
.trace
= trace
;
753 pos
->prot
= PROT_WRITE
; /* Write has priority */
754 pos
->flags
= MAP_SHARED
;
755 pos
->parent
.rw_table
= write_dispatch_table
;
756 pos
->parent
.event_cb
= ctf_write_event
;
757 pos
->parent
.trace
= trace
;
759 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
767 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
769 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
770 *pos
->content_size_loc
= pos
->offset
;
775 ret
= munmap_align(pos
->base_mma
);
777 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
782 if (pos
->packet_cycles_index
)
783 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
784 if (pos
->packet_real_index
)
785 (void) g_array_free(pos
->packet_real_index
, TRUE
);
790 * for SEEK_CUR: go to next packet.
791 * for SEEK_SET: go to packet numer (index).
793 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
795 struct ctf_stream_pos
*pos
=
796 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
797 struct ctf_file_stream
*file_stream
=
798 container_of(pos
, struct ctf_file_stream
, pos
);
801 struct packet_index
*packet_index
;
805 case SEEK_SET
: /* Fall-through */
811 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
812 *pos
->content_size_loc
= pos
->offset
;
816 ret
= munmap_align(pos
->base_mma
);
818 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
822 pos
->base_mma
= NULL
;
826 * The caller should never ask for ctf_move_pos across packets,
827 * except to get exactly at the beginning of the next packet.
829 if (pos
->prot
== PROT_WRITE
) {
832 /* The writer will add padding */
833 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
836 assert(index
== 0); /* only seek supported for now */
842 pos
->content_size
= -1U; /* Unknown at this point */
843 pos
->packet_size
= WRITE_PACKET_LEN
;
844 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
845 pos
->packet_size
/ CHAR_BIT
);
853 uint64_t events_discarded_diff
;
855 if (pos
->offset
== EOF
) {
858 assert(pos
->cur_index
< pos
->packet_cycles_index
->len
);
859 assert(pos
->cur_index
< pos
->packet_real_index
->len
);
861 /* For printing discarded event count */
862 packet_index
= &g_array_index(pos
->packet_cycles_index
,
863 struct packet_index
, pos
->cur_index
);
864 file_stream
->parent
.prev_cycles_timestamp_end
=
865 packet_index
->timestamp_end
;
866 file_stream
->parent
.prev_cycles_timestamp
=
867 packet_index
->timestamp_begin
;
869 packet_index
= &g_array_index(pos
->packet_real_index
,
870 struct packet_index
, pos
->cur_index
);
871 file_stream
->parent
.prev_real_timestamp_end
=
872 packet_index
->timestamp_end
;
873 file_stream
->parent
.prev_real_timestamp
=
874 packet_index
->timestamp_begin
;
876 events_discarded_diff
= packet_index
->events_discarded
;
877 if (pos
->cur_index
> 0) {
878 packet_index
= &g_array_index(pos
->packet_real_index
,
881 events_discarded_diff
-= packet_index
->events_discarded
;
883 * Deal with 32-bit wrap-around if the
884 * tracer provided a 32-bit field.
886 if (packet_index
->events_discarded_len
== 32) {
887 events_discarded_diff
= (uint32_t) events_discarded_diff
;
890 file_stream
->parent
.events_discarded
= events_discarded_diff
;
891 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
892 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
893 /* The reader will expect us to skip padding */
898 if (index
>= pos
->packet_cycles_index
->len
) {
902 packet_index
= &g_array_index(pos
->packet_cycles_index
,
903 struct packet_index
, index
);
904 pos
->last_events_discarded
= packet_index
->events_discarded
;
905 pos
->cur_index
= index
;
906 file_stream
->parent
.prev_real_timestamp
= 0;
907 file_stream
->parent
.prev_real_timestamp_end
= 0;
908 file_stream
->parent
.prev_cycles_timestamp
= 0;
909 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
914 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
916 * We need to check if we are in trace read or
917 * called from packet indexing. In this last
918 * case, the collection is not there, so we
919 * cannot print the timestamps.
921 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
923 * When a stream reaches the end of the
924 * file, we need to show the number of
925 * events discarded ourselves, because
926 * there is no next event scheduled to
927 * be printed in the output.
929 if (file_stream
->parent
.events_discarded
) {
931 ctf_print_discarded(stderr
,
932 &file_stream
->parent
,
934 file_stream
->parent
.events_discarded
= 0;
940 packet_index
= &g_array_index(pos
->packet_cycles_index
,
943 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
945 packet_index
= &g_array_index(pos
->packet_real_index
,
948 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
950 /* Lookup context/packet size in index */
951 if (packet_index
->data_offset
== -1) {
952 ret
= find_data_offset(pos
, file_stream
, packet_index
);
957 pos
->content_size
= packet_index
->content_size
;
958 pos
->packet_size
= packet_index
->packet_size
;
959 pos
->mmap_offset
= packet_index
->offset
;
960 if (packet_index
->data_offset
< packet_index
->content_size
) {
961 pos
->offset
= 0; /* will read headers */
962 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
964 pos
->offset
= packet_index
->data_offset
;
966 goto read_next_packet
;
972 /* map new base. Need mapping length from header. */
973 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
974 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
975 if (pos
->base_mma
== MAP_FAILED
) {
976 fprintf(stderr
, "[error] mmap error %s.\n",
981 /* update trace_packet_header and stream_packet_context */
982 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
983 /* Read packet header */
984 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
987 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
988 /* Read packet context */
989 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
995 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1001 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1005 if (magic
== TSDL_MAGIC
) {
1007 td
->byte_order
= BYTE_ORDER
;
1008 CTF_TRACE_SET_FIELD(td
, byte_order
);
1009 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1011 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1012 LITTLE_ENDIAN
: BIG_ENDIAN
;
1013 CTF_TRACE_SET_FIELD(td
, byte_order
);
1021 * Returns 0 on success, -1 on error.
1024 int check_version(unsigned int major
, unsigned int minor
)
1039 /* eventually return an error instead of warning */
1041 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1047 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1050 struct metadata_packet_header header
;
1051 size_t readlen
, writelen
, toread
;
1052 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1055 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1059 if (td
->byte_order
!= BYTE_ORDER
) {
1060 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1061 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1062 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1063 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1065 if (header
.checksum
)
1066 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1067 if (header
.compression_scheme
) {
1068 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1069 header
.compression_scheme
);
1072 if (header
.encryption_scheme
) {
1073 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1074 header
.encryption_scheme
);
1077 if (header
.checksum_scheme
) {
1078 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1079 header
.checksum_scheme
);
1082 if (check_version(header
.major
, header
.minor
) < 0)
1084 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1085 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1086 CTF_TRACE_SET_FIELD(td
, uuid
);
1088 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
1092 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1095 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1098 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1103 if (babeltrace_debug
) {
1104 buf
[readlen
] = '\0';
1105 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1109 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1110 if (writelen
< readlen
) {
1120 ret
= 0; /* continue reading next packet */
1127 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1128 ret
= fseek(in
, toread
, SEEK_CUR
);
1130 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1137 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1141 size_t size
, buflen
;
1146 * Using strlen on *buf instead of size of open_memstream
1147 * because its size includes garbage at the end (after final
1148 * \0). This is the allocated size, not the actual string size.
1150 out
= babeltrace_open_memstream(buf
, &size
);
1152 perror("Metadata open_memstream");
1156 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
1165 /* close to flush the buffer */
1166 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1170 perror("babeltrace_flush_memstream");
1172 closeret
= fclose(in
);
1174 perror("Error in fclose");
1180 perror("Error in fclose");
1182 /* open for reading */
1183 buflen
= strlen(*buf
);
1188 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1190 perror("Metadata fmemopen");
1197 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1198 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1199 int whence
), FILE *metadata_fp
)
1201 struct ctf_scanner
*scanner
;
1202 struct ctf_file_stream
*metadata_stream
;
1205 int ret
= 0, closeret
;
1207 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1208 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1211 metadata_stream
->pos
.packet_seek
= packet_seek
;
1213 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1220 metadata_stream
->pos
.fd
= -1;
1222 td
->metadata
= &metadata_stream
->parent
;
1223 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1224 if (metadata_stream
->pos
.fd
< 0) {
1225 fprintf(stderr
, "Unable to open metadata.\n");
1230 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1232 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1233 perror("Metadata stream open");
1237 /* fd now belongs to fp */
1238 metadata_stream
->pos
.fd
= -1;
1240 if (babeltrace_debug
)
1243 if (packet_metadata(td
, fp
)) {
1244 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1246 /* Warn about empty metadata */
1247 fprintf(stderr
, "[warning] Empty metadata.\n");
1248 goto end_packet_read
;
1250 td
->metadata_string
= buf
;
1251 td
->metadata_packetized
= 1;
1253 unsigned int major
, minor
;
1256 td
->byte_order
= BYTE_ORDER
;
1258 /* Check text-only metadata header and version */
1259 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1261 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1262 if (check_version(major
, minor
) < 0) {
1264 goto end_packet_read
;
1269 scanner
= ctf_scanner_alloc(fp
);
1271 fprintf(stderr
, "[error] Error allocating scanner\n");
1273 goto end_scanner_alloc
;
1275 ret
= ctf_scanner_append_ast(scanner
);
1277 fprintf(stderr
, "[error] Error creating AST\n");
1281 if (babeltrace_debug
) {
1282 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1284 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1289 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1291 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1294 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1295 td
, td
->byte_order
);
1297 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1301 ctf_scanner_free(scanner
);
1305 closeret
= fclose(fp
);
1307 perror("Error on fclose");
1311 if (metadata_stream
->pos
.fd
>= 0) {
1312 closeret
= close(metadata_stream
->pos
.fd
);
1314 perror("Error on metadata stream fd close");
1319 g_free(metadata_stream
);
1324 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1325 struct ctf_stream_definition
*stream
,
1326 struct ctf_event_declaration
*event
)
1328 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1330 if (event
->context_decl
) {
1331 struct bt_definition
*definition
=
1332 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1333 stream
->parent_def_scope
, 0, 0, "event.context");
1337 stream_event
->event_context
= container_of(definition
,
1338 struct definition_struct
, p
);
1339 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1341 if (event
->fields_decl
) {
1342 struct bt_definition
*definition
=
1343 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1344 stream
->parent_def_scope
, 0, 0, "event.fields");
1348 stream_event
->event_fields
= container_of(definition
,
1349 struct definition_struct
, p
);
1350 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1352 stream_event
->stream
= stream
;
1353 return stream_event
;
1356 if (stream_event
->event_fields
)
1357 bt_definition_unref(&stream_event
->event_fields
->p
);
1358 if (stream_event
->event_context
)
1359 bt_definition_unref(&stream_event
->event_context
->p
);
1360 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1361 g_quark_to_string(event
->name
));
1366 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1368 struct ctf_stream_declaration
*stream_class
;
1372 if (stream
->stream_definitions_created
)
1375 stream_class
= stream
->stream_class
;
1377 if (stream_class
->packet_context_decl
) {
1378 struct bt_definition
*definition
=
1379 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1380 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1385 stream
->stream_packet_context
= container_of(definition
,
1386 struct definition_struct
, p
);
1387 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1389 if (stream_class
->event_header_decl
) {
1390 struct bt_definition
*definition
=
1391 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1392 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1397 stream
->stream_event_header
=
1398 container_of(definition
, struct definition_struct
, p
);
1399 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1401 if (stream_class
->event_context_decl
) {
1402 struct bt_definition
*definition
=
1403 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1404 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1409 stream
->stream_event_context
=
1410 container_of(definition
, struct definition_struct
, p
);
1411 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1413 stream
->events_by_id
= g_ptr_array_new();
1414 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1415 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1416 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1417 struct ctf_event_definition
*stream_event
;
1421 stream_event
= create_event_definitions(td
, stream
, event
);
1422 if (!stream_event
) {
1426 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1431 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1432 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1434 g_free(stream_event
);
1436 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1438 if (stream
->stream_event_context
)
1439 bt_definition_unref(&stream
->stream_event_context
->p
);
1440 if (stream
->stream_event_header
)
1441 bt_definition_unref(&stream
->stream_event_header
->p
);
1442 if (stream
->stream_packet_context
)
1443 bt_definition_unref(&stream
->stream_packet_context
->p
);
1444 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1445 stream_class
->stream_id
, strerror(-ret
));
1450 int stream_assign_class(struct ctf_trace
*td
,
1451 struct ctf_file_stream
*file_stream
,
1454 struct ctf_stream_declaration
*stream
;
1457 file_stream
->parent
.stream_id
= stream_id
;
1458 if (stream_id
>= td
->streams
->len
) {
1459 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1462 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1464 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1467 file_stream
->parent
.stream_class
= stream
;
1468 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1475 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1476 struct ctf_trace
*td
,
1477 struct ctf_file_stream
*file_stream
,
1480 struct packet_index packet_index
;
1481 uint64_t stream_id
= 0;
1482 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1483 int first_packet
= 0;
1488 if (!pos
->mmap_offset
) {
1492 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1493 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1496 if (pos
->base_mma
) {
1497 /* unmap old base */
1498 ret
= munmap_align(pos
->base_mma
);
1500 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1504 pos
->base_mma
= NULL
;
1506 /* map new base. Need mapping length from header. */
1507 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1508 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1509 assert(pos
->base_mma
!= MAP_FAILED
);
1511 * Use current mapping size as temporary content and packet
1514 pos
->content_size
= packet_map_len
;
1515 pos
->packet_size
= packet_map_len
;
1516 pos
->offset
= 0; /* Position of the packet header */
1518 packet_index
.offset
= pos
->mmap_offset
;
1519 packet_index
.content_size
= 0;
1520 packet_index
.packet_size
= 0;
1521 packet_index
.timestamp_begin
= 0;
1522 packet_index
.timestamp_end
= 0;
1523 packet_index
.events_discarded
= 0;
1524 packet_index
.events_discarded_len
= 0;
1526 /* read and check header, set stream id (and check) */
1527 if (file_stream
->parent
.trace_packet_header
) {
1528 /* Read packet header */
1529 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1533 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1536 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1537 if (len_index
>= 0) {
1538 struct bt_definition
*field
;
1541 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1542 magic
= bt_get_unsigned_int(field
);
1543 if (magic
!= CTF_MAGIC
) {
1544 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1546 file_stream
->pos
.packet_cycles_index
->len
,
1547 (ssize_t
) pos
->mmap_offset
);
1553 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1554 if (len_index
>= 0) {
1555 struct definition_array
*defarray
;
1556 struct bt_definition
*field
;
1558 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1560 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1561 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1562 defarray
= container_of(field
, struct definition_array
, p
);
1563 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1565 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1566 struct bt_definition
*elem
;
1568 elem
= bt_array_index(defarray
, i
);
1569 uuidval
[i
] = bt_get_unsigned_int(elem
);
1571 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1573 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1578 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1579 if (len_index
>= 0) {
1580 struct bt_definition
*field
;
1582 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1583 stream_id
= bt_get_unsigned_int(field
);
1587 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1588 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1590 file_stream
->parent
.stream_id
);
1594 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1599 if (file_stream
->parent
.stream_packet_context
) {
1600 /* Read packet context */
1601 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1605 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1608 /* read packet size from header */
1609 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1610 if (len_index
>= 0) {
1611 struct bt_definition
*field
;
1613 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1614 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1616 /* Use file size for packet size */
1617 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1620 /* read content size from header */
1621 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1622 if (len_index
>= 0) {
1623 struct bt_definition
*field
;
1625 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1626 packet_index
.content_size
= bt_get_unsigned_int(field
);
1628 /* Use packet size if non-zero, else file size */
1629 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1632 /* read timestamp begin from header */
1633 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1634 if (len_index
>= 0) {
1635 struct bt_definition
*field
;
1637 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1638 packet_index
.timestamp_begin
= bt_get_unsigned_int(field
);
1639 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1640 packet_index
.timestamp_begin
=
1641 ctf_get_real_timestamp(
1642 &file_stream
->parent
,
1643 packet_index
.timestamp_begin
);
1647 /* read timestamp end from header */
1648 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1649 if (len_index
>= 0) {
1650 struct bt_definition
*field
;
1652 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1653 packet_index
.timestamp_end
= bt_get_unsigned_int(field
);
1654 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1655 packet_index
.timestamp_end
=
1656 ctf_get_real_timestamp(
1657 &file_stream
->parent
,
1658 packet_index
.timestamp_end
);
1662 /* read events discarded from header */
1663 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1664 if (len_index
>= 0) {
1665 struct bt_definition
*field
;
1667 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1668 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1669 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1672 /* Use file size for packet size */
1673 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1674 /* Use packet size if non-zero, else file size */
1675 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1678 /* Validate content size and packet size values */
1679 if (packet_index
.content_size
> packet_index
.packet_size
) {
1680 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1681 packet_index
.content_size
, packet_index
.packet_size
);
1685 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1686 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1687 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1691 if (packet_index
.content_size
< pos
->offset
) {
1692 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1696 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1697 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1701 /* Save position after header and context */
1702 packet_index
.data_offset
= pos
->offset
;
1704 /* add index to packet array */
1705 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1707 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1711 /* Retry with larger mapping */
1713 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1715 * Reached EOF, but still expecting header/context data.
1717 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1720 /* Double the mapping len, and retry */
1721 tmp_map_len
= packet_map_len
<< 1;
1722 if (tmp_map_len
>> 1 != packet_map_len
) {
1724 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1727 packet_map_len
= tmp_map_len
;
1732 int create_stream_packet_index(struct ctf_trace
*td
,
1733 struct ctf_file_stream
*file_stream
)
1735 struct ctf_stream_pos
*pos
;
1736 struct stat filestats
;
1739 pos
= &file_stream
->pos
;
1741 ret
= fstat(pos
->fd
, &filestats
);
1745 /* Deal with empty files */
1746 if (!filestats
.st_size
) {
1747 if (file_stream
->parent
.trace_packet_header
1748 || file_stream
->parent
.stream_packet_context
) {
1750 * We expect a trace packet header and/or stream packet
1751 * context. Since a trace needs to have at least one
1752 * packet, empty files are therefore not accepted.
1754 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1758 * Without trace packet header nor stream packet
1759 * context, a one-packet trace can indeed be empty. This
1760 * is only valid if there is only one stream class: 0.
1762 ret
= stream_assign_class(td
, file_stream
, 0);
1769 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1770 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1779 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1783 if (td
->packet_header_decl
) {
1784 struct bt_definition
*definition
=
1785 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1786 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1791 stream
->trace_packet_header
=
1792 container_of(definition
, struct definition_struct
, p
);
1793 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1799 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1804 int import_stream_packet_index(struct ctf_trace
*td
,
1805 struct ctf_file_stream
*file_stream
)
1807 struct ctf_stream_declaration
*stream
;
1808 struct ctf_stream_pos
*pos
;
1809 struct ctf_packet_index ctf_index
;
1810 struct ctf_packet_index_file_hdr index_hdr
;
1811 struct packet_index index
;
1813 int first_packet
= 1;
1816 pos
= &file_stream
->pos
;
1818 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
1820 perror("read index file header");
1824 /* Check the index header */
1825 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
1826 fprintf(stderr
, "[error] wrong index magic\n");
1830 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
1831 fprintf(stderr
, "[error] Incompatible index file %" PRIu64
1832 ".%" PRIu64
", supported %d.%d\n",
1833 be64toh(index_hdr
.index_major
),
1834 be64toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
1839 if (index_hdr
.packet_index_len
== 0) {
1840 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
1845 while (fread(&ctf_index
, index_hdr
.packet_index_len
, 1,
1846 pos
->index_fp
) == 1) {
1849 memset(&index
, 0, sizeof(index
));
1850 index
.offset
= be64toh(ctf_index
.offset
);
1851 index
.packet_size
= be64toh(ctf_index
.packet_size
);
1852 index
.content_size
= be64toh(ctf_index
.content_size
);
1853 index
.timestamp_begin
= be64toh(ctf_index
.timestamp_begin
);
1854 index
.timestamp_end
= be64toh(ctf_index
.timestamp_end
);
1855 index
.events_discarded
= be64toh(ctf_index
.events_discarded
);
1856 index
.events_discarded_len
= 64;
1857 index
.data_offset
= -1;
1858 stream_id
= be64toh(ctf_index
.stream_id
);
1860 if (!first_packet
) {
1861 /* add index to packet array */
1862 g_array_append_val(file_stream
->pos
.packet_cycles_index
, index
);
1866 file_stream
->parent
.stream_id
= stream_id
;
1867 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1869 fprintf(stderr
, "[error] Stream %" PRIu64
1870 " is not declared in metadata.\n",
1875 file_stream
->parent
.stream_class
= stream
;
1876 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1880 /* add index to packet array */
1881 g_array_append_val(file_stream
->pos
.packet_cycles_index
, index
);
1891 * Note: many file streams can inherit from the same stream class
1892 * description (metadata).
1895 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1896 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1899 int ret
, fd
, closeret
;
1900 struct ctf_file_stream
*file_stream
;
1901 struct stat statbuf
;
1904 fd
= openat(td
->dirfd
, path
, flags
);
1906 perror("File stream openat()");
1911 /* Don't try to mmap subdirectories. Skip them, return success. */
1912 ret
= fstat(fd
, &statbuf
);
1914 perror("File stream fstat()");
1917 if (S_ISDIR(statbuf
.st_mode
)) {
1918 if (strncmp(path
, "index", 5) != 0) {
1919 fprintf(stderr
, "[warning] Skipping directory '%s' "
1920 "found in trace\n", path
);
1926 file_stream
= g_new0(struct ctf_file_stream
, 1);
1927 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1928 file_stream
->pos
.fd
= -1;
1929 file_stream
->pos
.index_fp
= NULL
;
1931 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
1932 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
1935 file_stream
->pos
.packet_seek
= packet_seek
;
1937 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1942 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
1945 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1949 * For now, only a single clock per trace is supported.
1951 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
1954 * Allocate the index name for this stream and try to open it.
1956 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
1958 fprintf(stderr
, "[error] Cannot allocate index filename\n");
1961 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
1964 if (faccessat(td
->dirfd
, index_name
, O_RDONLY
, flags
) < 0) {
1965 ret
= create_stream_packet_index(td
, file_stream
);
1967 fprintf(stderr
, "[error] Stream index creation error.\n");
1971 ret
= openat(td
->dirfd
, index_name
, flags
);
1973 perror("Index file openat()");
1977 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
1978 if (!file_stream
->pos
.index_fp
) {
1979 perror("fdopen() error");
1982 ret
= import_stream_packet_index(td
, file_stream
);
1987 ret
= fclose(file_stream
->pos
.index_fp
);
1989 perror("close index");
1995 /* Add stream file to stream class */
1996 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1997 &file_stream
->parent
);
2001 if (file_stream
->pos
.index_fp
) {
2002 ret
= fclose(file_stream
->pos
.index_fp
);
2004 perror("close index");
2007 if (file_stream
->parent
.trace_packet_header
)
2008 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2012 closeret
= ctf_fini_pos(&file_stream
->pos
);
2014 fprintf(stderr
, "Error on ctf_fini_pos\n");
2016 g_free(file_stream
);
2019 closeret
= close(fd
);
2021 perror("Error on fd close");
2028 int ctf_open_trace_read(struct ctf_trace
*td
,
2029 const char *path
, int flags
,
2030 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2031 int whence
), FILE *metadata_fp
)
2034 struct dirent
*dirent
;
2035 struct dirent
*diriter
;
2041 /* Open trace directory */
2042 td
->dir
= opendir(path
);
2044 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2049 td
->dirfd
= open(path
, 0);
2050 if (td
->dirfd
< 0) {
2051 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2052 perror("Trace directory open");
2056 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2057 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2060 * Keep the metadata file separate.
2063 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
2065 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2066 goto error_metadata
;
2070 * Open each stream: for each file, try to open, check magic
2071 * number, and get the stream ID to add to the right location in
2075 dirent_len
= offsetof(struct dirent
, d_name
) +
2076 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
2078 dirent
= malloc(dirent_len
);
2081 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2083 fprintf(stderr
, "[error] Readdir error.\n");
2088 /* Ignore hidden files, ., .. and metadata. */
2089 if (!strncmp(diriter
->d_name
, ".", 1)
2090 || !strcmp(diriter
->d_name
, "..")
2091 || !strcmp(diriter
->d_name
, "metadata"))
2094 /* Ignore index files : *.idx */
2095 ext
= strrchr(diriter
->d_name
, '.');
2096 if (ext
&& (!strcmp(ext
, ".idx"))) {
2100 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2101 flags
, packet_seek
);
2103 fprintf(stderr
, "[error] Open file stream error.\n");
2114 closeret
= close(td
->dirfd
);
2116 perror("Error on fd close");
2119 closeret
= closedir(td
->dir
);
2121 perror("Error on closedir");
2128 * ctf_open_trace: Open a CTF trace and index it.
2129 * Note that the user must seek the trace after the open (using the iterator)
2130 * since the index creation read it entirely.
2133 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2134 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2135 int whence
), FILE *metadata_fp
)
2137 struct ctf_trace
*td
;
2141 * If packet_seek is NULL, we provide our default version.
2144 packet_seek
= ctf_packet_seek
;
2146 td
= g_new0(struct ctf_trace
, 1);
2148 switch (flags
& O_ACCMODE
) {
2151 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2154 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2159 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2162 fprintf(stderr
, "[error] Incorrect open flags.\n");
2173 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2174 struct bt_mmap_stream
*mmap_info
)
2176 pos
->mmap_offset
= 0;
2177 pos
->packet_size
= 0;
2178 pos
->content_size
= 0;
2179 pos
->content_size_loc
= NULL
;
2180 pos
->fd
= mmap_info
->fd
;
2181 pos
->base_mma
= NULL
;
2185 pos
->packet_cycles_index
= NULL
;
2186 pos
->packet_real_index
= NULL
;
2187 pos
->prot
= PROT_READ
;
2188 pos
->flags
= MAP_PRIVATE
;
2189 pos
->parent
.rw_table
= read_dispatch_table
;
2190 pos
->parent
.event_cb
= ctf_read_event
;
2194 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2195 struct ctf_file_stream
*file_stream
)
2197 struct ctf_stream_declaration
*stream
;
2198 uint64_t stream_id
= 0;
2201 file_stream
->parent
.stream_id
= stream_id
;
2202 if (stream_id
>= td
->streams
->len
) {
2203 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2204 "in metadata.\n", stream_id
);
2208 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2210 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2211 "in metadata.\n", stream_id
);
2215 file_stream
->parent
.stream_class
= stream
;
2216 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2222 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2223 struct bt_mmap_stream
*mmap_info
,
2224 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2228 struct ctf_file_stream
*file_stream
;
2230 file_stream
= g_new0(struct ctf_file_stream
, 1);
2231 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2232 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2234 file_stream
->pos
.packet_seek
= packet_seek
;
2236 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2241 ret
= prepare_mmap_stream_definition(td
, file_stream
);
2246 * For now, only a single clock per trace is supported.
2248 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2250 /* Add stream file to stream class */
2251 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2252 &file_stream
->parent
);
2256 if (file_stream
->parent
.trace_packet_header
)
2257 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2259 g_free(file_stream
);
2264 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2265 struct bt_mmap_stream_list
*mmap_list
,
2266 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2271 struct bt_mmap_stream
*mmap_info
;
2273 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
2279 * for each stream, try to open, check magic number, and get the
2280 * stream ID to add to the right location in the stream array.
2282 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2283 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2285 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2297 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2298 struct bt_mmap_stream_list
*mmap_list
,
2299 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2303 struct ctf_trace
*td
;
2307 fprintf(stderr
, "[error] No metadata file pointer associated, "
2308 "required for mmap parsing\n");
2312 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2315 td
= g_new0(struct ctf_trace
, 1);
2316 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2329 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2332 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2334 /* for each stream_class */
2335 for (i
= 0; i
< td
->streams
->len
; i
++) {
2336 struct ctf_stream_declaration
*stream_class
;
2338 stream_class
= g_ptr_array_index(td
->streams
, i
);
2341 /* for each file_stream */
2342 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2343 struct ctf_stream_definition
*stream
;
2344 struct ctf_stream_pos
*stream_pos
;
2345 struct ctf_file_stream
*cfs
;
2347 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2350 cfs
= container_of(stream
, struct ctf_file_stream
,
2352 stream_pos
= &cfs
->pos
;
2353 if (!stream_pos
->packet_cycles_index
)
2356 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
2357 struct packet_index
*index
;
2358 struct packet_index new_index
;
2360 index
= &g_array_index(stream_pos
->packet_cycles_index
,
2361 struct packet_index
, k
);
2362 memcpy(&new_index
, index
,
2363 sizeof(struct packet_index
));
2364 new_index
.timestamp_begin
=
2365 ctf_get_real_timestamp(stream
,
2366 index
->timestamp_begin
);
2367 new_index
.timestamp_end
=
2368 ctf_get_real_timestamp(stream
,
2369 index
->timestamp_end
);
2370 g_array_append_val(stream_pos
->packet_real_index
,
2379 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2383 ret
= ctf_fini_pos(&file_stream
->pos
);
2385 fprintf(stderr
, "Error on ctf_fini_pos\n");
2388 ret
= close(file_stream
->pos
.fd
);
2390 perror("Error closing file fd");
2397 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2399 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2405 for (i
= 0; i
< td
->streams
->len
; i
++) {
2406 struct ctf_stream_declaration
*stream
;
2409 stream
= g_ptr_array_index(td
->streams
, i
);
2412 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2413 struct ctf_file_stream
*file_stream
;
2414 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2415 struct ctf_file_stream
, parent
);
2416 ret
= ctf_close_file_stream(file_stream
);
2422 ctf_destroy_metadata(td
);
2423 ret
= close(td
->dirfd
);
2425 perror("Error closing dirfd");
2428 ret
= closedir(td
->dir
);
2430 perror("Error closedir");
2433 free(td
->metadata_string
);
2439 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2440 struct bt_context
*ctx
)
2442 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2445 td
->parent
.ctx
= ctx
;
2449 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2450 struct bt_trace_handle
*handle
)
2452 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2455 td
->parent
.handle
= handle
;
2459 void __attribute__((constructor
)) ctf_init(void)
2463 ctf_format
.name
= g_quark_from_static_string("ctf");
2464 ret
= bt_register_format(&ctf_format
);
2469 void __attribute__((destructor
)) ctf_exit(void)
2471 bt_unregister_format(&ctf_format
);