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.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/context-internal.h>
28 #include <babeltrace/uuid.h>
29 #include <babeltrace/endian.h>
34 #include <sys/types.h>
42 #include "metadata/ctf-scanner.h"
43 #include "metadata/ctf-parser.h"
44 #include "metadata/ctf-ast.h"
45 #include "events-private.h"
46 #include "memstream.h"
49 * We currently simply map a page to read the packet header and packet
50 * context to get the packet length and content length. (in bits)
52 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
53 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
56 #define min(a, b) (((a) < (b)) ? (a) : (b))
59 #define NSEC_PER_SEC 1000000000ULL
66 uint64_t opt_clock_offset
;
71 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
72 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
76 struct trace_descriptor
*ctf_open_mmap_trace(
77 struct mmap_stream_list
*mmap_list
,
78 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
82 void ctf_set_context(struct trace_descriptor
*descriptor
,
83 struct bt_context
*ctx
);
85 void ctf_set_handle(struct trace_descriptor
*descriptor
,
86 struct bt_trace_handle
*handle
);
89 int ctf_close_trace(struct trace_descriptor
*descriptor
);
91 uint64_t ctf_timestamp_begin(struct trace_descriptor
*descriptor
,
92 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
94 uint64_t ctf_timestamp_end(struct trace_descriptor
*descriptor
,
95 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
97 int ctf_convert_index_timestamp(struct trace_descriptor
*tdp
);
100 rw_dispatch read_dispatch_table
[] = {
101 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
102 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
103 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
104 [ CTF_TYPE_STRING
] = ctf_string_read
,
105 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
106 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
107 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
108 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
112 rw_dispatch write_dispatch_table
[] = {
113 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
114 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
115 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
116 [ CTF_TYPE_STRING
] = ctf_string_write
,
117 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
118 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
119 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
120 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
124 struct format ctf_format
= {
125 .open_trace
= ctf_open_trace
,
126 .open_mmap_trace
= ctf_open_mmap_trace
,
127 .close_trace
= ctf_close_trace
,
128 .set_context
= ctf_set_context
,
129 .set_handle
= ctf_set_handle
,
130 .timestamp_begin
= ctf_timestamp_begin
,
131 .timestamp_end
= ctf_timestamp_end
,
132 .convert_index_timestamp
= ctf_convert_index_timestamp
,
136 uint64_t ctf_timestamp_begin(struct trace_descriptor
*descriptor
,
137 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
139 struct ctf_trace
*tin
;
140 uint64_t begin
= ULLONG_MAX
;
143 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
148 /* for each stream_class */
149 for (i
= 0; i
< tin
->streams
->len
; i
++) {
150 struct ctf_stream_declaration
*stream_class
;
152 stream_class
= g_ptr_array_index(tin
->streams
, i
);
155 /* for each file_stream */
156 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
157 struct ctf_stream_definition
*stream
;
158 struct ctf_file_stream
*cfs
;
159 struct ctf_stream_pos
*stream_pos
;
160 struct packet_index
*index
;
162 stream
= g_ptr_array_index(stream_class
->streams
, j
);
163 cfs
= container_of(stream
, struct ctf_file_stream
,
165 stream_pos
= &cfs
->pos
;
167 if (!stream_pos
->packet_real_index
)
170 if (stream_pos
->packet_real_index
->len
<= 0)
173 if (type
== BT_CLOCK_REAL
) {
174 index
= &g_array_index(stream_pos
->packet_real_index
,
176 stream_pos
->packet_real_index
->len
- 1);
177 } else if (type
== BT_CLOCK_CYCLES
) {
178 index
= &g_array_index(stream_pos
->packet_cycles_index
,
180 stream_pos
->packet_real_index
->len
- 1);
185 if (index
->timestamp_begin
< begin
)
186 begin
= index
->timestamp_begin
;
197 uint64_t ctf_timestamp_end(struct trace_descriptor
*descriptor
,
198 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
200 struct ctf_trace
*tin
;
204 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
209 /* for each stream_class */
210 for (i
= 0; i
< tin
->streams
->len
; i
++) {
211 struct ctf_stream_declaration
*stream_class
;
213 stream_class
= g_ptr_array_index(tin
->streams
, i
);
216 /* for each file_stream */
217 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
218 struct ctf_stream_definition
*stream
;
219 struct ctf_file_stream
*cfs
;
220 struct ctf_stream_pos
*stream_pos
;
221 struct packet_index
*index
;
223 stream
= g_ptr_array_index(stream_class
->streams
, j
);
224 cfs
= container_of(stream
, struct ctf_file_stream
,
226 stream_pos
= &cfs
->pos
;
228 if (!stream_pos
->packet_real_index
)
231 if (stream_pos
->packet_real_index
->len
<= 0)
234 if (type
== BT_CLOCK_REAL
) {
235 index
= &g_array_index(stream_pos
->packet_real_index
,
237 stream_pos
->packet_real_index
->len
- 1);
238 } else if (type
== BT_CLOCK_CYCLES
) {
239 index
= &g_array_index(stream_pos
->packet_cycles_index
,
241 stream_pos
->packet_real_index
->len
- 1);
246 if (index
->timestamp_end
> end
)
247 end
= index
->timestamp_end
;
258 * Update stream current timestamp
261 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
262 struct definition_integer
*integer_definition
)
264 struct declaration_integer
*integer_declaration
=
265 integer_definition
->declaration
;
266 uint64_t oldval
, newval
, updateval
;
268 if (unlikely(integer_declaration
->len
== 64)) {
269 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
270 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
271 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
272 stream
->prev_cycles_timestamp
);
273 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
274 stream
->cycles_timestamp
);
278 oldval
= stream
->cycles_timestamp
;
279 oldval
&= (1ULL << integer_declaration
->len
) - 1;
280 newval
= integer_definition
->value
._unsigned
;
281 /* Test for overflow by comparing low bits */
283 newval
+= 1ULL << integer_declaration
->len
;
284 /* updateval contains old high bits, and new low bits (sum) */
285 updateval
= stream
->cycles_timestamp
;
286 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
288 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
289 stream
->cycles_timestamp
= updateval
;
291 /* convert to real timestamp */
292 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
293 stream
->prev_cycles_timestamp
);
294 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
295 stream
->cycles_timestamp
);
299 * Print timestamp, rescaling clock frequency to nanoseconds and
300 * applying offsets as needed (unix time).
302 void ctf_print_timestamp_real(FILE *fp
,
303 struct ctf_stream_definition
*stream
,
306 uint64_t ts_sec
= 0, ts_nsec
;
310 /* Add command-line offset */
311 ts_sec
+= opt_clock_offset
;
313 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
314 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
316 if (!opt_clock_seconds
) {
318 time_t time_s
= (time_t) ts_sec
;
320 if (!opt_clock_gmt
) {
323 res
= localtime_r(&time_s
, &tm
);
325 fprintf(stderr
, "[warning] Unable to get localtime.\n");
331 res
= gmtime_r(&time_s
, &tm
);
333 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
337 if (opt_clock_date
) {
341 /* Print date and time */
342 res
= strftime(timestr
, sizeof(timestr
),
345 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
348 fprintf(fp
, "%s", timestr
);
350 /* Print time in HH:MM:SS.ns */
351 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
352 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
356 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
364 * Print timestamp, in cycles
366 void ctf_print_timestamp_cycles(FILE *fp
,
367 struct ctf_stream_definition
*stream
,
370 fprintf(fp
, "%020" PRIu64
, timestamp
);
373 void ctf_print_timestamp(FILE *fp
,
374 struct ctf_stream_definition
*stream
,
377 if (opt_clock_cycles
) {
378 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
380 ctf_print_timestamp_real(fp
, stream
, timestamp
);
385 int ctf_read_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
387 struct ctf_stream_pos
*pos
=
388 container_of(ppos
, struct ctf_stream_pos
, parent
);
389 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
390 struct ctf_event_definition
*event
;
394 /* We need to check for EOF here for empty files. */
395 if (unlikely(pos
->offset
== EOF
))
398 ctf_pos_get_event(pos
);
400 /* save the current position as a restore point */
401 pos
->last_offset
= pos
->offset
;
404 * This is the EOF check after we've advanced the position in
407 if (unlikely(pos
->offset
== EOF
))
409 assert(pos
->offset
< pos
->content_size
);
411 /* Read event header */
412 if (likely(stream
->stream_event_header
)) {
413 struct definition_integer
*integer_definition
;
414 struct definition
*variant
;
416 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
419 /* lookup event id */
420 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
421 if (integer_definition
) {
422 id
= integer_definition
->value
._unsigned
;
424 struct definition_enum
*enum_definition
;
426 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
427 if (enum_definition
) {
428 id
= enum_definition
->integer
->value
._unsigned
;
432 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
434 integer_definition
= lookup_integer(variant
, "id", FALSE
);
435 if (integer_definition
) {
436 id
= integer_definition
->value
._unsigned
;
439 stream
->event_id
= id
;
441 /* lookup timestamp */
442 stream
->has_timestamp
= 0;
443 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
444 if (integer_definition
) {
445 ctf_update_timestamp(stream
, integer_definition
);
446 stream
->has_timestamp
= 1;
449 integer_definition
= lookup_integer(variant
, "timestamp", FALSE
);
450 if (integer_definition
) {
451 ctf_update_timestamp(stream
, integer_definition
);
452 stream
->has_timestamp
= 1;
458 /* Read stream-declared event context */
459 if (stream
->stream_event_context
) {
460 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
465 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
466 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
469 event
= g_ptr_array_index(stream
->events_by_id
, id
);
470 if (unlikely(!event
)) {
471 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
475 /* Read event-declared event context */
476 if (event
->event_context
) {
477 ret
= generic_rw(ppos
, &event
->event_context
->p
);
482 /* Read event payload */
483 if (likely(event
->event_fields
)) {
484 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
492 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
497 int ctf_write_event(struct stream_pos
*pos
, struct ctf_stream_definition
*stream
)
499 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
500 struct ctf_event_definition
*event
;
504 id
= stream
->event_id
;
506 /* print event header */
507 if (likely(stream
->stream_event_header
)) {
508 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
513 /* print stream-declared event context */
514 if (stream
->stream_event_context
) {
515 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
520 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
521 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
524 event
= g_ptr_array_index(stream
->events_by_id
, id
);
525 if (unlikely(!event
)) {
526 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
530 /* print event-declared event context */
531 if (event
->event_context
) {
532 ret
= generic_rw(pos
, &event
->event_context
->p
);
537 /* Read and print event payload */
538 if (likely(event
->event_fields
)) {
539 ret
= generic_rw(pos
, &event
->event_fields
->p
);
547 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
551 int ctf_init_pos(struct ctf_stream_pos
*pos
, int fd
, int open_flags
)
555 pos
->packet_cycles_index
= g_array_new(FALSE
, TRUE
,
556 sizeof(struct packet_index
));
557 pos
->packet_real_index
= g_array_new(FALSE
, TRUE
,
558 sizeof(struct packet_index
));
560 pos
->packet_cycles_index
= NULL
;
561 pos
->packet_real_index
= NULL
;
563 switch (open_flags
& O_ACCMODE
) {
565 pos
->prot
= PROT_READ
;
566 pos
->flags
= MAP_PRIVATE
;
567 pos
->parent
.rw_table
= read_dispatch_table
;
568 pos
->parent
.event_cb
= ctf_read_event
;
571 pos
->prot
= PROT_WRITE
; /* Write has priority */
572 pos
->flags
= MAP_SHARED
;
573 pos
->parent
.rw_table
= write_dispatch_table
;
574 pos
->parent
.event_cb
= ctf_write_event
;
576 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
584 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
586 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
587 *pos
->content_size_loc
= pos
->offset
;
592 ret
= munmap_align(pos
->base_mma
);
594 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
599 if (pos
->packet_cycles_index
)
600 (void) g_array_free(pos
->packet_cycles_index
, TRUE
);
601 if (pos
->packet_real_index
)
602 (void) g_array_free(pos
->packet_real_index
, TRUE
);
607 * for SEEK_CUR: go to next packet.
608 * for SEEK_POS: go to packet numer (index).
610 void ctf_packet_seek(struct stream_pos
*stream_pos
, size_t index
, int whence
)
612 struct ctf_stream_pos
*pos
=
613 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
614 struct ctf_file_stream
*file_stream
=
615 container_of(pos
, struct ctf_file_stream
, pos
);
618 struct packet_index
*packet_index
;
620 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
621 *pos
->content_size_loc
= pos
->offset
;
625 ret
= munmap_align(pos
->base_mma
);
627 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
631 pos
->base_mma
= NULL
;
635 * The caller should never ask for ctf_move_pos across packets,
636 * except to get exactly at the beginning of the next packet.
638 if (pos
->prot
== PROT_WRITE
) {
641 /* The writer will add padding */
642 pos
->mmap_offset
+= WRITE_PACKET_LEN
/ CHAR_BIT
;
645 assert(index
== 0); /* only seek supported for now */
651 pos
->content_size
= -1U; /* Unknown at this point */
652 pos
->packet_size
= WRITE_PACKET_LEN
;
653 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
654 pos
->packet_size
/ CHAR_BIT
);
662 uint64_t events_discarded_diff
;
664 if (pos
->offset
== EOF
) {
667 /* For printing discarded event count */
668 packet_index
= &g_array_index(pos
->packet_cycles_index
,
669 struct packet_index
, pos
->cur_index
);
670 file_stream
->parent
.prev_cycles_timestamp_end
=
671 packet_index
->timestamp_end
;
672 file_stream
->parent
.prev_cycles_timestamp
=
673 packet_index
->timestamp_begin
;
675 packet_index
= &g_array_index(pos
->packet_real_index
,
676 struct packet_index
, pos
->cur_index
);
677 file_stream
->parent
.prev_real_timestamp_end
=
678 packet_index
->timestamp_end
;
679 file_stream
->parent
.prev_real_timestamp
=
680 packet_index
->timestamp_begin
;
682 events_discarded_diff
= packet_index
->events_discarded
;
683 if (pos
->cur_index
> 0) {
684 packet_index
= &g_array_index(pos
->packet_real_index
,
687 events_discarded_diff
-= packet_index
->events_discarded
;
689 * Deal with 32-bit wrap-around if the
690 * tracer provided a 32-bit field.
692 if (packet_index
->events_discarded_len
== 32) {
693 events_discarded_diff
= (uint32_t) events_discarded_diff
;
696 file_stream
->parent
.events_discarded
= events_discarded_diff
;
697 file_stream
->parent
.prev_real_timestamp
= file_stream
->parent
.real_timestamp
;
698 file_stream
->parent
.prev_cycles_timestamp
= file_stream
->parent
.cycles_timestamp
;
699 /* The reader will expect us to skip padding */
704 packet_index
= &g_array_index(pos
->packet_cycles_index
,
705 struct packet_index
, index
);
706 pos
->last_events_discarded
= packet_index
->events_discarded
;
707 pos
->cur_index
= index
;
708 file_stream
->parent
.prev_real_timestamp
= 0;
709 file_stream
->parent
.prev_real_timestamp_end
= 0;
710 file_stream
->parent
.prev_cycles_timestamp
= 0;
711 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
716 if (pos
->cur_index
>= pos
->packet_real_index
->len
) {
718 * When a stream reaches the end of the
719 * file, we need to show the number of
720 * events discarded ourselves, because
721 * there is no next event scheduled to
722 * be printed in the output.
724 if (file_stream
->parent
.events_discarded
) {
726 * We need to check if we are in trace
727 * read or called from packet indexing.
728 * In this last case, the collection is
729 * not there, so we cannot print the
732 if ((&file_stream
->parent
)->stream_class
->trace
->collection
) {
734 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events at end of stream between [",
735 file_stream
->parent
.events_discarded
);
736 if (opt_clock_cycles
) {
737 ctf_print_timestamp(stderr
,
738 &file_stream
->parent
,
739 file_stream
->parent
.prev_cycles_timestamp
);
740 fprintf(stderr
, "] and [");
741 ctf_print_timestamp(stderr
, &file_stream
->parent
,
742 file_stream
->parent
.prev_cycles_timestamp_end
);
744 ctf_print_timestamp(stderr
,
745 &file_stream
->parent
,
746 file_stream
->parent
.prev_real_timestamp
);
747 fprintf(stderr
, "] and [");
748 ctf_print_timestamp(stderr
, &file_stream
->parent
,
749 file_stream
->parent
.prev_real_timestamp_end
);
751 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
754 file_stream
->parent
.events_discarded
= 0;
759 packet_index
= &g_array_index(pos
->packet_cycles_index
,
762 file_stream
->parent
.cycles_timestamp
= packet_index
->timestamp_begin
;
764 packet_index
= &g_array_index(pos
->packet_real_index
,
767 file_stream
->parent
.real_timestamp
= packet_index
->timestamp_begin
;
768 pos
->mmap_offset
= packet_index
->offset
;
770 /* Lookup context/packet size in index */
771 pos
->content_size
= packet_index
->content_size
;
772 pos
->packet_size
= packet_index
->packet_size
;
773 if (packet_index
->data_offset
< packet_index
->content_size
) {
774 pos
->offset
= 0; /* will read headers */
775 } else if (packet_index
->data_offset
== packet_index
->content_size
) {
777 pos
->offset
= packet_index
->data_offset
;
779 goto read_next_packet
;
785 /* map new base. Need mapping length from header. */
786 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
787 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
788 if (pos
->base_mma
== MAP_FAILED
) {
789 fprintf(stderr
, "[error] mmap error %s.\n",
794 /* update trace_packet_header and stream_packet_context */
795 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
796 /* Read packet header */
797 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
800 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
801 /* Read packet context */
802 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
808 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
814 len
= fread(&magic
, sizeof(magic
), 1, fp
);
818 if (magic
== TSDL_MAGIC
) {
820 td
->byte_order
= BYTE_ORDER
;
821 CTF_TRACE_SET_FIELD(td
, byte_order
);
822 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
824 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
825 LITTLE_ENDIAN
: BIG_ENDIAN
;
826 CTF_TRACE_SET_FIELD(td
, byte_order
);
834 * Returns 0 on success, -1 on error.
837 int check_version(unsigned int major
, unsigned int minor
)
852 /* eventually return an error instead of warning */
854 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
860 int ctf_open_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
863 struct metadata_packet_header header
;
864 size_t readlen
, writelen
, toread
;
865 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
868 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
872 if (td
->byte_order
!= BYTE_ORDER
) {
873 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
874 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
875 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
876 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
879 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
880 if (header
.compression_scheme
) {
881 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
882 header
.compression_scheme
);
885 if (header
.encryption_scheme
) {
886 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
887 header
.encryption_scheme
);
890 if (header
.checksum_scheme
) {
891 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
892 header
.checksum_scheme
);
895 if (check_version(header
.major
, header
.minor
) < 0)
897 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
898 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
899 CTF_TRACE_SET_FIELD(td
, uuid
);
901 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
905 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
908 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
913 if (babeltrace_debug
) {
915 fprintf(stderr
, "[debug] metadata packet read: %s\n",
919 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
920 if (writelen
< readlen
) {
930 ret
= 0; /* continue reading next packet */
937 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
938 ret
= fseek(in
, toread
, SEEK_CUR
);
940 fprintf(stderr
, "[warning] Missing padding at end of file\n");
947 int ctf_open_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
956 * Using strlen on *buf instead of size of open_memstream
957 * because its size includes garbage at the end (after final
958 * \0). This is the allocated size, not the actual string size.
960 out
= babeltrace_open_memstream(buf
, &size
);
962 perror("Metadata open_memstream");
966 ret
= ctf_open_trace_metadata_packet_read(td
, in
, out
);
975 /* close to flush the buffer */
976 ret
= babeltrace_close_memstream(buf
, &size
, out
);
980 perror("babeltrace_flush_memstream");
982 closeret
= fclose(in
);
984 perror("Error in fclose");
990 perror("Error in fclose");
992 /* open for reading */
993 *fp
= babeltrace_fmemopen(*buf
, strlen(*buf
), "rb");
995 perror("Metadata fmemopen");
1002 int ctf_open_trace_metadata_read(struct ctf_trace
*td
,
1003 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1004 int whence
), FILE *metadata_fp
)
1006 struct ctf_scanner
*scanner
;
1007 struct ctf_file_stream
*metadata_stream
;
1010 int ret
= 0, closeret
;
1012 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1013 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1016 metadata_stream
->pos
.packet_seek
= packet_seek
;
1018 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1025 metadata_stream
->pos
.fd
= -1;
1027 td
->metadata
= &metadata_stream
->parent
;
1028 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1029 if (metadata_stream
->pos
.fd
< 0) {
1030 fprintf(stderr
, "Unable to open metadata.\n");
1035 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1037 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1038 perror("Metadata stream open");
1042 /* fd now belongs to fp */
1043 metadata_stream
->pos
.fd
= -1;
1045 if (babeltrace_debug
)
1048 if (packet_metadata(td
, fp
)) {
1049 ret
= ctf_open_trace_metadata_stream_read(td
, &fp
, &buf
);
1051 goto end_packet_read
;
1053 unsigned int major
, minor
;
1056 td
->byte_order
= BYTE_ORDER
;
1058 /* Check text-only metadata header and version */
1059 nr_items
= fscanf(fp
, "/* CTF %u.%u", &major
, &minor
);
1061 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1062 if (check_version(major
, minor
) < 0) {
1064 goto end_packet_read
;
1069 scanner
= ctf_scanner_alloc(fp
);
1071 fprintf(stderr
, "[error] Error allocating scanner\n");
1073 goto end_scanner_alloc
;
1075 ret
= ctf_scanner_append_ast(scanner
);
1077 fprintf(stderr
, "[error] Error creating AST\n");
1081 if (babeltrace_debug
) {
1082 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1084 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1089 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1091 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1094 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1095 td
, td
->byte_order
);
1097 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1101 ctf_scanner_free(scanner
);
1105 closeret
= fclose(fp
);
1107 perror("Error on fclose");
1112 if (metadata_stream
->pos
.fd
>= 0) {
1113 closeret
= close(metadata_stream
->pos
.fd
);
1115 perror("Error on metadata stream fd close");
1120 g_free(metadata_stream
);
1125 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1126 struct ctf_stream_definition
*stream
,
1127 struct ctf_event_declaration
*event
)
1129 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1131 if (event
->context_decl
) {
1132 struct definition
*definition
=
1133 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1134 stream
->parent_def_scope
, 0, 0, "event.context");
1138 stream_event
->event_context
= container_of(definition
,
1139 struct definition_struct
, p
);
1140 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1142 if (event
->fields_decl
) {
1143 struct definition
*definition
=
1144 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1145 stream
->parent_def_scope
, 0, 0, "event.fields");
1149 stream_event
->event_fields
= container_of(definition
,
1150 struct definition_struct
, p
);
1151 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1153 stream_event
->stream
= stream
;
1154 return stream_event
;
1157 if (stream_event
->event_fields
)
1158 definition_unref(&stream_event
->event_fields
->p
);
1159 if (stream_event
->event_context
)
1160 definition_unref(&stream_event
->event_context
->p
);
1165 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1167 struct ctf_stream_declaration
*stream_class
;
1171 if (stream
->stream_definitions_created
)
1174 stream_class
= stream
->stream_class
;
1176 if (stream_class
->packet_context_decl
) {
1177 struct definition
*definition
=
1178 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1179 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1184 stream
->stream_packet_context
= container_of(definition
,
1185 struct definition_struct
, p
);
1186 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1188 if (stream_class
->event_header_decl
) {
1189 struct definition
*definition
=
1190 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1191 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1196 stream
->stream_event_header
=
1197 container_of(definition
, struct definition_struct
, p
);
1198 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1200 if (stream_class
->event_context_decl
) {
1201 struct definition
*definition
=
1202 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1203 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1208 stream
->stream_event_context
=
1209 container_of(definition
, struct definition_struct
, p
);
1210 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1212 stream
->events_by_id
= g_ptr_array_new();
1213 g_ptr_array_set_size(stream
->events_by_id
, stream_class
->events_by_id
->len
);
1214 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1215 struct ctf_event_declaration
*event
= g_ptr_array_index(stream_class
->events_by_id
, i
);
1216 struct ctf_event_definition
*stream_event
;
1220 stream_event
= create_event_definitions(td
, stream
, event
);
1223 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1228 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1229 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1231 g_free(stream_event
);
1233 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1235 if (stream
->stream_event_context
)
1236 definition_unref(&stream
->stream_event_context
->p
);
1237 if (stream
->stream_event_header
)
1238 definition_unref(&stream
->stream_event_header
->p
);
1239 if (stream
->stream_packet_context
)
1240 definition_unref(&stream
->stream_packet_context
->p
);
1246 int create_stream_packet_index(struct ctf_trace
*td
,
1247 struct ctf_file_stream
*file_stream
)
1249 struct ctf_stream_declaration
*stream
;
1251 struct ctf_stream_pos
*pos
;
1252 struct stat filestats
;
1253 struct packet_index packet_index
;
1254 int first_packet
= 1;
1257 pos
= &file_stream
->pos
;
1259 ret
= fstat(pos
->fd
, &filestats
);
1263 if (filestats
.st_size
< MAX_PACKET_HEADER_LEN
/ CHAR_BIT
)
1266 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1267 uint64_t stream_id
= 0;
1269 if (pos
->base_mma
) {
1270 /* unmap old base */
1271 ret
= munmap_align(pos
->base_mma
);
1273 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1277 pos
->base_mma
= NULL
;
1279 /* map new base. Need mapping length from header. */
1280 pos
->base_mma
= mmap_align(MAX_PACKET_HEADER_LEN
/ CHAR_BIT
, PROT_READ
,
1281 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1282 assert(pos
->base_mma
!= MAP_FAILED
);
1283 pos
->content_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1284 pos
->packet_size
= MAX_PACKET_HEADER_LEN
; /* Unknown at this point */
1285 pos
->offset
= 0; /* Position of the packet header */
1287 packet_index
.offset
= pos
->mmap_offset
;
1288 packet_index
.content_size
= 0;
1289 packet_index
.packet_size
= 0;
1290 packet_index
.timestamp_begin
= 0;
1291 packet_index
.timestamp_end
= 0;
1292 packet_index
.events_discarded
= 0;
1293 packet_index
.events_discarded_len
= 0;
1295 /* read and check header, set stream id (and check) */
1296 if (file_stream
->parent
.trace_packet_header
) {
1297 /* Read packet header */
1298 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1301 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1302 if (len_index
>= 0) {
1303 struct definition
*field
;
1306 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1307 magic
= get_unsigned_int(field
);
1308 if (magic
!= CTF_MAGIC
) {
1309 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1311 file_stream
->pos
.packet_cycles_index
->len
,
1312 (ssize_t
) pos
->mmap_offset
);
1318 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1319 if (len_index
>= 0) {
1320 struct definition_array
*defarray
;
1321 struct definition
*field
;
1323 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1325 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1326 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1327 defarray
= container_of(field
, struct definition_array
, p
);
1328 assert(array_len(defarray
) == BABELTRACE_UUID_LEN
);
1330 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1331 struct definition
*elem
;
1333 elem
= array_index(defarray
, i
);
1334 uuidval
[i
] = get_unsigned_int(elem
);
1336 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1338 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1344 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1345 if (len_index
>= 0) {
1346 struct definition
*field
;
1348 field
= struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1349 stream_id
= get_unsigned_int(field
);
1353 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1354 fprintf(stderr
, "[error] Stream ID is changing within a stream.\n");
1358 file_stream
->parent
.stream_id
= stream_id
;
1359 if (stream_id
>= td
->streams
->len
) {
1360 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1363 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1365 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1368 file_stream
->parent
.stream_class
= stream
;
1369 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1375 if (file_stream
->parent
.stream_packet_context
) {
1376 /* Read packet context */
1377 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1380 /* read content size from header */
1381 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1382 if (len_index
>= 0) {
1383 struct definition
*field
;
1385 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1386 packet_index
.content_size
= get_unsigned_int(field
);
1388 /* Use file size for packet size */
1389 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1392 /* read packet size from header */
1393 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1394 if (len_index
>= 0) {
1395 struct definition
*field
;
1397 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1398 packet_index
.packet_size
= get_unsigned_int(field
);
1400 /* Use content size if non-zero, else file size */
1401 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1404 /* read timestamp begin from header */
1405 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1406 if (len_index
>= 0) {
1407 struct definition
*field
;
1409 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1410 packet_index
.timestamp_begin
= get_unsigned_int(field
);
1411 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1412 packet_index
.timestamp_begin
=
1413 ctf_get_real_timestamp(
1414 &file_stream
->parent
,
1415 packet_index
.timestamp_begin
);
1419 /* read timestamp end from header */
1420 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1421 if (len_index
>= 0) {
1422 struct definition
*field
;
1424 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1425 packet_index
.timestamp_end
= get_unsigned_int(field
);
1426 if (file_stream
->parent
.stream_class
->trace
->collection
) {
1427 packet_index
.timestamp_end
=
1428 ctf_get_real_timestamp(
1429 &file_stream
->parent
,
1430 packet_index
.timestamp_end
);
1434 /* read events discarded from header */
1435 len_index
= struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1436 if (len_index
>= 0) {
1437 struct definition
*field
;
1439 field
= struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1440 packet_index
.events_discarded
= get_unsigned_int(field
);
1441 packet_index
.events_discarded_len
= get_int_len(field
);
1444 /* Use file size for packet size */
1445 packet_index
.content_size
= filestats
.st_size
* CHAR_BIT
;
1446 /* Use content size if non-zero, else file size */
1447 packet_index
.packet_size
= packet_index
.content_size
? : filestats
.st_size
* CHAR_BIT
;
1450 /* Validate content size and packet size values */
1451 if (packet_index
.content_size
> packet_index
.packet_size
) {
1452 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1453 packet_index
.content_size
, packet_index
.packet_size
);
1457 if (packet_index
.packet_size
> ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
) {
1458 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1459 packet_index
.packet_size
, ((uint64_t)filestats
.st_size
- packet_index
.offset
) * CHAR_BIT
);
1463 /* Save position after header and context */
1464 packet_index
.data_offset
= pos
->offset
;
1466 /* add index to packet array */
1467 g_array_append_val(file_stream
->pos
.packet_cycles_index
, packet_index
);
1469 pos
->mmap_offset
+= packet_index
.packet_size
/ CHAR_BIT
;
1476 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1480 if (td
->packet_header_decl
) {
1481 struct definition
*definition
=
1482 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1483 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1488 stream
->trace_packet_header
=
1489 container_of(definition
, struct definition_struct
, p
);
1490 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1500 * Note: many file streams can inherit from the same stream class
1501 * description (metadata).
1504 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1505 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1508 int ret
, fd
, closeret
;
1509 struct ctf_file_stream
*file_stream
;
1510 struct stat statbuf
;
1512 fd
= openat(td
->dirfd
, path
, flags
);
1514 perror("File stream openat()");
1519 /* Don't try to mmap subdirectories. Skip them, return success. */
1520 ret
= fstat(fd
, &statbuf
);
1522 perror("File stream fstat()");
1525 if (S_ISDIR(statbuf
.st_mode
)) {
1526 fprintf(stderr
, "[warning] Skipping directory '%s' found in trace\n", path
);
1531 file_stream
= g_new0(struct ctf_file_stream
, 1);
1532 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1535 file_stream
->pos
.packet_seek
= packet_seek
;
1537 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1542 ret
= ctf_init_pos(&file_stream
->pos
, fd
, flags
);
1545 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1549 * For now, only a single clock per trace is supported.
1551 file_stream
->parent
.current_clock
= td
->single_clock
;
1552 ret
= create_stream_packet_index(td
, file_stream
);
1555 /* Add stream file to stream class */
1556 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1557 &file_stream
->parent
);
1561 if (file_stream
->parent
.trace_packet_header
)
1562 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1564 closeret
= ctf_fini_pos(&file_stream
->pos
);
1566 fprintf(stderr
, "Error on ctf_fini_pos\n");
1568 g_free(file_stream
);
1571 closeret
= close(fd
);
1573 perror("Error on fd close");
1580 int ctf_open_trace_read(struct ctf_trace
*td
,
1581 const char *path
, int flags
,
1582 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1583 int whence
), FILE *metadata_fp
)
1586 struct dirent
*dirent
;
1587 struct dirent
*diriter
;
1592 /* Open trace directory */
1593 td
->dir
= opendir(path
);
1595 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
1600 td
->dirfd
= open(path
, 0);
1601 if (td
->dirfd
< 0) {
1602 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
1603 perror("Trace directory open");
1607 strncpy(td
->path
, path
, sizeof(td
->path
));
1608 td
->path
[sizeof(td
->path
) - 1] = '\0';
1611 * Keep the metadata file separate.
1614 ret
= ctf_open_trace_metadata_read(td
, packet_seek
, metadata_fp
);
1616 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
1617 goto error_metadata
;
1621 * Open each stream: for each file, try to open, check magic
1622 * number, and get the stream ID to add to the right location in
1626 dirent_len
= offsetof(struct dirent
, d_name
) +
1627 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
1629 dirent
= malloc(dirent_len
);
1632 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
1634 fprintf(stderr
, "[error] Readdir error.\n");
1639 /* Ignore hidden files, ., .. and metadata. */
1640 if (!strncmp(diriter
->d_name
, ".", 1)
1641 || !strcmp(diriter
->d_name
, "..")
1642 || !strcmp(diriter
->d_name
, "metadata"))
1644 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
1645 flags
, packet_seek
);
1647 fprintf(stderr
, "[error] Open file stream error.\n");
1658 closeret
= close(td
->dirfd
);
1660 perror("Error on fd close");
1663 closeret
= closedir(td
->dir
);
1665 perror("Error on closedir");
1672 * ctf_open_trace: Open a CTF trace and index it.
1673 * Note that the user must seek the trace after the open (using the iterator)
1674 * since the index creation read it entirely.
1677 struct trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
1678 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1679 int whence
), FILE *metadata_fp
)
1681 struct ctf_trace
*td
;
1685 * If packet_seek is NULL, we provide our default version.
1688 packet_seek
= ctf_packet_seek
;
1690 td
= g_new0(struct ctf_trace
, 1);
1692 switch (flags
& O_ACCMODE
) {
1695 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
1698 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
1703 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
1706 fprintf(stderr
, "[error] Incorrect open flags.\n");
1717 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
1718 struct mmap_stream
*mmap_info
)
1720 pos
->mmap_offset
= 0;
1721 pos
->packet_size
= 0;
1722 pos
->content_size
= 0;
1723 pos
->content_size_loc
= NULL
;
1724 pos
->fd
= mmap_info
->fd
;
1725 pos
->base_mma
= NULL
;
1729 pos
->packet_cycles_index
= NULL
;
1730 pos
->packet_real_index
= NULL
;
1731 pos
->prot
= PROT_READ
;
1732 pos
->flags
= MAP_PRIVATE
;
1733 pos
->parent
.rw_table
= read_dispatch_table
;
1734 pos
->parent
.event_cb
= ctf_read_event
;
1738 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
1739 struct ctf_file_stream
*file_stream
)
1741 struct ctf_stream_declaration
*stream
;
1742 uint64_t stream_id
= 0;
1745 file_stream
->parent
.stream_id
= stream_id
;
1746 if (stream_id
>= td
->streams
->len
) {
1747 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1748 "in metadata.\n", stream_id
);
1752 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1754 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
1755 "in metadata.\n", stream_id
);
1759 file_stream
->parent
.stream_class
= stream
;
1760 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1766 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
1767 struct mmap_stream
*mmap_info
,
1768 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1772 struct ctf_file_stream
*file_stream
;
1774 file_stream
= g_new0(struct ctf_file_stream
, 1);
1775 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1776 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
1778 file_stream
->pos
.packet_seek
= packet_seek
;
1780 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1785 ret
= prepare_mmap_stream_definition(td
, file_stream
);
1790 * For now, only a single clock per trace is supported.
1792 file_stream
->parent
.current_clock
= td
->single_clock
;
1794 /* Add stream file to stream class */
1795 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
1796 &file_stream
->parent
);
1800 if (file_stream
->parent
.trace_packet_header
)
1801 definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
1803 g_free(file_stream
);
1807 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
1808 struct mmap_stream_list
*mmap_list
,
1809 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1814 struct mmap_stream
*mmap_info
;
1816 ret
= ctf_open_trace_metadata_read(td
, ctf_packet_seek
, metadata_fp
);
1822 * for each stream, try to open, check magic number, and get the
1823 * stream ID to add to the right location in the stream array.
1825 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
1826 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
1828 fprintf(stderr
, "[error] Open file mmap stream error.\n");
1840 struct trace_descriptor
*ctf_open_mmap_trace(
1841 struct mmap_stream_list
*mmap_list
,
1842 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
1846 struct ctf_trace
*td
;
1850 fprintf(stderr
, "[error] No metadata file pointer associated, "
1851 "required for mmap parsing\n");
1855 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1858 td
= g_new0(struct ctf_trace
, 1);
1859 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
1872 int ctf_convert_index_timestamp(struct trace_descriptor
*tdp
)
1875 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1877 /* for each stream_class */
1878 for (i
= 0; i
< td
->streams
->len
; i
++) {
1879 struct ctf_stream_declaration
*stream_class
;
1881 stream_class
= g_ptr_array_index(td
->streams
, i
);
1884 /* for each file_stream */
1885 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
1886 struct ctf_stream_definition
*stream
;
1887 struct ctf_stream_pos
*stream_pos
;
1888 struct ctf_file_stream
*cfs
;
1890 stream
= g_ptr_array_index(stream_class
->streams
, j
);
1893 cfs
= container_of(stream
, struct ctf_file_stream
,
1895 stream_pos
= &cfs
->pos
;
1896 if (!stream_pos
->packet_cycles_index
)
1899 for (k
= 0; k
< stream_pos
->packet_cycles_index
->len
; k
++) {
1900 struct packet_index
*index
;
1901 struct packet_index new_index
;
1903 index
= &g_array_index(stream_pos
->packet_cycles_index
,
1904 struct packet_index
, k
);
1905 memcpy(&new_index
, index
,
1906 sizeof(struct packet_index
));
1907 new_index
.timestamp_begin
=
1908 ctf_get_real_timestamp(stream
,
1909 index
->timestamp_begin
);
1910 new_index
.timestamp_end
=
1911 ctf_get_real_timestamp(stream
,
1912 index
->timestamp_end
);
1913 g_array_append_val(stream_pos
->packet_real_index
,
1922 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
1926 ret
= ctf_fini_pos(&file_stream
->pos
);
1928 fprintf(stderr
, "Error on ctf_fini_pos\n");
1931 ret
= close(file_stream
->pos
.fd
);
1933 perror("Error closing file fd");
1940 int ctf_close_trace(struct trace_descriptor
*tdp
)
1942 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
1948 for (i
= 0; i
< td
->streams
->len
; i
++) {
1949 struct ctf_stream_declaration
*stream
;
1952 stream
= g_ptr_array_index(td
->streams
, i
);
1955 for (j
= 0; j
< stream
->streams
->len
; j
++) {
1956 struct ctf_file_stream
*file_stream
;
1957 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
1958 struct ctf_file_stream
, parent
);
1959 ret
= ctf_close_file_stream(file_stream
);
1965 ctf_destroy_metadata(td
);
1966 ret
= close(td
->dirfd
);
1968 perror("Error closing dirfd");
1971 ret
= closedir(td
->dir
);
1973 perror("Error closedir");
1981 void ctf_set_context(struct trace_descriptor
*descriptor
,
1982 struct bt_context
*ctx
)
1984 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1991 void ctf_set_handle(struct trace_descriptor
*descriptor
,
1992 struct bt_trace_handle
*handle
)
1994 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
1997 td
->handle
= handle
;
2001 void __attribute__((constructor
)) ctf_init(void)
2005 ctf_format
.name
= g_quark_from_static_string("ctf");
2006 ret
= bt_register_format(&ctf_format
);
2011 void __attribute__((destructor
)) ctf_exit(void)
2013 bt_unregister_format(&ctf_format
);