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 trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
80 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
84 struct trace_descriptor
*ctf_open_mmap_trace(
85 struct mmap_stream_list
*mmap_list
,
86 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
90 void ctf_set_context(struct trace_descriptor
*descriptor
,
91 struct bt_context
*ctx
);
93 void ctf_set_handle(struct trace_descriptor
*descriptor
,
94 struct bt_trace_handle
*handle
);
97 int ctf_close_trace(struct trace_descriptor
*descriptor
);
99 uint64_t ctf_timestamp_begin(struct trace_descriptor
*descriptor
,
100 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
102 uint64_t ctf_timestamp_end(struct trace_descriptor
*descriptor
,
103 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
105 int ctf_convert_index_timestamp(struct 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 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 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 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).
310 void ctf_print_timestamp_real(FILE *fp
,
311 struct ctf_stream_definition
*stream
,
314 uint64_t ts_sec
= 0, ts_nsec
;
318 /* Add command-line offset */
319 ts_sec
+= opt_clock_offset
;
321 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
322 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
324 if (!opt_clock_seconds
) {
326 time_t time_s
= (time_t) ts_sec
;
328 if (!opt_clock_gmt
) {
331 res
= localtime_r(&time_s
, &tm
);
333 fprintf(stderr
, "[warning] Unable to get localtime.\n");
339 res
= gmtime_r(&time_s
, &tm
);
341 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
345 if (opt_clock_date
) {
349 /* Print date and time */
350 res
= strftime(timestr
, sizeof(timestr
),
353 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
356 fprintf(fp
, "%s", timestr
);
358 /* Print time in HH:MM:SS.ns */
359 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
360 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
364 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
372 * Print timestamp, in cycles
374 void ctf_print_timestamp_cycles(FILE *fp
,
375 struct ctf_stream_definition
*stream
,
378 fprintf(fp
, "%020" PRIu64
, timestamp
);
381 void ctf_print_timestamp(FILE *fp
,
382 struct ctf_stream_definition
*stream
,
385 if (opt_clock_cycles
) {
386 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
388 ctf_print_timestamp_real(fp
, stream
, timestamp
);
393 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
395 struct ctf_stream_pos
*pos
=
396 container_of(ppos
, struct ctf_stream_pos
, parent
);
397 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
398 struct ctf_event_definition
*event
;
402 /* We need to check for EOF here for empty files. */
403 if (unlikely(pos
->offset
== EOF
))
406 ctf_pos_get_event(pos
);
408 /* save the current position as a restore point */
409 pos
->last_offset
= pos
->offset
;
412 * This is the EOF check after we've advanced the position in
415 if (unlikely(pos
->offset
== EOF
))
417 assert(pos
->offset
< pos
->content_size
);
419 /* Read event header */
420 if (likely(stream
->stream_event_header
)) {
421 struct definition_integer
*integer_definition
;
422 struct definition
*variant
;
424 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
427 /* lookup event id */
428 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
429 if (integer_definition
) {
430 id
= integer_definition
->value
._unsigned
;
432 struct definition_enum
*enum_definition
;
434 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
435 if (enum_definition
) {
436 id
= enum_definition
->integer
->value
._unsigned
;
440 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
442 integer_definition
= lookup_integer(variant
, "id", FALSE
);
443 if (integer_definition
) {
444 id
= integer_definition
->value
._unsigned
;
447 stream
->event_id
= id
;
449 /* lookup timestamp */
450 stream
->has_timestamp
= 0;
451 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
452 if (integer_definition
) {
453 ctf_update_timestamp(stream
, integer_definition
);
454 stream
->has_timestamp
= 1;
457 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
458 if (integer_definition
) {
459 ctf_update_timestamp(stream
, integer_definition
);
460 stream
->has_timestamp
= 1;
466 /* Read stream-declared event context */
467 if (stream
->stream_event_context
) {
468 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
473 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
474 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
477 event
= g_ptr_array_index(stream
->events_by_id
, id
);
478 if (unlikely(!event
)) {
479 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
483 /* Read event-declared event context */
484 if (event
->event_context
) {
485 ret
= generic_rw(ppos
, &event
->event_context
->p
);
490 /* Read event payload */
491 if (likely(event
->event_fields
)) {
492 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
500 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
505 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream_definition
*stream
)
507 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
508 struct ctf_event_definition
*event
;
512 id
= stream
->event_id
;
514 /* print event header */
515 if (likely(stream
->stream_event_header
)) {
516 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
521 /* print stream-declared event context */
522 if (stream
->stream_event_context
) {
523 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
528 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
529 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
532 event
= g_ptr_array_index(stream
->events_by_id
, id
);
533 if (unlikely(!event
)) {
534 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
538 /* print event-declared event context */
539 if (event
->event_context
) {
540 ret
= generic_rw(pos
, &event
->event_context
->p
);
545 /* Read and print event payload */
546 if (likely(event
->event_fields
)) {
547 ret
= generic_rw(pos
, &event
->event_fields
->p
);
555 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
559 int ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
563 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
564 sizeof(struct packet_index
));
565 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
566 sizeof(struct packet_index
));
568 pos
->packet_cycles_index
= NULL
;
569 pos
->packet_real_index
= NULL
;
571 switch (open_flags
& O_ACCMODE
) {
573 pos
->prot
= PROT_READ
;
574 pos
->flags
= MAP_PRIVATE
;
575 pos
->parent
.rw_table
= read_dispatch_table
;
576 pos
->parent
.event_cb
= ctf_read_event
;
579 pos
->prot
= PROT_WRITE
; /* Write has priority */
580 pos
->flags
= MAP_SHARED
;
581 pos
->parent
.rw_table
= write_dispatch_table
;
582 pos
->parent
.event_cb
= ctf_write_event
;
584 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
592 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
594 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
595 *pos
->content_size_loc
= pos
->offset
;
600 ret
= munmap_align(pos
->base_mma
);
602 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
607 if (pos
->packet_cycles_index
)
608 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
609 if (pos
->packet_real_index
)
610 (void) g_array_free(pos
->packet_real_index
, TRUE
);
615 * for SEEK_CUR: go to next packet.
616 * for SEEK_POS: go to packet numer (index).
618 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
620 struct ctf_stream_pos
*pos
=
621 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
622 struct ctf_file_stream
*file_stream
=
623 container_of(pos
, struct ctf_file_stream
, pos
);
626 struct packet_index
*packet_index
;
628 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
629 *pos
->content_size_loc
= pos
->offset
;
633 ret
= munmap_align(pos
->base_mma
);
635 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
639 pos
->base_mma
= NULL
;
643 * The caller should never ask for ctf_move_pos across packets,
644 * except to get exactly at the beginning of the next packet.
646 if (pos
->prot
== PROT_WRITE
) {
649 /* The writer will add padding */
650 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
653 assert(index
== 0); /* only seek supported for now */
659 pos
->content_size
= -1U; /* Unknown at this point */
660 pos
->packet_size
= WRITE_PACKET_LEN
;
661 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
662 pos
->packet_size
/ CHAR_BIT
);
670 uint64_t events_discarded_diff
;
672 if (pos
->offset
== EOF
) {
675 /* For printing discarded event count */
676 packet_index
= &g_array_index(pos
->packet_cycles_index
,
677 struct packet_index
, pos
->cur_index
);
678 file_stream
->parent
.prev_cycles_timestamp_end
=
679 packet_index
->timestamp_end
;
680 file_stream
->parent
.prev_cycles_timestamp
=
681 packet_index
->timestamp_begin
;
683 packet_index
= &g_array_index(pos
->packet_real_index
,
684 struct packet_index
, pos
->cur_index
);
685 file_stream
->parent
.prev_real_timestamp_end
=
686 packet_index
->timestamp_end
;
687 file_stream
->parent
.prev_real_timestamp
=
688 packet_index
->timestamp_begin
;
690 events_discarded_diff
= packet_index
->events_discarded
;
691 if (pos
->cur_index
> 0) {
692 packet_index
= &g_array_index(pos
->packet_real_index
,
695 events_discarded_diff
-= packet_index
->events_discarded
;
697 * Deal with 32-bit wrap-around if the
698 * tracer provided a 32-bit field.
700 if (packet_index
->events_discarded_len
== 32) {
701 events_discarded_diff
= (uint32_t) events_discarded_diff
;
704 file_stream
->parent
.events_discarded
= events_discarded_diff
;
705 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
706 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
707 /* The reader will expect us to skip padding */
712 packet_index
= &g_array_index(pos
->packet_cycles_index
,
713 struct packet_index
, index
);
714 pos
->last_events_discarded
= packet_index
->events_discarded
;
715 pos
->cur_index
= index
;
716 file_stream
->parent
.prev_real_timestamp
= 0;
717 file_stream
->parent
.prev_real_timestamp_end
= 0;
718 file_stream
->parent
.prev_cycles_timestamp
= 0;
719 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
724 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
726 * When a stream reaches the end of the
727 * file, we need to show the number of
728 * events discarded ourselves, because
729 * there is no next event scheduled to
730 * be printed in the output.
732 if (file_stream
->parent
.events_discarded
) {
734 * We need to check if we are in trace
735 * read or called from packet indexing.
736 * In this last case, the collection is
737 * not there, so we cannot print the
740 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
742 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events at end of stream between [",
743 file_stream
->parent
.events_discarded
);
744 if (opt_clock_cycles
) {
745 ctf_print_timestamp(stderr
,
746 &file_stream
->parent
,
747 file_stream
->parent
.prev_cycles_timestamp
);
748 fprintf(stderr
, "] and [");
749 ctf_print_timestamp(stderr
, &file_stream
->parent
,
750 file_stream
->parent
.prev_cycles_timestamp_end
);
752 ctf_print_timestamp(stderr
,
753 &file_stream
->parent
,
754 file_stream
->parent
.prev_real_timestamp
);
755 fprintf(stderr
, "] and [");
756 ctf_print_timestamp(stderr
, &file_stream
->parent
,
757 file_stream
->parent
.prev_real_timestamp_end
);
759 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
762 file_stream
->parent
.events_discarded
= 0;
767 packet_index
= &g_array_index(pos
->packet_cycles_index
,
770 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
772 packet_index
= &g_array_index(pos
->packet_real_index
,
775 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
776 pos
->mmap_offset
= packet_index
->offset
;
778 /* Lookup context/packet size in index */
779 pos
->content_size
= packet_index
->content_size
;
780 pos
->packet_size
= packet_index
->packet_size
;
781 if (packet_index
->data_offset
< packet_index
->content_size
) {
782 pos
->offset
= 0; /* will read headers */
783 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
785 pos
->offset
= packet_index
->data_offset
;
787 goto read_next_packet
;
793 /* map new base. Need mapping length from header. */
794 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
795 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
796 if (pos
->base_mma
== MAP_FAILED
) {
797 fprintf(stderr
, "[error] mmap error %s.\n",
802 /* update trace_packet_header and stream_packet_context */
803 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
804 /* Read packet header */
805 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
808 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
809 /* Read packet context */
810 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
816 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
822 len
= fread(&magic
, sizeof(magic
), 1, fp
);
826 if (magic
== TSDL_MAGIC
) {
828 td
->byte_order
= BYTE_ORDER
;
829 CTF_TRACE_SET_FIELD(td
, byte_order
);
830 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
832 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
833 LITTLE_ENDIAN
: BIG_ENDIAN
;
834 CTF_TRACE_SET_FIELD(td
, byte_order
);
842 * Returns 0 on success, -1 on error.
845 int check_version(unsigned int major
, unsigned int minor
)
860 /* eventually return an error instead of warning */
862 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
868 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
871 struct metadata_packet_header header
;
872 size_t readlen
, writelen
, toread
;
873 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
876 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
880 if (td
->byte_order
!= BYTE_ORDER
) {
881 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
882 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
883 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
884 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
887 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
888 if (header
.compression_scheme
) {
889 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
890 header
.compression_scheme
);
893 if (header
.encryption_scheme
) {
894 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
895 header
.encryption_scheme
);
898 if (header
.checksum_scheme
) {
899 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
900 header
.checksum_scheme
);
903 if (check_version(header
.major
, header
.minor
) < 0)
905 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
906 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
907 CTF_TRACE_SET_FIELD(td
, uuid
);
909 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
913 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
916 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
919 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
924 if (babeltrace_debug
) {
926 fprintf(stderr
, "[debug] metadata packet read: %s\n",
930 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
931 if (writelen
< readlen
) {
941 ret
= 0; /* continue reading next packet */
948 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
949 ret
= fseek(in
, toread
, SEEK_CUR
);
951 fprintf(stderr
, "[warning] Missing padding at end of file\n");
958 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
967 * Using strlen on *buf instead of size of open_memstream
968 * because its size includes garbage at the end (after final
969 * \0). This is the allocated size, not the actual string size.
971 out
= babeltrace_open_memstream(buf
, &size
);
973 perror("Metadata open_memstream");
977 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
986 /* close to flush the buffer */
987 ret
= babeltrace_close_memstream(buf
, &size
, out
);
991 perror("babeltrace_flush_memstream");
993 closeret
= fclose(in
);
995 perror("Error in fclose");
1001 perror("Error in fclose");
1003 /* open for reading */
1004 *fp
= babeltrace_fmemopen(*buf
, strlen(*buf
), "rb");
1006 perror("Metadata fmemopen");
1013 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1014 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1015 int whence
), FILE *metadata_fp
)
1017 struct ctf_scanner
*scanner
;
1018 struct ctf_file_stream
*metadata_stream
;
1021 int ret
= 0, closeret
;
1023 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1024 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1027 metadata_stream
->pos
.packet_seek
= packet_seek
;
1029 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1036 metadata_stream
->pos
.fd
= -1;
1038 td
->metadata
= &metadata_stream
->parent
;
1039 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1040 if (metadata_stream
->pos
.fd
< 0) {
1041 fprintf(stderr
, "Unable to open metadata.\n");
1046 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1048 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1049 perror("Metadata stream open");
1053 /* fd now belongs to fp */
1054 metadata_stream
->pos
.fd
= -1;
1056 if (babeltrace_debug
)
1059 if (packet_metadata(td
, fp
)) {
1060 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1062 goto end_packet_read
;
1064 unsigned int major
, minor
;
1067 td
->byte_order
= BYTE_ORDER
;
1069 /* Check text-only metadata header and version */
1070 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1072 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1073 if (check_version(major
, minor
) < 0) {
1075 goto end_packet_read
;
1080 scanner
= ctf_scanner_alloc(fp
);
1082 fprintf(stderr
, "[error] Error allocating scanner\n");
1084 goto end_scanner_alloc
;
1086 ret
= ctf_scanner_append_ast(scanner
);
1088 fprintf(stderr
, "[error] Error creating AST\n");
1092 if (babeltrace_debug
) {
1093 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1095 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1100 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1102 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1105 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1106 td
, td
->byte_order
);
1108 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1112 ctf_scanner_free(scanner
);
1116 closeret
= fclose(fp
);
1118 perror("Error on fclose");
1123 if (metadata_stream
->pos
.fd
>= 0) {
1124 closeret
= close(metadata_stream
->pos
.fd
);
1126 perror("Error on metadata stream fd close");
1131 g_free(metadata_stream
);
1136 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1137 struct ctf_stream_definition
*stream
,
1138 struct ctf_event_declaration
*event
)
1140 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1142 if (event
->context_decl
) {
1143 struct definition
*definition
=
1144 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1145 stream
->parent_def_scope
, 0, 0, "event.context");
1149 stream_event
->event_context
= container_of(definition
,
1150 struct definition_struct
, p
);
1151 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1153 if (event
->fields_decl
) {
1154 struct definition
*definition
=
1155 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1156 stream
->parent_def_scope
, 0, 0, "event.fields");
1160 stream_event
->event_fields
= container_of(definition
,
1161 struct definition_struct
, p
);
1162 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1164 stream_event
->stream
= stream
;
1165 return stream_event
;
1168 if (stream_event
->event_fields
)
1169 definition_unref(&stream_event
->event_fields
->p
);
1170 if (stream_event
->event_context
)
1171 definition_unref(&stream_event
->event_context
->p
);
1176 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1178 struct ctf_stream_declaration
*stream_class
;
1182 if (stream
->stream_definitions_created
)
1185 stream_class
= stream
->stream_class
;
1187 if (stream_class
->packet_context_decl
) {
1188 struct definition
*definition
=
1189 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1190 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1195 stream
->stream_packet_context
= container_of(definition
,
1196 struct definition_struct
, p
);
1197 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1199 if (stream_class
->event_header_decl
) {
1200 struct definition
*definition
=
1201 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1202 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1207 stream
->stream_event_header
=
1208 container_of(definition
, struct definition_struct
, p
);
1209 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1211 if (stream_class
->event_context_decl
) {
1212 struct definition
*definition
=
1213 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1214 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1219 stream
->stream_event_context
=
1220 container_of(definition
, struct definition_struct
, p
);
1221 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1223 stream
->events_by_id
= g_ptr_array_new();
1224 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1225 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1226 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1227 struct ctf_event_definition
*stream_event
;
1231 stream_event
= create_event_definitions(td
, stream
, event
);
1234 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1239 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1240 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1242 g_free(stream_event
);
1244 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1246 if (stream
->stream_event_context
)
1247 definition_unref(&stream
->stream_event_context
->p
);
1248 if (stream
->stream_event_header
)
1249 definition_unref(&stream
->stream_event_header
->p
);
1250 if (stream
->stream_packet_context
)
1251 definition_unref(&stream
->stream_packet_context
->p
);
1257 int create_stream_packet_index(struct ctf_trace
*td
,
1258 struct ctf_file_stream
*file_stream
)
1260 struct ctf_stream_declaration
*stream
;
1262 struct ctf_stream_pos
*pos
;
1263 struct stat filestats
;
1264 struct packet_index packet_index
;
1265 int first_packet
= 1;
1268 pos
= &file_stream
->pos
;
1270 ret
= fstat(pos
->fd
, &filestats
);
1274 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1277 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1278 uint64_t stream_id
= 0;
1280 if (pos
->base_mma
) {
1281 /* unmap old base */
1282 ret
= munmap_align(pos
->base_mma
);
1284 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1288 pos
->base_mma
= NULL
;
1290 /* map new base. Need mapping length from header. */
1291 pos
->base_mma
= mmap_align(MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1292 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1293 assert(pos
->base_mma
!= MAP_FAILED
);
1294 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1295 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1296 pos
->offset
= 0; /* Position of the packet header */
1298 packet_index
.offset
= pos
->mmap_offset
;
1299 packet_index
.content_size
= 0;
1300 packet_index
.packet_size
= 0;
1301 packet_index
.timestamp_begin
= 0;
1302 packet_index
.timestamp_end
= 0;
1303 packet_index
.events_discarded
= 0;
1304 packet_index
.events_discarded_len
= 0;
1306 /* read and check header, set stream id (and check) */
1307 if (file_stream
->parent
.trace_packet_header
) {
1308 /* Read packet header */
1309 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1312 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1313 if (len_index
>= 0) {
1314 struct definition
*field
;
1317 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1318 magic
= get_unsigned_int(field
);
1319 if (magic
!= CTF_MAGIC
) {
1320 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1322 file_stream
->pos
.packet_cycles_index
->len
,
1323 (ssize_t
) pos
->mmap_offset
);
1329 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1330 if (len_index
>= 0) {
1331 struct definition_array
*defarray
;
1332 struct definition
*field
;
1334 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1336 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1337 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1338 defarray
= container_of(field
, struct definition_array
, p
);
1339 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1341 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1342 struct definition
*elem
;
1344 elem
= array_index(defarray
, i
);
1345 uuidval
[i
] = get_unsigned_int(elem
);
1347 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1349 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1355 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1356 if (len_index
>= 0) {
1357 struct definition
*field
;
1359 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1360 stream_id
= get_unsigned_int(field
);
1364 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1365 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1369 file_stream
->parent
.stream_id
= stream_id
;
1370 if (stream_id
>= td
->streams
->len
) {
1371 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1374 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1376 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1379 file_stream
->parent
.stream_class
= stream
;
1380 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1386 if (file_stream
->parent
.stream_packet_context
) {
1387 /* Read packet context */
1388 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1391 /* read content size from header */
1392 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1393 if (len_index
>= 0) {
1394 struct definition
*field
;
1396 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1397 packet_index
.content_size
= get_unsigned_int(field
);
1399 /* Use file size for packet size */
1400 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1403 /* read packet size from header */
1404 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1405 if (len_index
>= 0) {
1406 struct definition
*field
;
1408 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1409 packet_index
.packet_size
= get_unsigned_int(field
);
1411 /* Use content size if non-zero, else file size */
1412 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1415 /* read timestamp begin from header */
1416 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1417 if (len_index
>= 0) {
1418 struct definition
*field
;
1420 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1421 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1422 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1423 packet_index
.timestamp_begin
=
1424 ctf_get_real_timestamp(
1425 &file_stream
->parent
,
1426 packet_index
.timestamp_begin
);
1430 /* read timestamp end from header */
1431 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1432 if (len_index
>= 0) {
1433 struct definition
*field
;
1435 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1436 packet_index
.timestamp_end
= get_unsigned_int(field
);
1437 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1438 packet_index
.timestamp_end
=
1439 ctf_get_real_timestamp(
1440 &file_stream
->parent
,
1441 packet_index
.timestamp_end
);
1445 /* read events discarded from header */
1446 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1447 if (len_index
>= 0) {
1448 struct definition
*field
;
1450 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1451 packet_index
.events_discarded
= get_unsigned_int(field
);
1452 packet_index
.events_discarded_len
= get_int_len(field
);
1455 /* Use file size for packet size */
1456 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1457 /* Use content size if non-zero, else file size */
1458 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1461 /* Validate content size and packet size values */
1462 if (packet_index
.content_size
> packet_index
.packet_size
) {
1463 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1464 packet_index
.content_size
, packet_index
.packet_size
);
1468 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1469 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1470 packet_index
.packet_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1474 /* Save position after header and context */
1475 packet_index
.data_offset
= pos
->offset
;
1477 /* add index to packet array */
1478 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1480 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1487 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1491 if (td
->packet_header_decl
) {
1492 struct definition
*definition
=
1493 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1494 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1499 stream
->trace_packet_header
=
1500 container_of(definition
, struct definition_struct
, p
);
1501 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1511 * Note: many file streams can inherit from the same stream class
1512 * description (metadata).
1515 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1516 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1519 int ret
, fd
, closeret
;
1520 struct ctf_file_stream
*file_stream
;
1521 struct stat statbuf
;
1523 fd
= openat(td
->dirfd
, path
, flags
);
1525 perror("File stream openat()");
1530 /* Don't try to mmap subdirectories. Skip them, return success. */
1531 ret
= fstat(fd
, &statbuf
);
1533 perror("File stream fstat()");
1536 if (S_ISDIR(statbuf
.st_mode
)) {
1537 fprintf(stderr
, "[warning] Skipping directory '%s' found in trace\n", path
);
1542 file_stream
= g_new0(struct ctf_file_stream
, 1);
1543 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1546 file_stream
->pos
.packet_seek
= packet_seek
;
1548 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1553 ret
= ctf_init_pos(&file_stream
->pos
, fd
, flags
);
1556 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1560 * For now, only a single clock per trace is supported.
1562 file_stream
->parent
.current_clock
= td
->single_clock
;
1563 ret
= create_stream_packet_index(td
, file_stream
);
1566 /* Add stream file to stream class */
1567 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1568 &file_stream
->parent
);
1572 if (file_stream
->parent
.trace_packet_header
)
1573 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1575 closeret
= ctf_fini_pos(&file_stream
->pos
);
1577 fprintf(stderr
, "Error on ctf_fini_pos\n");
1579 g_free(file_stream
);
1582 closeret
= close(fd
);
1584 perror("Error on fd close");
1591 int ctf_open_trace_read(struct ctf_trace
*td
,
1592 const char *path
, int flags
,
1593 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1594 int whence
), FILE *metadata_fp
)
1597 struct dirent
*dirent
;
1598 struct dirent
*diriter
;
1603 /* Open trace directory */
1604 td
->dir
= opendir(path
);
1606 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1611 td
->dirfd
= open(path
, 0);
1612 if (td
->dirfd
< 0) {
1613 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1614 perror("Trace directory open");
1618 strncpy(td
->path
, path
, sizeof(td
->path
));
1619 td
->path
[sizeof(td
->path
) - 1] = '\0';
1622 * Keep the metadata file separate.
1625 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1627 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1628 goto error_metadata
;
1632 * Open each stream: for each file, try to open, check magic
1633 * number, and get the stream ID to add to the right location in
1637 dirent_len
= offsetof(struct dirent
, d_name
) +
1638 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1640 dirent
= malloc(dirent_len
);
1643 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1645 fprintf(stderr
, "[error] Readdir error.\n");
1650 /* Ignore hidden files, ., .. and metadata. */
1651 if (!strncmp(diriter
->d_name
, ".", 1)
1652 || !strcmp(diriter
->d_name
, "..")
1653 || !strcmp(diriter
->d_name
, "metadata"))
1655 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1656 flags
, packet_seek
);
1658 fprintf(stderr
, "[error] Open file stream error.\n");
1669 closeret
= close(td
->dirfd
);
1671 perror("Error on fd close");
1674 closeret
= closedir(td
->dir
);
1676 perror("Error on closedir");
1683 * ctf_open_trace: Open a CTF trace and index it.
1684 * Note that the user must seek the trace after the open (using the iterator)
1685 * since the index creation read it entirely.
1688 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1689 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1690 int whence
), FILE *metadata_fp
)
1692 struct ctf_trace
*td
;
1696 * If packet_seek is NULL, we provide our default version.
1699 packet_seek
= ctf_packet_seek
;
1701 td
= g_new0(struct ctf_trace
, 1);
1703 switch (flags
& O_ACCMODE
) {
1706 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1709 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1714 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1717 fprintf(stderr
, "[error] Incorrect open flags.\n");
1728 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1729 struct mmap_stream
*mmap_info
)
1731 pos
->mmap_offset
= 0;
1732 pos
->packet_size
= 0;
1733 pos
->content_size
= 0;
1734 pos
->content_size_loc
= NULL
;
1735 pos
->fd
= mmap_info
->fd
;
1736 pos
->base_mma
= NULL
;
1740 pos
->packet_cycles_index
= NULL
;
1741 pos
->packet_real_index
= NULL
;
1742 pos
->prot
= PROT_READ
;
1743 pos
->flags
= MAP_PRIVATE
;
1744 pos
->parent
.rw_table
= read_dispatch_table
;
1745 pos
->parent
.event_cb
= ctf_read_event
;
1749 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1750 struct ctf_file_stream
*file_stream
)
1752 struct ctf_stream_declaration
*stream
;
1753 uint64_t stream_id
= 0;
1756 file_stream
->parent
.stream_id
= stream_id
;
1757 if (stream_id
>= td
->streams
->len
) {
1758 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1759 "in metadata.\n", stream_id
);
1763 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1765 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1766 "in metadata.\n", stream_id
);
1770 file_stream
->parent
.stream_class
= stream
;
1771 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1777 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1778 struct mmap_stream
*mmap_info
,
1779 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1783 struct ctf_file_stream
*file_stream
;
1785 file_stream
= g_new0(struct ctf_file_stream
, 1);
1786 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1787 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1789 file_stream
->pos
.packet_seek
= packet_seek
;
1791 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1796 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1801 * For now, only a single clock per trace is supported.
1803 file_stream
->parent
.current_clock
= td
->single_clock
;
1805 /* Add stream file to stream class */
1806 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1807 &file_stream
->parent
);
1811 if (file_stream
->parent
.trace_packet_header
)
1812 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1814 g_free(file_stream
);
1818 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1819 struct mmap_stream_list
*mmap_list
,
1820 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1825 struct mmap_stream
*mmap_info
;
1827 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1833 * for each stream, try to open, check magic number, and get the
1834 * stream ID to add to the right location in the stream array.
1836 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1837 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1839 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1851 struct trace_descriptor
*ctf_open_mmap_trace(
1852 struct mmap_stream_list
*mmap_list
,
1853 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1857 struct ctf_trace
*td
;
1861 fprintf(stderr
, "[error] No metadata file pointer associated, "
1862 "required for mmap parsing\n");
1866 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1869 td
= g_new0(struct ctf_trace
, 1);
1870 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1883 int ctf_convert_index_timestamp(struct trace_descriptor
*tdp
)
1886 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1888 /* for each stream_class */
1889 for (i
= 0; i
< td
->streams
->len
; i
++) {
1890 struct ctf_stream_declaration
*stream_class
;
1892 stream_class
= g_ptr_array_index(td
->streams
, i
);
1895 /* for each file_stream */
1896 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
1897 struct ctf_stream_definition
*stream
;
1898 struct ctf_stream_pos
*stream_pos
;
1899 struct ctf_file_stream
*cfs
;
1901 stream
= g_ptr_array_index(stream_class
->streams
, j
);
1904 cfs
= container_of(stream
, struct ctf_file_stream
,
1906 stream_pos
= &cfs
->pos
;
1907 if (!stream_pos
->packet_cycles_index
)
1910 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
1911 struct packet_index
*index
;
1912 struct packet_index new_index
;
1914 index
= &g_array_index(stream_pos
->packet_cycles_index
,
1915 struct packet_index
, k
);
1916 memcpy(&new_index
, index
,
1917 sizeof(struct packet_index
));
1918 new_index
.timestamp_begin
=
1919 ctf_get_real_timestamp(stream
,
1920 index
->timestamp_begin
);
1921 new_index
.timestamp_end
=
1922 ctf_get_real_timestamp(stream
,
1923 index
->timestamp_end
);
1924 g_array_append_val(stream_pos
->packet_real_index
,
1933 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1937 ret
= ctf_fini_pos(&file_stream
->pos
);
1939 fprintf(stderr
, "Error on ctf_fini_pos\n");
1942 ret
= close(file_stream
->pos
.fd
);
1944 perror("Error closing file fd");
1951 int ctf_close_trace(struct trace_descriptor
*tdp
)
1953 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1959 for (i
= 0; i
< td
->streams
->len
; i
++) {
1960 struct ctf_stream_declaration
*stream
;
1963 stream
= g_ptr_array_index(td
->streams
, i
);
1966 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1967 struct ctf_file_stream
*file_stream
;
1968 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
1969 struct ctf_file_stream
, parent
);
1970 ret
= ctf_close_file_stream(file_stream
);
1976 ctf_destroy_metadata(td
);
1977 ret
= close(td
->dirfd
);
1979 perror("Error closing dirfd");
1982 ret
= closedir(td
->dir
);
1984 perror("Error closedir");
1992 void ctf_set_context(struct trace_descriptor
*descriptor
,
1993 struct bt_context
*ctx
)
1995 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2002 void ctf_set_handle(struct trace_descriptor
*descriptor
,
2003 struct bt_trace_handle
*handle
)
2005 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2008 td
->handle
= handle
;
2012 void __attribute__((constructor
)) ctf_init(void)
2016 ctf_format
.name
= g_quark_from_static_string("ctf");
2017 ret
= bt_register_format(&ctf_format
);
2022 void __attribute__((destructor
)) ctf_exit(void)
2024 bt_unregister_format(&ctf_format
);