2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <babeltrace/trace-handle-internal.h>
35 #include <babeltrace/context-internal.h>
36 #include <babeltrace/uuid.h>
37 #include <babeltrace/endian.h>
42 #include <sys/types.h>
50 #include "metadata/ctf-scanner.h"
51 #include "metadata/ctf-parser.h"
52 #include "metadata/ctf-ast.h"
53 #include "events-private.h"
54 #include "memstream.h"
57 * We currently simply map a page to read the packet header and packet
58 * context to get the packet length and content length. (in bits)
60 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
61 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
64 #define min(a, b) (((a) < (b)) ? (a) : (b))
67 #define NSEC_PER_SEC 1000000000ULL
74 uint64_t opt_clock_offset
;
79 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
80 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
84 struct bt_trace_descriptor
*ctf_open_mmap_trace(
85 struct bt_mmap_stream_list
*mmap_list
,
86 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
90 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
91 struct bt_context
*ctx
);
93 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
94 struct bt_trace_handle
*handle
);
97 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
99 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
100 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
102 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
103 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
105 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
108 rw_dispatch read_dispatch_table
[] = {
109 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
110 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
111 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
112 [ CTF_TYPE_STRING
] = ctf_string_read
,
113 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
114 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
115 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
116 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
120 rw_dispatch write_dispatch_table
[] = {
121 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
122 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
123 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
124 [ CTF_TYPE_STRING
] = ctf_string_write
,
125 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
126 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
127 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
128 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
132 struct bt_format ctf_format
= {
133 .open_trace
= ctf_open_trace
,
134 .open_mmap_trace
= ctf_open_mmap_trace
,
135 .close_trace
= ctf_close_trace
,
136 .set_context
= ctf_set_context
,
137 .set_handle
= ctf_set_handle
,
138 .timestamp_begin
= ctf_timestamp_begin
,
139 .timestamp_end
= ctf_timestamp_end
,
140 .convert_index_timestamp
= ctf_convert_index_timestamp
,
144 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
145 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
147 struct ctf_trace
*tin
;
148 uint64_t begin
= ULLONG_MAX
;
151 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
156 /* for each stream_class */
157 for (i
= 0; i
< tin
->streams
->len
; i
++) {
158 struct ctf_stream_declaration
*stream_class
;
160 stream_class
= g_ptr_array_index(tin
->streams
, i
);
163 /* for each file_stream */
164 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
165 struct ctf_stream_definition
*stream
;
166 struct ctf_file_stream
*cfs
;
167 struct ctf_stream_pos
*stream_pos
;
168 struct packet_index
*index
;
170 stream
= g_ptr_array_index(stream_class
->streams
, j
);
171 cfs
= container_of(stream
, struct ctf_file_stream
,
173 stream_pos
= &cfs
->pos
;
175 if (!stream_pos
->packet_real_index
)
178 if (stream_pos
->packet_real_index
->len
<= 0)
181 if (type
== BT_CLOCK_REAL
) {
182 index
= &g_array_index(stream_pos
->packet_real_index
,
184 stream_pos
->packet_real_index
->len
- 1);
185 } else if (type
== BT_CLOCK_CYCLES
) {
186 index
= &g_array_index(stream_pos
->packet_cycles_index
,
188 stream_pos
->packet_real_index
->len
- 1);
193 if (index
->timestamp_begin
< begin
)
194 begin
= index
->timestamp_begin
;
205 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
206 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
208 struct ctf_trace
*tin
;
212 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
217 /* for each stream_class */
218 for (i
= 0; i
< tin
->streams
->len
; i
++) {
219 struct ctf_stream_declaration
*stream_class
;
221 stream_class
= g_ptr_array_index(tin
->streams
, i
);
224 /* for each file_stream */
225 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
226 struct ctf_stream_definition
*stream
;
227 struct ctf_file_stream
*cfs
;
228 struct ctf_stream_pos
*stream_pos
;
229 struct packet_index
*index
;
231 stream
= g_ptr_array_index(stream_class
->streams
, j
);
232 cfs
= container_of(stream
, struct ctf_file_stream
,
234 stream_pos
= &cfs
->pos
;
236 if (!stream_pos
->packet_real_index
)
239 if (stream_pos
->packet_real_index
->len
<= 0)
242 if (type
== BT_CLOCK_REAL
) {
243 index
= &g_array_index(stream_pos
->packet_real_index
,
245 stream_pos
->packet_real_index
->len
- 1);
246 } else if (type
== BT_CLOCK_CYCLES
) {
247 index
= &g_array_index(stream_pos
->packet_cycles_index
,
249 stream_pos
->packet_real_index
->len
- 1);
254 if (index
->timestamp_end
> end
)
255 end
= index
->timestamp_end
;
266 * Update stream current timestamp
269 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
270 struct definition_integer
*integer_definition
)
272 struct declaration_integer
*integer_declaration
=
273 integer_definition
->declaration
;
274 uint64_t oldval
, newval
, updateval
;
276 if (unlikely(integer_declaration
->len
== 64)) {
277 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
278 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
279 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
280 stream
->prev_cycles_timestamp
);
281 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
282 stream
->cycles_timestamp
);
286 oldval
= stream
->cycles_timestamp
;
287 oldval
&= (1ULL << integer_declaration
->len
) - 1;
288 newval
= integer_definition
->value
._unsigned
;
289 /* Test for overflow by comparing low bits */
291 newval
+= 1ULL << integer_declaration
->len
;
292 /* updateval contains old high bits, and new low bits (sum) */
293 updateval
= stream
->cycles_timestamp
;
294 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
296 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
297 stream
->cycles_timestamp
= updateval
;
299 /* convert to real timestamp */
300 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
301 stream
->prev_cycles_timestamp
);
302 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
303 stream
->cycles_timestamp
);
307 * Print timestamp, rescaling clock frequency to nanoseconds and
308 * applying offsets as needed (unix time).
311 void ctf_print_timestamp_real(FILE *fp
,
312 struct ctf_stream_definition
*stream
,
315 uint64_t ts_sec
= 0, ts_nsec
;
319 /* Add command-line offset */
320 ts_sec
+= opt_clock_offset
;
322 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
323 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
325 if (!opt_clock_seconds
) {
327 time_t time_s
= (time_t) ts_sec
;
329 if (!opt_clock_gmt
) {
332 res
= localtime_r(&time_s
, &tm
);
334 fprintf(stderr
, "[warning] Unable to get localtime.\n");
340 res
= gmtime_r(&time_s
, &tm
);
342 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
346 if (opt_clock_date
) {
350 /* Print date and time */
351 res
= strftime(timestr
, sizeof(timestr
),
354 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
357 fprintf(fp
, "%s", timestr
);
359 /* Print time in HH:MM:SS.ns */
360 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
361 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
365 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
373 * Print timestamp, in cycles
376 void ctf_print_timestamp_cycles(FILE *fp
,
377 struct ctf_stream_definition
*stream
,
380 fprintf(fp
, "%020" PRIu64
, timestamp
);
383 void ctf_print_timestamp(FILE *fp
,
384 struct ctf_stream_definition
*stream
,
387 if (opt_clock_cycles
) {
388 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
390 ctf_print_timestamp_real(fp
, stream
, timestamp
);
395 void print_uuid(FILE *fp
, unsigned char *uuid
)
399 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
400 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
403 void ctf_print_discarded(FILE *fp
, struct ctf_stream_definition
*stream
,
406 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events %sbetween [",
407 stream
->events_discarded
,
408 end_stream
? "at end of stream " : "");
409 if (opt_clock_cycles
) {
410 ctf_print_timestamp(fp
, stream
,
411 stream
->prev_cycles_timestamp
);
412 fprintf(fp
, "] and [");
413 ctf_print_timestamp(fp
, stream
,
414 stream
->prev_cycles_timestamp_end
);
416 ctf_print_timestamp(fp
, stream
,
417 stream
->prev_real_timestamp
);
418 fprintf(fp
, "] and [");
419 ctf_print_timestamp(fp
, stream
,
420 stream
->prev_real_timestamp_end
);
422 fprintf(fp
, "] in trace UUID ");
423 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
424 if (stream
->stream_class
->trace
->path
[0])
425 fprintf(fp
, ", at path: \"%s\"",
426 stream
->stream_class
->trace
->path
);
428 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
430 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
432 fprintf(fp
, "You should consider recording a new trace with larger "
433 "buffers or with fewer events enabled.\n");
438 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
440 struct ctf_stream_pos
*pos
=
441 container_of(ppos
, struct ctf_stream_pos
, parent
);
442 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
443 struct ctf_event_definition
*event
;
447 /* We need to check for EOF here for empty files. */
448 if (unlikely(pos
->offset
== EOF
))
451 ctf_pos_get_event(pos
);
453 /* save the current position as a restore point */
454 pos
->last_offset
= pos
->offset
;
457 * This is the EOF check after we've advanced the position in
460 if (unlikely(pos
->offset
== EOF
))
462 assert(pos
->offset
< pos
->content_size
);
464 /* Read event header */
465 if (likely(stream
->stream_event_header
)) {
466 struct definition_integer
*integer_definition
;
467 struct bt_definition
*variant
;
469 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
472 /* lookup event id */
473 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
474 if (integer_definition
) {
475 id
= integer_definition
->value
._unsigned
;
477 struct definition_enum
*enum_definition
;
479 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
480 if (enum_definition
) {
481 id
= enum_definition
->integer
->value
._unsigned
;
485 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
487 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
488 if (integer_definition
) {
489 id
= integer_definition
->value
._unsigned
;
492 stream
->event_id
= id
;
494 /* lookup timestamp */
495 stream
->has_timestamp
= 0;
496 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
497 if (integer_definition
) {
498 ctf_update_timestamp(stream
, integer_definition
);
499 stream
->has_timestamp
= 1;
502 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
503 if (integer_definition
) {
504 ctf_update_timestamp(stream
, integer_definition
);
505 stream
->has_timestamp
= 1;
511 /* Read stream-declared event context */
512 if (stream
->stream_event_context
) {
513 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
518 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
519 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
522 event
= g_ptr_array_index(stream
->events_by_id
, id
);
523 if (unlikely(!event
)) {
524 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
528 /* Read event-declared event context */
529 if (event
->event_context
) {
530 ret
= generic_rw(ppos
, &event
->event_context
->p
);
535 /* Read event payload */
536 if (likely(event
->event_fields
)) {
537 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
545 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
550 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
552 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
553 struct ctf_event_definition
*event
;
557 id
= stream
->event_id
;
559 /* print event header */
560 if (likely(stream
->stream_event_header
)) {
561 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
566 /* print stream-declared event context */
567 if (stream
->stream_event_context
) {
568 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
573 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
574 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
577 event
= g_ptr_array_index(stream
->events_by_id
, id
);
578 if (unlikely(!event
)) {
579 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
583 /* print event-declared event context */
584 if (event
->event_context
) {
585 ret
= generic_rw(pos
, &event
->event_context
->p
);
590 /* Read and print event payload */
591 if (likely(event
->event_fields
)) {
592 ret
= generic_rw(pos
, &event
->event_fields
->p
);
600 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
604 int ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
608 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
609 sizeof(struct packet_index
));
610 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
611 sizeof(struct packet_index
));
613 pos
->packet_cycles_index
= NULL
;
614 pos
->packet_real_index
= NULL
;
616 switch (open_flags
& O_ACCMODE
) {
618 pos
->prot
= PROT_READ
;
619 pos
->flags
= MAP_PRIVATE
;
620 pos
->parent
.rw_table
= read_dispatch_table
;
621 pos
->parent
.event_cb
= ctf_read_event
;
624 pos
->prot
= PROT_WRITE
; /* Write has priority */
625 pos
->flags
= MAP_SHARED
;
626 pos
->parent
.rw_table
= write_dispatch_table
;
627 pos
->parent
.event_cb
= ctf_write_event
;
629 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
637 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
639 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
640 *pos
->content_size_loc
= pos
->offset
;
645 ret
= munmap_align(pos
->base_mma
);
647 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
652 if (pos
->packet_cycles_index
)
653 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
654 if (pos
->packet_real_index
)
655 (void) g_array_free(pos
->packet_real_index
, TRUE
);
660 * for SEEK_CUR: go to next packet.
661 * for SEEK_POS: go to packet numer (index).
663 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
665 struct ctf_stream_pos
*pos
=
666 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
667 struct ctf_file_stream
*file_stream
=
668 container_of(pos
, struct ctf_file_stream
, pos
);
671 struct packet_index
*packet_index
;
673 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
674 *pos
->content_size_loc
= pos
->offset
;
678 ret
= munmap_align(pos
->base_mma
);
680 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
684 pos
->base_mma
= NULL
;
688 * The caller should never ask for ctf_move_pos across packets,
689 * except to get exactly at the beginning of the next packet.
691 if (pos
->prot
== PROT_WRITE
) {
694 /* The writer will add padding */
695 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
698 assert(index
== 0); /* only seek supported for now */
704 pos
->content_size
= -1U; /* Unknown at this point */
705 pos
->packet_size
= WRITE_PACKET_LEN
;
706 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
707 pos
->packet_size
/ CHAR_BIT
);
715 uint64_t events_discarded_diff
;
717 if (pos
->offset
== EOF
) {
720 /* For printing discarded event count */
721 packet_index
= &g_array_index(pos
->packet_cycles_index
,
722 struct packet_index
, pos
->cur_index
);
723 file_stream
->parent
.prev_cycles_timestamp_end
=
724 packet_index
->timestamp_end
;
725 file_stream
->parent
.prev_cycles_timestamp
=
726 packet_index
->timestamp_begin
;
728 packet_index
= &g_array_index(pos
->packet_real_index
,
729 struct packet_index
, pos
->cur_index
);
730 file_stream
->parent
.prev_real_timestamp_end
=
731 packet_index
->timestamp_end
;
732 file_stream
->parent
.prev_real_timestamp
=
733 packet_index
->timestamp_begin
;
735 events_discarded_diff
= packet_index
->events_discarded
;
736 if (pos
->cur_index
> 0) {
737 packet_index
= &g_array_index(pos
->packet_real_index
,
740 events_discarded_diff
-= packet_index
->events_discarded
;
742 * Deal with 32-bit wrap-around if the
743 * tracer provided a 32-bit field.
745 if (packet_index
->events_discarded_len
== 32) {
746 events_discarded_diff
= (uint32_t) events_discarded_diff
;
749 file_stream
->parent
.events_discarded
= events_discarded_diff
;
750 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
751 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
752 /* The reader will expect us to skip padding */
757 packet_index
= &g_array_index(pos
->packet_cycles_index
,
758 struct packet_index
, index
);
759 pos
->last_events_discarded
= packet_index
->events_discarded
;
760 pos
->cur_index
= index
;
761 file_stream
->parent
.prev_real_timestamp
= 0;
762 file_stream
->parent
.prev_real_timestamp_end
= 0;
763 file_stream
->parent
.prev_cycles_timestamp
= 0;
764 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
769 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
771 * We need to check if we are in trace read or
772 * called from packet indexing. In this last
773 * case, the collection is not there, so we
774 * cannot print the timestamps.
776 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
778 * When a stream reaches the end of the
779 * file, we need to show the number of
780 * events discarded ourselves, because
781 * there is no next event scheduled to
782 * be printed in the output.
784 if (file_stream
->parent
.events_discarded
) {
786 ctf_print_discarded(stderr
,
787 &file_stream
->parent
,
789 file_stream
->parent
.events_discarded
= 0;
795 packet_index
= &g_array_index(pos
->packet_cycles_index
,
798 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
800 packet_index
= &g_array_index(pos
->packet_real_index
,
803 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
804 pos
->mmap_offset
= packet_index
->offset
;
806 /* Lookup context/packet size in index */
807 pos
->content_size
= packet_index
->content_size
;
808 pos
->packet_size
= packet_index
->packet_size
;
809 if (packet_index
->data_offset
< packet_index
->content_size
) {
810 pos
->offset
= 0; /* will read headers */
811 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
813 pos
->offset
= packet_index
->data_offset
;
815 goto read_next_packet
;
821 /* map new base. Need mapping length from header. */
822 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
823 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
824 if (pos
->base_mma
== MAP_FAILED
) {
825 fprintf(stderr
, "[error] mmap error %s.\n",
830 /* update trace_packet_header and stream_packet_context */
831 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
832 /* Read packet header */
833 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
836 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
837 /* Read packet context */
838 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
844 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
850 len
= fread(&magic
, sizeof(magic
), 1, fp
);
854 if (magic
== TSDL_MAGIC
) {
856 td
->byte_order
= BYTE_ORDER
;
857 CTF_TRACE_SET_FIELD(td
, byte_order
);
858 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
860 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
861 LITTLE_ENDIAN
: BIG_ENDIAN
;
862 CTF_TRACE_SET_FIELD(td
, byte_order
);
870 * Returns 0 on success, -1 on error.
873 int check_version(unsigned int major
, unsigned int minor
)
888 /* eventually return an error instead of warning */
890 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
896 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
899 struct metadata_packet_header header
;
900 size_t readlen
, writelen
, toread
;
901 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
904 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
908 if (td
->byte_order
!= BYTE_ORDER
) {
909 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
910 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
911 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
912 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
915 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
916 if (header
.compression_scheme
) {
917 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
918 header
.compression_scheme
);
921 if (header
.encryption_scheme
) {
922 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
923 header
.encryption_scheme
);
926 if (header
.checksum_scheme
) {
927 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
928 header
.checksum_scheme
);
931 if (check_version(header
.major
, header
.minor
) < 0)
933 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
934 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
935 CTF_TRACE_SET_FIELD(td
, uuid
);
937 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
941 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
944 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
947 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
952 if (babeltrace_debug
) {
954 fprintf(stderr
, "[debug] metadata packet read: %s\n",
958 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
959 if (writelen
< readlen
) {
969 ret
= 0; /* continue reading next packet */
976 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
977 ret
= fseek(in
, toread
, SEEK_CUR
);
979 fprintf(stderr
, "[warning] Missing padding at end of file\n");
986 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
995 * Using strlen on *buf instead of size of open_memstream
996 * because its size includes garbage at the end (after final
997 * \0). This is the allocated size, not the actual string size.
999 out
= babeltrace_open_memstream(buf
, &size
);
1001 perror("Metadata open_memstream");
1005 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
1014 /* close to flush the buffer */
1015 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1019 perror("babeltrace_flush_memstream");
1021 closeret
= fclose(in
);
1023 perror("Error in fclose");
1029 perror("Error in fclose");
1031 /* open for reading */
1032 buflen
= strlen(*buf
);
1037 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1039 perror("Metadata fmemopen");
1046 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1047 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1048 int whence
), FILE *metadata_fp
)
1050 struct ctf_scanner
*scanner
;
1051 struct ctf_file_stream
*metadata_stream
;
1054 int ret
= 0, closeret
;
1056 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1057 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1060 metadata_stream
->pos
.packet_seek
= packet_seek
;
1062 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1069 metadata_stream
->pos
.fd
= -1;
1071 td
->metadata
= &metadata_stream
->parent
;
1072 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1073 if (metadata_stream
->pos
.fd
< 0) {
1074 fprintf(stderr
, "Unable to open metadata.\n");
1079 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1081 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1082 perror("Metadata stream open");
1086 /* fd now belongs to fp */
1087 metadata_stream
->pos
.fd
= -1;
1089 if (babeltrace_debug
)
1092 if (packet_metadata(td
, fp
)) {
1093 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1095 /* Warn about empty metadata */
1096 fprintf(stderr
, "[warning] Empty metadata.\n");
1097 goto end_packet_read
;
1100 unsigned int major
, minor
;
1103 td
->byte_order
= BYTE_ORDER
;
1105 /* Check text-only metadata header and version */
1106 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1108 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1109 if (check_version(major
, minor
) < 0) {
1111 goto end_packet_read
;
1116 scanner
= ctf_scanner_alloc(fp
);
1118 fprintf(stderr
, "[error] Error allocating scanner\n");
1120 goto end_scanner_alloc
;
1122 ret
= ctf_scanner_append_ast(scanner
);
1124 fprintf(stderr
, "[error] Error creating AST\n");
1128 if (babeltrace_debug
) {
1129 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1131 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1136 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1138 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1141 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1142 td
, td
->byte_order
);
1144 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1148 ctf_scanner_free(scanner
);
1152 closeret
= fclose(fp
);
1154 perror("Error on fclose");
1159 if (metadata_stream
->pos
.fd
>= 0) {
1160 closeret
= close(metadata_stream
->pos
.fd
);
1162 perror("Error on metadata stream fd close");
1167 g_free(metadata_stream
);
1172 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1173 struct ctf_stream_definition
*stream
,
1174 struct ctf_event_declaration
*event
)
1176 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1178 if (event
->context_decl
) {
1179 struct bt_definition
*definition
=
1180 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1181 stream
->parent_def_scope
, 0, 0, "event.context");
1185 stream_event
->event_context
= container_of(definition
,
1186 struct definition_struct
, p
);
1187 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1189 if (event
->fields_decl
) {
1190 struct bt_definition
*definition
=
1191 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1192 stream
->parent_def_scope
, 0, 0, "event.fields");
1196 stream_event
->event_fields
= container_of(definition
,
1197 struct definition_struct
, p
);
1198 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1200 stream_event
->stream
= stream
;
1201 return stream_event
;
1204 if (stream_event
->event_fields
)
1205 bt_definition_unref(&stream_event
->event_fields
->p
);
1206 if (stream_event
->event_context
)
1207 bt_definition_unref(&stream_event
->event_context
->p
);
1212 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1214 struct ctf_stream_declaration
*stream_class
;
1218 if (stream
->stream_definitions_created
)
1221 stream_class
= stream
->stream_class
;
1223 if (stream_class
->packet_context_decl
) {
1224 struct bt_definition
*definition
=
1225 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1226 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1231 stream
->stream_packet_context
= container_of(definition
,
1232 struct definition_struct
, p
);
1233 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1235 if (stream_class
->event_header_decl
) {
1236 struct bt_definition
*definition
=
1237 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1238 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1243 stream
->stream_event_header
=
1244 container_of(definition
, struct definition_struct
, p
);
1245 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1247 if (stream_class
->event_context_decl
) {
1248 struct bt_definition
*definition
=
1249 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1250 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1255 stream
->stream_event_context
=
1256 container_of(definition
, struct definition_struct
, p
);
1257 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1259 stream
->events_by_id
= g_ptr_array_new();
1260 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1261 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1262 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1263 struct ctf_event_definition
*stream_event
;
1267 stream_event
= create_event_definitions(td
, stream
, event
);
1270 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1275 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1276 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1278 g_free(stream_event
);
1280 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1282 if (stream
->stream_event_context
)
1283 bt_definition_unref(&stream
->stream_event_context
->p
);
1284 if (stream
->stream_event_header
)
1285 bt_definition_unref(&stream
->stream_event_header
->p
);
1286 if (stream
->stream_packet_context
)
1287 bt_definition_unref(&stream
->stream_packet_context
->p
);
1293 int create_stream_packet_index(struct ctf_trace
*td
,
1294 struct ctf_file_stream
*file_stream
)
1296 struct ctf_stream_declaration
*stream
;
1298 struct ctf_stream_pos
*pos
;
1299 struct stat filestats
;
1300 struct packet_index packet_index
;
1301 int first_packet
= 1;
1304 pos
= &file_stream
->pos
;
1306 ret
= fstat(pos
->fd
, &filestats
);
1310 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1313 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1314 uint64_t stream_id
= 0;
1316 if (pos
->base_mma
) {
1317 /* unmap old base */
1318 ret
= munmap_align(pos
->base_mma
);
1320 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1324 pos
->base_mma
= NULL
;
1326 /* map new base. Need mapping length from header. */
1327 pos
->base_mma
= mmap_align(MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1328 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1329 assert(pos
->base_mma
!= MAP_FAILED
);
1330 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1331 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1332 pos
->offset
= 0; /* Position of the packet header */
1334 packet_index
.offset
= pos
->mmap_offset
;
1335 packet_index
.content_size
= 0;
1336 packet_index
.packet_size
= 0;
1337 packet_index
.timestamp_begin
= 0;
1338 packet_index
.timestamp_end
= 0;
1339 packet_index
.events_discarded
= 0;
1340 packet_index
.events_discarded_len
= 0;
1342 /* read and check header, set stream id (and check) */
1343 if (file_stream
->parent
.trace_packet_header
) {
1344 /* Read packet header */
1345 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1348 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1349 if (len_index
>= 0) {
1350 struct bt_definition
*field
;
1353 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1354 magic
= bt_get_unsigned_int(field
);
1355 if (magic
!= CTF_MAGIC
) {
1356 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1358 file_stream
->pos
.packet_cycles_index
->len
,
1359 (ssize_t
) pos
->mmap_offset
);
1365 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1366 if (len_index
>= 0) {
1367 struct definition_array
*defarray
;
1368 struct bt_definition
*field
;
1370 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1372 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1373 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1374 defarray
= container_of(field
, struct definition_array
, p
);
1375 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1377 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1378 struct bt_definition
*elem
;
1380 elem
= bt_array_index(defarray
, i
);
1381 uuidval
[i
] = bt_get_unsigned_int(elem
);
1383 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1385 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1391 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1392 if (len_index
>= 0) {
1393 struct bt_definition
*field
;
1395 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1396 stream_id
= bt_get_unsigned_int(field
);
1400 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1401 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1403 file_stream
->parent
.stream_id
);
1407 file_stream
->parent
.stream_id
= stream_id
;
1408 if (stream_id
>= td
->streams
->len
) {
1409 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1412 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1414 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1417 file_stream
->parent
.stream_class
= stream
;
1418 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1424 if (file_stream
->parent
.stream_packet_context
) {
1425 /* Read packet context */
1426 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1429 /* read content size from header */
1430 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1431 if (len_index
>= 0) {
1432 struct bt_definition
*field
;
1434 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1435 packet_index
.content_size
= bt_get_unsigned_int(field
);
1437 /* Use file size for packet size */
1438 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1441 /* read packet size from header */
1442 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1443 if (len_index
>= 0) {
1444 struct bt_definition
*field
;
1446 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1447 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1449 /* Use content size if non-zero, else file size */
1450 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1453 /* read timestamp begin from header */
1454 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1455 if (len_index
>= 0) {
1456 struct bt_definition
*field
;
1458 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1459 packet_index
.timestamp_begin
= bt_get_unsigned_int(field
);
1460 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1461 packet_index
.timestamp_begin
=
1462 ctf_get_real_timestamp(
1463 &file_stream
->parent
,
1464 packet_index
.timestamp_begin
);
1468 /* read timestamp end from header */
1469 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1470 if (len_index
>= 0) {
1471 struct bt_definition
*field
;
1473 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1474 packet_index
.timestamp_end
= bt_get_unsigned_int(field
);
1475 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1476 packet_index
.timestamp_end
=
1477 ctf_get_real_timestamp(
1478 &file_stream
->parent
,
1479 packet_index
.timestamp_end
);
1483 /* read events discarded from header */
1484 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1485 if (len_index
>= 0) {
1486 struct bt_definition
*field
;
1488 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1489 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1490 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1493 /* Use file size for packet size */
1494 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1495 /* Use content size if non-zero, else file size */
1496 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1499 /* Validate content size and packet size values */
1500 if (packet_index
.content_size
> packet_index
.packet_size
) {
1501 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1502 packet_index
.content_size
, packet_index
.packet_size
);
1506 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1507 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1508 packet_index
.packet_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1512 /* Save position after header and context */
1513 packet_index
.data_offset
= pos
->offset
;
1515 /* add index to packet array */
1516 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1518 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1525 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1529 if (td
->packet_header_decl
) {
1530 struct bt_definition
*definition
=
1531 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1532 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1537 stream
->trace_packet_header
=
1538 container_of(definition
, struct definition_struct
, p
);
1539 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1549 * Note: many file streams can inherit from the same stream class
1550 * description (metadata).
1553 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1554 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1557 int ret
, fd
, closeret
;
1558 struct ctf_file_stream
*file_stream
;
1559 struct stat statbuf
;
1561 fd
= openat(td
->dirfd
, path
, flags
);
1563 perror("File stream openat()");
1568 /* Don't try to mmap subdirectories. Skip them, return success. */
1569 ret
= fstat(fd
, &statbuf
);
1571 perror("File stream fstat()");
1574 if (S_ISDIR(statbuf
.st_mode
)) {
1575 fprintf(stderr
, "[warning] Skipping directory '%s' found in trace\n", path
);
1580 file_stream
= g_new0(struct ctf_file_stream
, 1);
1581 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1583 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
1584 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
1587 file_stream
->pos
.packet_seek
= packet_seek
;
1589 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1594 ret
= ctf_init_pos(&file_stream
->pos
, fd
, flags
);
1597 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1601 * For now, only a single clock per trace is supported.
1603 file_stream
->parent
.current_clock
= td
->single_clock
;
1604 ret
= create_stream_packet_index(td
, file_stream
);
1607 /* Add stream file to stream class */
1608 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1609 &file_stream
->parent
);
1613 if (file_stream
->parent
.trace_packet_header
)
1614 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1616 closeret
= ctf_fini_pos(&file_stream
->pos
);
1618 fprintf(stderr
, "Error on ctf_fini_pos\n");
1620 g_free(file_stream
);
1623 closeret
= close(fd
);
1625 perror("Error on fd close");
1632 int ctf_open_trace_read(struct ctf_trace
*td
,
1633 const char *path
, int flags
,
1634 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1635 int whence
), FILE *metadata_fp
)
1638 struct dirent
*dirent
;
1639 struct dirent
*diriter
;
1644 /* Open trace directory */
1645 td
->dir
= opendir(path
);
1647 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1652 td
->dirfd
= open(path
, 0);
1653 if (td
->dirfd
< 0) {
1654 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1655 perror("Trace directory open");
1659 strncpy(td
->path
, path
, sizeof(td
->path
));
1660 td
->path
[sizeof(td
->path
) - 1] = '\0';
1663 * Keep the metadata file separate.
1666 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1668 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1669 goto error_metadata
;
1673 * Open each stream: for each file, try to open, check magic
1674 * number, and get the stream ID to add to the right location in
1678 dirent_len
= offsetof(struct dirent
, d_name
) +
1679 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1681 dirent
= malloc(dirent_len
);
1684 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1686 fprintf(stderr
, "[error] Readdir error.\n");
1691 /* Ignore hidden files, ., .. and metadata. */
1692 if (!strncmp(diriter
->d_name
, ".", 1)
1693 || !strcmp(diriter
->d_name
, "..")
1694 || !strcmp(diriter
->d_name
, "metadata"))
1696 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1697 flags
, packet_seek
);
1699 fprintf(stderr
, "[error] Open file stream error.\n");
1710 closeret
= close(td
->dirfd
);
1712 perror("Error on fd close");
1715 closeret
= closedir(td
->dir
);
1717 perror("Error on closedir");
1724 * ctf_open_trace: Open a CTF trace and index it.
1725 * Note that the user must seek the trace after the open (using the iterator)
1726 * since the index creation read it entirely.
1729 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1730 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1731 int whence
), FILE *metadata_fp
)
1733 struct ctf_trace
*td
;
1737 * If packet_seek is NULL, we provide our default version.
1740 packet_seek
= ctf_packet_seek
;
1742 td
= g_new0(struct ctf_trace
, 1);
1744 switch (flags
& O_ACCMODE
) {
1747 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1750 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1755 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1758 fprintf(stderr
, "[error] Incorrect open flags.\n");
1769 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1770 struct bt_mmap_stream
*mmap_info
)
1772 pos
->mmap_offset
= 0;
1773 pos
->packet_size
= 0;
1774 pos
->content_size
= 0;
1775 pos
->content_size_loc
= NULL
;
1776 pos
->fd
= mmap_info
->fd
;
1777 pos
->base_mma
= NULL
;
1781 pos
->packet_cycles_index
= NULL
;
1782 pos
->packet_real_index
= NULL
;
1783 pos
->prot
= PROT_READ
;
1784 pos
->flags
= MAP_PRIVATE
;
1785 pos
->parent
.rw_table
= read_dispatch_table
;
1786 pos
->parent
.event_cb
= ctf_read_event
;
1790 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1791 struct ctf_file_stream
*file_stream
)
1793 struct ctf_stream_declaration
*stream
;
1794 uint64_t stream_id
= 0;
1797 file_stream
->parent
.stream_id
= stream_id
;
1798 if (stream_id
>= td
->streams
->len
) {
1799 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1800 "in metadata.\n", stream_id
);
1804 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1806 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1807 "in metadata.\n", stream_id
);
1811 file_stream
->parent
.stream_class
= stream
;
1812 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1818 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1819 struct bt_mmap_stream
*mmap_info
,
1820 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1824 struct ctf_file_stream
*file_stream
;
1826 file_stream
= g_new0(struct ctf_file_stream
, 1);
1827 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1828 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1830 file_stream
->pos
.packet_seek
= packet_seek
;
1832 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1837 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1842 * For now, only a single clock per trace is supported.
1844 file_stream
->parent
.current_clock
= td
->single_clock
;
1846 /* Add stream file to stream class */
1847 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1848 &file_stream
->parent
);
1852 if (file_stream
->parent
.trace_packet_header
)
1853 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1855 g_free(file_stream
);
1860 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1861 struct bt_mmap_stream_list
*mmap_list
,
1862 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1867 struct bt_mmap_stream
*mmap_info
;
1869 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1875 * for each stream, try to open, check magic number, and get the
1876 * stream ID to add to the right location in the stream array.
1878 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1879 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1881 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1893 struct bt_trace_descriptor
*ctf_open_mmap_trace(
1894 struct bt_mmap_stream_list
*mmap_list
,
1895 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1899 struct ctf_trace
*td
;
1903 fprintf(stderr
, "[error] No metadata file pointer associated, "
1904 "required for mmap parsing\n");
1908 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1911 td
= g_new0(struct ctf_trace
, 1);
1912 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1925 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
1928 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1930 /* for each stream_class */
1931 for (i
= 0; i
< td
->streams
->len
; i
++) {
1932 struct ctf_stream_declaration
*stream_class
;
1934 stream_class
= g_ptr_array_index(td
->streams
, i
);
1937 /* for each file_stream */
1938 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
1939 struct ctf_stream_definition
*stream
;
1940 struct ctf_stream_pos
*stream_pos
;
1941 struct ctf_file_stream
*cfs
;
1943 stream
= g_ptr_array_index(stream_class
->streams
, j
);
1946 cfs
= container_of(stream
, struct ctf_file_stream
,
1948 stream_pos
= &cfs
->pos
;
1949 if (!stream_pos
->packet_cycles_index
)
1952 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
1953 struct packet_index
*index
;
1954 struct packet_index new_index
;
1956 index
= &g_array_index(stream_pos
->packet_cycles_index
,
1957 struct packet_index
, k
);
1958 memcpy(&new_index
, index
,
1959 sizeof(struct packet_index
));
1960 new_index
.timestamp_begin
=
1961 ctf_get_real_timestamp(stream
,
1962 index
->timestamp_begin
);
1963 new_index
.timestamp_end
=
1964 ctf_get_real_timestamp(stream
,
1965 index
->timestamp_end
);
1966 g_array_append_val(stream_pos
->packet_real_index
,
1975 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1979 ret
= ctf_fini_pos(&file_stream
->pos
);
1981 fprintf(stderr
, "Error on ctf_fini_pos\n");
1984 ret
= close(file_stream
->pos
.fd
);
1986 perror("Error closing file fd");
1993 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
1995 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2001 for (i
= 0; i
< td
->streams
->len
; i
++) {
2002 struct ctf_stream_declaration
*stream
;
2005 stream
= g_ptr_array_index(td
->streams
, i
);
2008 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2009 struct ctf_file_stream
*file_stream
;
2010 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2011 struct ctf_file_stream
, parent
);
2012 ret
= ctf_close_file_stream(file_stream
);
2018 ctf_destroy_metadata(td
);
2019 ret
= close(td
->dirfd
);
2021 perror("Error closing dirfd");
2024 ret
= closedir(td
->dir
);
2026 perror("Error closedir");
2034 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2035 struct bt_context
*ctx
)
2037 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2044 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2045 struct bt_trace_handle
*handle
)
2047 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2050 td
->handle
= handle
;
2054 void __attribute__((constructor
)) ctf_init(void)
2058 ctf_format
.name
= g_quark_from_static_string("ctf");
2059 ret
= bt_register_format(&ctf_format
);
2064 void __attribute__((destructor
)) ctf_exit(void)
2066 bt_unregister_format(&ctf_format
);