2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <babeltrace/trace-handle-internal.h>
35 #include <babeltrace/context-internal.h>
36 #include <babeltrace/compat/uuid.h>
37 #include <babeltrace/endian.h>
38 #include <babeltrace/trace-debuginfo.h>
39 #include <babeltrace/ctf/ctf-index.h>
44 #include <sys/types.h>
52 #include "metadata/ctf-scanner.h"
53 #include "metadata/ctf-parser.h"
54 #include "metadata/ctf-ast.h"
55 #include "events-private.h"
56 #include <babeltrace/compat/memstream.h>
57 #include <babeltrace/compat/fcntl.h>
59 #define LOG2_CHAR_BIT 3
62 * Length of first attempt at mapping a packet header, in bits.
64 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
67 * Lenght of packet to write, in bits.
69 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
72 #define min(a, b) (((a) < (b)) ? (a) : (b))
75 #define NSEC_PER_SEC 1000000000LL
77 #define INDEX_PATH "./index/%s.idx"
84 int64_t opt_clock_offset
;
85 int64_t opt_clock_offset_ns
;
88 char *opt_debug_info_dir
;
91 * TODO: babeltrace_ctf_console_output ensures that we only print
92 * discarded events when ctf-text plugin is used. Should be cleaned up
93 * with the plugin system redesign.
95 int babeltrace_ctf_console_output
;
98 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
99 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
103 struct bt_trace_descriptor
*ctf_open_mmap_trace(
104 struct bt_mmap_stream_list
*mmap_list
,
105 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
109 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
110 struct bt_context
*ctx
);
112 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
113 struct bt_trace_handle
*handle
);
116 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
118 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
119 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
122 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
123 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
126 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
129 rw_dispatch read_dispatch_table
[] = {
130 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_read
,
131 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_read
,
132 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_read
,
133 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_read
,
134 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
135 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
136 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_read
,
137 [ BT_CTF_TYPE_ID_SEQUENCE
] = ctf_sequence_read
,
141 rw_dispatch write_dispatch_table
[] = {
142 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_write
,
143 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_write
,
144 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_write
,
145 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_write
,
146 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
147 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
148 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_write
,
149 [ BT_CTF_TYPE_ID_SEQUENCE
] = ctf_sequence_write
,
153 struct bt_format ctf_format
= {
154 .open_trace
= ctf_open_trace
,
155 .open_mmap_trace
= ctf_open_mmap_trace
,
156 .close_trace
= ctf_close_trace
,
157 .set_context
= ctf_set_context
,
158 .set_handle
= ctf_set_handle
,
159 .timestamp_begin
= ctf_timestamp_begin
,
160 .timestamp_end
= ctf_timestamp_end
,
161 .convert_index_timestamp
= ctf_convert_index_timestamp
,
165 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
166 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
169 struct ctf_trace
*tin
;
170 int64_t begin
= LLONG_MAX
;
173 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
175 if (!tin
|| !timestamp
) {
180 /* for each stream_class */
181 for (i
= 0; i
< tin
->streams
->len
; i
++) {
182 struct ctf_stream_declaration
*stream_class
;
184 stream_class
= g_ptr_array_index(tin
->streams
, i
);
187 /* for each file_stream */
188 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
189 struct ctf_stream_definition
*stream
;
190 struct ctf_file_stream
*cfs
;
191 struct ctf_stream_pos
*stream_pos
;
192 struct packet_index
*index
;
194 stream
= g_ptr_array_index(stream_class
->streams
, j
);
195 cfs
= container_of(stream
, struct ctf_file_stream
,
197 stream_pos
= &cfs
->pos
;
199 if (!stream_pos
->packet_index
) {
204 if (stream_pos
->packet_index
->len
<= 0)
207 index
= &g_array_index(stream_pos
->packet_index
,
209 stream_pos
->packet_index
->len
- 1);
210 if (type
== BT_CLOCK_REAL
) {
211 if (index
->ts_real
.timestamp_begin
< begin
)
212 begin
= index
->ts_real
.timestamp_begin
;
213 } else if (type
== BT_CLOCK_CYCLES
) {
214 if (index
->ts_cycles
.timestamp_begin
< begin
)
215 begin
= index
->ts_cycles
.timestamp_begin
;
222 if (begin
== LLONG_MAX
) {
234 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
235 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
238 struct ctf_trace
*tin
;
239 int64_t end
= LLONG_MIN
;
242 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
244 if (!tin
|| !timestamp
) {
249 /* for each stream_class */
250 for (i
= 0; i
< tin
->streams
->len
; i
++) {
251 struct ctf_stream_declaration
*stream_class
;
253 stream_class
= g_ptr_array_index(tin
->streams
, i
);
256 /* for each file_stream */
257 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
258 struct ctf_stream_definition
*stream
;
259 struct ctf_file_stream
*cfs
;
260 struct ctf_stream_pos
*stream_pos
;
261 struct packet_index
*index
;
263 stream
= g_ptr_array_index(stream_class
->streams
, j
);
264 cfs
= container_of(stream
, struct ctf_file_stream
,
266 stream_pos
= &cfs
->pos
;
268 if (!stream_pos
->packet_index
) {
273 if (stream_pos
->packet_index
->len
<= 0)
276 index
= &g_array_index(stream_pos
->packet_index
,
278 stream_pos
->packet_index
->len
- 1);
279 if (type
== BT_CLOCK_REAL
) {
280 if (index
->ts_real
.timestamp_end
> end
)
281 end
= index
->ts_real
.timestamp_end
;
282 } else if (type
== BT_CLOCK_CYCLES
) {
283 if (index
->ts_cycles
.timestamp_end
> end
)
284 end
= index
->ts_cycles
.timestamp_end
;
291 if (end
== LLONG_MIN
) {
303 * Update stream current timestamp
306 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
307 struct definition_integer
*integer_definition
)
309 struct declaration_integer
*integer_declaration
=
310 integer_definition
->declaration
;
311 uint64_t oldval
, newval
, updateval
;
313 if (unlikely(integer_declaration
->len
== 64)) {
314 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
315 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
316 stream
->cycles_timestamp
);
320 oldval
= stream
->cycles_timestamp
;
321 oldval
&= (1ULL << integer_declaration
->len
) - 1;
322 newval
= integer_definition
->value
._unsigned
;
323 /* Test for overflow by comparing low bits */
325 newval
+= 1ULL << integer_declaration
->len
;
326 /* updateval contains old high bits, and new low bits (sum) */
327 updateval
= stream
->cycles_timestamp
;
328 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
330 stream
->cycles_timestamp
= updateval
;
332 /* convert to real timestamp */
333 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
334 stream
->cycles_timestamp
);
338 * Print timestamp, rescaling clock frequency to nanoseconds and
339 * applying offsets as needed (unix time).
342 void ctf_print_timestamp_real(FILE *fp
,
343 struct ctf_stream_definition
*stream
,
346 int64_t ts_sec
= 0, ts_nsec
;
347 uint64_t ts_sec_abs
, ts_nsec_abs
;
352 /* Add command-line offset in ns */
353 ts_nsec
+= opt_clock_offset_ns
;
355 /* Add command-line offset */
356 ts_sec
+= opt_clock_offset
;
358 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
359 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
360 if (ts_sec
>= 0 && ts_nsec
>= 0) {
363 ts_nsec_abs
= ts_nsec
;
364 } else if (ts_sec
> 0 && ts_nsec
< 0) {
366 ts_sec_abs
= ts_sec
- 1;
367 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
368 } else if (ts_sec
== 0 && ts_nsec
< 0) {
371 ts_nsec_abs
= -ts_nsec
;
372 } else if (ts_sec
< 0 && ts_nsec
> 0) {
374 ts_sec_abs
= -(ts_sec
+ 1);
375 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
376 } else if (ts_sec
< 0 && ts_nsec
== 0) {
378 ts_sec_abs
= -ts_sec
;
379 ts_nsec_abs
= ts_nsec
;
380 } else { /* (ts_sec < 0 && ts_nsec < 0) */
382 ts_sec_abs
= -ts_sec
;
383 ts_nsec_abs
= -ts_nsec
;
386 if (!opt_clock_seconds
) {
388 time_t time_s
= (time_t) ts_sec_abs
;
391 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
395 if (!opt_clock_gmt
) {
398 res
= localtime_r(&time_s
, &tm
);
400 fprintf(stderr
, "[warning] Unable to get localtime.\n");
406 res
= gmtime_r(&time_s
, &tm
);
408 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
412 if (opt_clock_date
) {
416 /* Print date and time */
417 res
= strftime(timestr
, sizeof(timestr
),
420 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
423 fprintf(fp
, "%s", timestr
);
425 /* Print time in HH:MM:SS.ns */
426 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
427 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec_abs
);
431 fprintf(fp
, "%s%" PRId64
".%09" PRIu64
,
432 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
439 * Print timestamp, in cycles
442 void ctf_print_timestamp_cycles(FILE *fp
,
443 struct ctf_stream_definition
*stream
,
446 fprintf(fp
, "%020" PRIu64
, timestamp
);
449 void ctf_print_timestamp(FILE *fp
,
450 struct ctf_stream_definition
*stream
,
453 if (opt_clock_cycles
) {
454 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
456 ctf_print_timestamp_real(fp
, stream
, timestamp
);
461 void print_uuid(FILE *fp
, unsigned char *uuid
)
465 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
466 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
470 * Discarded events can be either:
471 * - discarded after end of previous buffer due to buffer full:
472 * happened within range: [ prev_timestamp_end, timestamp_begin ]
473 * - discarded within current buffer due to either event too large or
474 * nested wrap-around:
475 * happened within range: [ timestamp_begin, timestamp_end ]
477 * Given we have discarded counters of those two types merged into the
478 * events_discarded counter, we need to use the union of those ranges:
479 * [ prev_timestamp_end, timestamp_end ]
481 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
482 * consumer had time to extract them. We keep track of those gaps with the
483 * packet sequence number in each packet.
486 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
488 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
489 !babeltrace_ctf_console_output
) {
493 if (stream
->events_discarded
) {
494 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
495 stream
->events_discarded
);
496 } else if (stream
->packets_lost
) {
497 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
498 stream
->packets_lost
);
500 if (opt_clock_cycles
) {
501 ctf_print_timestamp(fp
, stream
,
502 stream
->prev
.cycles
.end
);
503 fprintf(fp
, "] and [");
504 ctf_print_timestamp(fp
, stream
,
505 stream
->current
.cycles
.end
);
507 ctf_print_timestamp(fp
, stream
,
508 stream
->prev
.real
.end
);
509 fprintf(fp
, "] and [");
510 ctf_print_timestamp(fp
, stream
,
511 stream
->current
.real
.end
);
513 fprintf(fp
, "] in trace UUID ");
514 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
515 if (stream
->stream_class
->trace
->parent
.path
[0])
516 fprintf(fp
, ", at path: \"%s\"",
517 stream
->stream_class
->trace
->parent
.path
);
519 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
521 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
523 fprintf(fp
, "You should consider recording a new trace with larger "
524 "buffers or with fewer events enabled.\n");
529 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
531 struct ctf_stream_pos
*pos
=
532 container_of(ppos
, struct ctf_stream_pos
, parent
);
533 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
534 struct ctf_event_definition
*event
;
538 /* We need to check for EOF here for empty files. */
539 if (unlikely(pos
->offset
== EOF
))
542 ctf_pos_get_event(pos
);
544 /* save the current position as a restore point */
545 pos
->last_offset
= pos
->offset
;
548 * This is the EOF check after we've advanced the position in
551 if (unlikely(pos
->offset
== EOF
))
554 /* Stream is inactive for now (live reading). */
555 if (unlikely(pos
->content_size
== 0))
559 * Packet seeked to by ctf_pos_get_event() only contains
560 * headers, no event. Consider stream as inactive (live
563 if (unlikely(pos
->data_offset
== pos
->content_size
))
566 assert(pos
->offset
< pos
->content_size
);
568 /* Read event header */
569 if (likely(stream
->stream_event_header
)) {
570 struct definition_integer
*integer_definition
;
571 struct bt_definition
*variant
;
573 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
576 /* lookup event id */
577 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
578 if (integer_definition
) {
579 id
= integer_definition
->value
._unsigned
;
581 struct definition_enum
*enum_definition
;
583 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
584 if (enum_definition
) {
585 id
= enum_definition
->integer
->value
._unsigned
;
589 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
591 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
592 if (integer_definition
) {
593 id
= integer_definition
->value
._unsigned
;
596 stream
->event_id
= id
;
598 /* lookup timestamp */
599 stream
->has_timestamp
= 0;
600 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
601 if (integer_definition
) {
602 ctf_update_timestamp(stream
, integer_definition
);
603 stream
->has_timestamp
= 1;
606 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
607 if (integer_definition
) {
608 ctf_update_timestamp(stream
, integer_definition
);
609 stream
->has_timestamp
= 1;
615 /* Read stream-declared event context */
616 if (stream
->stream_event_context
) {
617 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
622 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
623 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
626 event
= g_ptr_array_index(stream
->events_by_id
, id
);
627 if (unlikely(!event
)) {
628 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
632 /* Read event-declared event context */
633 if (event
->event_context
) {
634 ret
= generic_rw(ppos
, &event
->event_context
->p
);
639 /* Read event payload */
640 if (likely(event
->event_fields
)) {
641 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
646 if (pos
->last_offset
== pos
->offset
) {
647 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
654 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
659 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
661 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
662 struct ctf_event_definition
*event
;
666 id
= stream
->event_id
;
668 /* print event header */
669 if (likely(stream
->stream_event_header
)) {
670 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
675 /* print stream-declared event context */
676 if (stream
->stream_event_context
) {
677 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
682 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
683 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
686 event
= g_ptr_array_index(stream
->events_by_id
, id
);
687 if (unlikely(!event
)) {
688 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
692 /* print event-declared event context */
693 if (event
->event_context
) {
694 ret
= generic_rw(pos
, &event
->event_context
->p
);
699 /* Read and print event payload */
700 if (likely(event
->event_fields
)) {
701 ret
= generic_rw(pos
, &event
->event_fields
->p
);
709 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
714 * One side-effect of this function is to unmap pos mmap base if one is
718 int find_data_offset(struct ctf_stream_pos
*pos
,
719 struct ctf_file_stream
*file_stream
,
720 struct packet_index
*packet_index
)
722 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
723 struct stat filestats
;
727 pos
= &file_stream
->pos
;
729 ret
= fstat(pos
->fd
, &filestats
);
732 filesize
= filestats
.st_size
;
734 /* Deal with empty files */
740 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
741 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
746 ret
= munmap_align(pos
->base_mma
);
748 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
752 pos
->base_mma
= NULL
;
754 /* map new base. Need mapping length from header. */
755 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
756 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
757 assert(pos
->base_mma
!= MAP_FAILED
);
759 pos
->content_size
= packet_map_len
;
760 pos
->packet_size
= packet_map_len
;
761 pos
->offset
= 0; /* Position of the packet header */
763 /* update trace_packet_header and stream_packet_context */
764 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
765 /* Read packet header */
766 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
772 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
773 /* Read packet context */
774 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
780 packet_index
->data_offset
= pos
->offset
;
783 ret
= munmap_align(pos
->base_mma
);
785 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
789 pos
->base_mma
= NULL
;
793 /* Retry with larger mapping */
795 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
797 * Reached EOF, but still expecting header/context data.
799 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
802 /* Double the mapping len, and retry */
803 tmp_map_len
= packet_map_len
<< 1;
804 if (tmp_map_len
>> 1 != packet_map_len
) {
806 fprintf(stderr
, "[error] Packet mapping length overflow\n");
809 packet_map_len
= tmp_map_len
;
814 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
815 int fd
, int open_flags
)
819 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
820 sizeof(struct packet_index
));
822 pos
->packet_index
= NULL
;
824 switch (open_flags
& O_ACCMODE
) {
826 pos
->prot
= PROT_READ
;
827 pos
->flags
= MAP_PRIVATE
;
828 pos
->parent
.rw_table
= read_dispatch_table
;
829 pos
->parent
.event_cb
= ctf_read_event
;
830 pos
->parent
.trace
= trace
;
833 pos
->prot
= PROT_READ
| PROT_WRITE
;
834 pos
->flags
= MAP_SHARED
;
835 pos
->parent
.rw_table
= write_dispatch_table
;
836 pos
->parent
.event_cb
= ctf_write_event
;
837 pos
->parent
.trace
= trace
;
845 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
847 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
848 *pos
->content_size_loc
= pos
->offset
;
853 ret
= munmap_align(pos
->base_mma
);
855 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
860 if (pos
->packet_index
)
861 (void) g_array_free(pos
->packet_index
, TRUE
);
865 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
866 struct packet_index
*prev_index
,
867 struct packet_index
*cur_index
)
869 uint64_t events_discarded_diff
;
870 uint64_t packets_lost_diff
= 0;
872 /* Update packet index time information */
874 /* Current packet begin/end */
875 stream
->current
.real
.begin
=
876 cur_index
->ts_real
.timestamp_begin
;
877 stream
->current
.cycles
.begin
=
878 cur_index
->ts_cycles
.timestamp_begin
;
879 stream
->current
.real
.end
=
880 cur_index
->ts_real
.timestamp_end
;
881 stream
->current
.cycles
.end
=
882 cur_index
->ts_cycles
.timestamp_end
;
884 /* Update packet index discarded event information */
885 events_discarded_diff
= cur_index
->events_discarded
;
887 /* Previous packet begin/end */
888 stream
->prev
.cycles
.begin
=
889 prev_index
->ts_cycles
.timestamp_begin
;
890 stream
->prev
.real
.begin
=
891 prev_index
->ts_real
.timestamp_begin
;
892 stream
->prev
.cycles
.end
=
893 prev_index
->ts_cycles
.timestamp_end
;
894 stream
->prev
.real
.end
=
895 prev_index
->ts_real
.timestamp_end
;
897 events_discarded_diff
-= prev_index
->events_discarded
;
898 /* packet_seq_num stays at 0 if not produced by the tracer */
899 if (cur_index
->packet_seq_num
) {
900 packets_lost_diff
= cur_index
->packet_seq_num
-
901 prev_index
->packet_seq_num
- 1;
904 * Deal with 32-bit wrap-around if the tracer provided a
907 if (prev_index
->events_discarded_len
== 32) {
908 events_discarded_diff
= (uint32_t) events_discarded_diff
;
912 * First packet: use current packet info as limits for
915 stream
->prev
.cycles
.begin
=
916 stream
->prev
.cycles
.end
=
917 stream
->current
.cycles
.begin
;
918 stream
->prev
.real
.begin
=
919 stream
->prev
.real
.end
=
920 stream
->current
.real
.begin
;
922 stream
->events_discarded
= events_discarded_diff
;
923 stream
->packets_lost
= packets_lost_diff
;
927 * Find the timerange where all the streams in the trace are active
930 * Return 0 and update begin/end if necessary on success, return 1 for
931 * empty streams and return a negative value on error.
934 int ctf_intersect_trace(struct bt_trace_descriptor
*td_read
,
935 uint64_t *begin
, uint64_t *end
)
937 struct ctf_trace
*tin
;
938 int stream_id
, ret
= 0;
940 tin
= container_of(td_read
, struct ctf_trace
, parent
);
942 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
945 struct ctf_stream_declaration
*stream_class
;
947 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
951 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
952 struct ctf_file_stream
*file_stream
;
953 struct ctf_stream_pos
*stream_pos
;
954 struct packet_index
*index
;
956 file_stream
= g_ptr_array_index(stream_class
->streams
,
961 stream_pos
= &file_stream
->pos
;
962 if (!stream_pos
->packet_index
||
963 stream_pos
->packet_index
->len
<= 0) {
967 index
= &g_array_index(stream_pos
->packet_index
,
968 struct packet_index
, 0);
969 if (index
->ts_real
.timestamp_begin
> *begin
) {
970 *begin
= index
->ts_real
.timestamp_begin
;
972 index
= &g_array_index(stream_pos
->packet_index
,
974 stream_pos
->packet_index
->len
- 1);
975 if (index
->ts_real
.timestamp_end
< *end
) {
976 *end
= index
->ts_real
.timestamp_end
;
986 * Find the timerange where all streams in the trace collection are active
989 * Return 0 on success.
990 * Return 1 if no intersections are found.
991 * Return a negative value on error.
993 int ctf_find_packets_intersection(struct bt_context
*ctx
,
994 uint64_t *ts_begin
, uint64_t *ts_end
)
998 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
1003 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
1004 struct bt_trace_descriptor
*td_read
;
1006 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
1010 ret
= ctf_intersect_trace(td_read
, ts_begin
, ts_end
);
1015 if (*ts_end
< *ts_begin
) {
1025 * for SEEK_CUR: go to next packet.
1026 * for SEEK_SET: go to packet numer (index).
1028 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
1030 struct ctf_stream_pos
*pos
=
1031 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
1032 struct ctf_file_stream
*file_stream
=
1033 container_of(pos
, struct ctf_file_stream
, pos
);
1035 struct packet_index
*packet_index
, *prev_index
;
1039 case SEEK_SET
: /* Fall-through */
1045 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
1046 *pos
->content_size_loc
= pos
->offset
;
1048 if (pos
->base_mma
) {
1049 /* unmap old base */
1050 ret
= munmap_align(pos
->base_mma
);
1052 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1056 pos
->base_mma
= NULL
;
1060 * The caller should never ask for ctf_move_pos across packets,
1061 * except to get exactly at the beginning of the next packet.
1063 if (pos
->prot
& PROT_WRITE
) {
1066 /* The writer will add padding */
1067 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1070 assert(index
== 0); /* only seek supported for now */
1076 pos
->content_size
= -1U; /* Unknown at this point */
1077 pos
->packet_size
= WRITE_PACKET_LEN
;
1079 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1080 pos
->packet_size
/ CHAR_BIT
);
1081 } while (ret
== EINTR
);
1089 if (pos
->offset
== EOF
) {
1092 assert(pos
->cur_index
< pos
->packet_index
->len
);
1093 /* The reader will expect us to skip padding */
1098 if (index
>= pos
->packet_index
->len
) {
1102 pos
->cur_index
= index
;
1108 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1113 packet_index
= &g_array_index(pos
->packet_index
,
1114 struct packet_index
, pos
->cur_index
);
1115 if (pos
->cur_index
> 0) {
1116 prev_index
= &g_array_index(pos
->packet_index
,
1117 struct packet_index
,
1118 pos
->cur_index
- 1);
1122 ctf_update_current_packet_index(&file_stream
->parent
,
1123 prev_index
, packet_index
);
1126 * We need to check if we are in trace read or called
1127 * from packet indexing. In this last case, the
1128 * collection is not there, so we cannot print the
1131 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1132 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1135 packet_index
= &g_array_index(pos
->packet_index
,
1136 struct packet_index
,
1138 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1140 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1142 /* Lookup context/packet size in index */
1143 if (packet_index
->data_offset
== -1) {
1144 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1149 pos
->content_size
= packet_index
->content_size
;
1150 pos
->packet_size
= packet_index
->packet_size
;
1151 pos
->mmap_offset
= packet_index
->offset
;
1152 pos
->data_offset
= packet_index
->data_offset
;
1153 if (pos
->data_offset
< packet_index
->content_size
) {
1154 pos
->offset
= 0; /* will read headers */
1155 } else if (pos
->data_offset
== packet_index
->content_size
) {
1157 pos
->offset
= packet_index
->data_offset
;
1159 goto read_next_packet
;
1165 /* map new base. Need mapping length from header. */
1166 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1167 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1168 if (pos
->base_mma
== MAP_FAILED
) {
1169 fprintf(stderr
, "[error] mmap error %s.\n",
1174 /* update trace_packet_header and stream_packet_context */
1175 if (!(pos
->prot
& PROT_WRITE
) &&
1176 file_stream
->parent
.trace_packet_header
) {
1177 /* Read packet header */
1178 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1181 if (!(pos
->prot
& PROT_WRITE
) &&
1182 file_stream
->parent
.stream_packet_context
) {
1183 /* Read packet context */
1184 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1190 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1196 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1200 if (magic
== TSDL_MAGIC
) {
1202 td
->byte_order
= BYTE_ORDER
;
1203 CTF_TRACE_SET_FIELD(td
, byte_order
);
1204 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1206 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1207 LITTLE_ENDIAN
: BIG_ENDIAN
;
1208 CTF_TRACE_SET_FIELD(td
, byte_order
);
1216 * Returns 0 on success, -1 on error.
1219 int check_version(unsigned int major
, unsigned int minor
)
1234 /* eventually return an error instead of warning */
1236 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1242 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1245 struct metadata_packet_header header
;
1246 size_t readlen
, writelen
, toread
;
1247 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1250 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1254 if (td
->byte_order
!= BYTE_ORDER
) {
1255 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1256 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1257 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1258 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1260 if (header
.checksum
)
1261 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1262 if (header
.compression_scheme
) {
1263 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1264 header
.compression_scheme
);
1267 if (header
.encryption_scheme
) {
1268 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1269 header
.encryption_scheme
);
1272 if (header
.checksum_scheme
) {
1273 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1274 header
.checksum_scheme
);
1277 if (check_version(header
.major
, header
.minor
) < 0)
1279 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1280 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1281 CTF_TRACE_SET_FIELD(td
, uuid
);
1283 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1287 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1290 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1293 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1298 if (babeltrace_debug
) {
1299 buf
[readlen
] = '\0';
1300 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1304 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1305 if (writelen
< readlen
) {
1315 ret
= 0; /* continue reading next packet */
1322 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1323 ret
= fseek(in
, toread
, SEEK_CUR
);
1325 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1332 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1336 size_t size
, buflen
;
1341 * Using strlen on *buf instead of size of open_memstream
1342 * because its size includes garbage at the end (after final
1343 * \0). This is the allocated size, not the actual string size.
1345 out
= babeltrace_open_memstream(buf
, &size
);
1347 perror("Metadata open_memstream");
1351 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1360 /* close to flush the buffer */
1361 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1365 perror("babeltrace_flush_memstream");
1367 closeret
= fclose(in
);
1369 perror("Error in fclose");
1375 perror("Error in fclose");
1377 /* open for reading */
1378 buflen
= strlen(*buf
);
1383 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1385 perror("Metadata fmemopen");
1392 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1393 struct ctf_scanner
*scanner
, int append
)
1395 struct ctf_file_stream
*metadata_stream
;
1398 int ret
= 0, closeret
;
1400 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1401 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1405 metadata_stream
->pos
.fd
= -1;
1407 td
->metadata
= &metadata_stream
->parent
;
1408 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1409 if (metadata_stream
->pos
.fd
< 0) {
1410 fprintf(stderr
, "Unable to open metadata.\n");
1415 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1417 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1418 perror("Metadata stream open");
1422 /* fd now belongs to fp */
1423 metadata_stream
->pos
.fd
= -1;
1425 if (babeltrace_debug
)
1428 if (packet_metadata(td
, fp
)) {
1429 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1433 td
->metadata_string
= buf
;
1434 td
->metadata_packetized
= 1;
1437 unsigned int major
, minor
;
1440 td
->byte_order
= BYTE_ORDER
;
1442 /* Check text-only metadata header and version */
1443 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1445 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1446 if (check_version(major
, minor
) < 0) {
1454 ret
= ctf_scanner_append_ast(scanner
, fp
);
1456 fprintf(stderr
, "[error] Error creating AST\n");
1460 if (babeltrace_debug
) {
1461 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1463 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1468 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1470 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1473 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1474 td
, td
->byte_order
);
1476 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1481 closeret
= fclose(fp
);
1483 perror("Error on fclose");
1487 if (metadata_stream
->pos
.fd
>= 0) {
1488 closeret
= close(metadata_stream
->pos
.fd
);
1490 perror("Error on metadata stream fd close");
1495 g_free(metadata_stream
);
1500 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1501 struct ctf_stream_definition
*stream
,
1502 struct ctf_event_declaration
*event
)
1504 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1506 if (event
->context_decl
) {
1507 struct bt_definition
*definition
=
1508 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1509 stream
->parent_def_scope
, 0, 0, "event.context");
1513 stream_event
->event_context
= container_of(definition
,
1514 struct definition_struct
, p
);
1515 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1517 if (event
->fields_decl
) {
1518 struct bt_definition
*definition
=
1519 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1520 stream
->parent_def_scope
, 0, 0, "event.fields");
1524 stream_event
->event_fields
= container_of(definition
,
1525 struct definition_struct
, p
);
1526 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1528 stream_event
->stream
= stream
;
1529 return stream_event
;
1532 if (stream_event
->event_fields
)
1533 bt_definition_unref(&stream_event
->event_fields
->p
);
1534 if (stream_event
->event_context
)
1535 bt_definition_unref(&stream_event
->event_context
->p
);
1536 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1537 g_quark_to_string(event
->name
));
1542 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1543 struct ctf_stream_declaration
*stream_class
,
1544 struct ctf_stream_definition
*stream
)
1546 size_t def_size
, class_size
, i
;
1549 def_size
= stream
->events_by_id
->len
;
1550 class_size
= stream_class
->events_by_id
->len
;
1552 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1553 for (i
= def_size
; i
< class_size
; i
++) {
1554 struct ctf_event_declaration
*event
=
1555 g_ptr_array_index(stream_class
->events_by_id
, i
);
1556 struct ctf_event_definition
*stream_event
;
1560 stream_event
= create_event_definitions(td
, stream
, event
);
1561 if (!stream_event
) {
1565 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1572 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1574 struct ctf_stream_declaration
*stream_class
;
1578 if (stream
->stream_definitions_created
)
1581 stream_class
= stream
->stream_class
;
1583 if (stream_class
->packet_context_decl
) {
1584 struct bt_definition
*definition
=
1585 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1586 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1591 stream
->stream_packet_context
= container_of(definition
,
1592 struct definition_struct
, p
);
1593 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1595 if (stream_class
->event_header_decl
) {
1596 struct bt_definition
*definition
=
1597 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1598 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1603 stream
->stream_event_header
=
1604 container_of(definition
, struct definition_struct
, p
);
1605 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1607 if (stream_class
->event_context_decl
) {
1608 struct bt_definition
*definition
=
1609 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1610 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1615 stream
->stream_event_context
=
1616 container_of(definition
, struct definition_struct
, p
);
1617 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1619 stream
->events_by_id
= g_ptr_array_new();
1620 ret
= copy_event_declarations_stream_class_to_stream(td
,
1621 stream_class
, stream
);
1627 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1628 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1630 g_free(stream_event
);
1632 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1634 if (stream
->stream_event_context
)
1635 bt_definition_unref(&stream
->stream_event_context
->p
);
1636 if (stream
->stream_event_header
)
1637 bt_definition_unref(&stream
->stream_event_header
->p
);
1638 if (stream
->stream_packet_context
)
1639 bt_definition_unref(&stream
->stream_packet_context
->p
);
1640 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1641 stream_class
->stream_id
, strerror(-ret
));
1646 int stream_assign_class(struct ctf_trace
*td
,
1647 struct ctf_file_stream
*file_stream
,
1650 struct ctf_stream_declaration
*stream
;
1653 file_stream
->parent
.stream_id
= stream_id
;
1654 if (stream_id
>= td
->streams
->len
) {
1655 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1658 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1660 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1663 file_stream
->parent
.stream_class
= stream
;
1664 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1671 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1672 struct ctf_trace
*td
,
1673 struct ctf_file_stream
*file_stream
,
1676 struct packet_index packet_index
;
1677 uint64_t stream_id
= 0;
1678 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1679 int first_packet
= 0;
1684 memset(&packet_index
, 0, sizeof(packet_index
));
1685 if (!pos
->mmap_offset
) {
1689 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1690 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1693 if (pos
->base_mma
) {
1694 /* unmap old base */
1695 ret
= munmap_align(pos
->base_mma
);
1697 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1701 pos
->base_mma
= NULL
;
1703 /* map new base. Need mapping length from header. */
1704 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1705 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1706 assert(pos
->base_mma
!= MAP_FAILED
);
1708 * Use current mapping size as temporary content and packet
1711 pos
->content_size
= packet_map_len
;
1712 pos
->packet_size
= packet_map_len
;
1713 pos
->offset
= 0; /* Position of the packet header */
1715 packet_index
.offset
= pos
->mmap_offset
;
1717 /* read and check header, set stream id (and check) */
1718 if (file_stream
->parent
.trace_packet_header
) {
1719 /* Read packet header */
1720 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1724 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1727 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1728 if (len_index
>= 0) {
1729 struct bt_definition
*field
;
1732 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1733 magic
= bt_get_unsigned_int(field
);
1734 if (magic
!= CTF_MAGIC
) {
1735 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1737 file_stream
->pos
.packet_index
->len
,
1738 (ssize_t
) pos
->mmap_offset
);
1744 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1745 if (len_index
>= 0) {
1746 struct definition_array
*defarray
;
1747 struct bt_definition
*field
;
1749 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1751 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1752 assert(field
->declaration
->id
== BT_CTF_TYPE_ID_ARRAY
);
1753 defarray
= container_of(field
, struct definition_array
, p
);
1754 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1756 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1757 struct bt_definition
*elem
;
1759 elem
= bt_array_index(defarray
, i
);
1760 uuidval
[i
] = bt_get_unsigned_int(elem
);
1762 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1764 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1769 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1770 if (len_index
>= 0) {
1771 struct bt_definition
*field
;
1773 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1774 stream_id
= bt_get_unsigned_int(field
);
1778 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1779 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1781 file_stream
->parent
.stream_id
);
1785 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1790 if (file_stream
->parent
.stream_packet_context
) {
1791 /* Read packet context */
1792 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1796 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1799 /* read packet size from header */
1800 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1801 if (len_index
>= 0) {
1802 struct bt_definition
*field
;
1804 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1805 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1807 /* Use file size for packet size */
1808 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1811 /* read content size from header */
1812 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1813 if (len_index
>= 0) {
1814 struct bt_definition
*field
;
1816 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1817 packet_index
.content_size
= bt_get_unsigned_int(field
);
1819 /* Use packet size if non-zero, else file size */
1820 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1823 /* read timestamp begin from header */
1824 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1825 if (len_index
>= 0) {
1826 struct bt_definition
*field
;
1828 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1829 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1830 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1831 packet_index
.ts_real
.timestamp_begin
=
1832 ctf_get_real_timestamp(
1833 &file_stream
->parent
,
1834 packet_index
.ts_cycles
.timestamp_begin
);
1838 /* read timestamp end from header */
1839 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1840 if (len_index
>= 0) {
1841 struct bt_definition
*field
;
1843 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1844 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1845 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1846 packet_index
.ts_real
.timestamp_end
=
1847 ctf_get_real_timestamp(
1848 &file_stream
->parent
,
1849 packet_index
.ts_cycles
.timestamp_end
);
1853 /* read events discarded from header */
1854 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1855 if (len_index
>= 0) {
1856 struct bt_definition
*field
;
1858 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1859 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1860 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1863 /* read packet_seq_num from header */
1864 len_index
= bt_struct_declaration_lookup_field_index(
1865 file_stream
->parent
.stream_packet_context
->declaration
,
1866 g_quark_from_static_string("packet_seq_num"));
1867 if (len_index
>= 0) {
1868 struct bt_definition
*field
;
1870 field
= bt_struct_definition_get_field_from_index(
1871 file_stream
->parent
.stream_packet_context
,
1873 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1876 /* Use file size for packet size */
1877 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1878 /* Use packet size if non-zero, else file size */
1879 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1882 /* Validate content size and packet size values */
1883 if (packet_index
.content_size
> packet_index
.packet_size
) {
1884 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1885 packet_index
.content_size
, packet_index
.packet_size
);
1889 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1890 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1891 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1895 if (packet_index
.content_size
< pos
->offset
) {
1896 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1900 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1901 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1905 /* Save position after header and context */
1906 packet_index
.data_offset
= pos
->offset
;
1908 /* add index to packet array */
1909 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1911 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1915 /* Retry with larger mapping */
1917 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1919 * Reached EOF, but still expecting header/context data.
1921 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1924 /* Double the mapping len, and retry */
1925 tmp_map_len
= packet_map_len
<< 1;
1926 if (tmp_map_len
>> 1 != packet_map_len
) {
1928 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1931 packet_map_len
= tmp_map_len
;
1936 int create_stream_packet_index(struct ctf_trace
*td
,
1937 struct ctf_file_stream
*file_stream
)
1939 struct ctf_stream_pos
*pos
;
1940 struct stat filestats
;
1943 pos
= &file_stream
->pos
;
1945 ret
= fstat(pos
->fd
, &filestats
);
1949 /* Deal with empty files */
1950 if (!filestats
.st_size
) {
1951 if (file_stream
->parent
.trace_packet_header
1952 || file_stream
->parent
.stream_packet_context
) {
1954 * We expect a trace packet header and/or stream packet
1955 * context. Since a trace needs to have at least one
1956 * packet, empty files are therefore not accepted.
1958 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1962 * Without trace packet header nor stream packet
1963 * context, a one-packet trace can indeed be empty. This
1964 * is only valid if there is only one stream class: 0.
1966 ret
= stream_assign_class(td
, file_stream
, 0);
1973 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1974 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1983 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1987 if (td
->packet_header_decl
) {
1988 struct bt_definition
*definition
=
1989 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1990 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1995 stream
->trace_packet_header
=
1996 container_of(definition
, struct definition_struct
, p
);
1997 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
2003 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
2008 int import_stream_packet_index(struct ctf_trace
*td
,
2009 struct ctf_file_stream
*file_stream
)
2011 struct ctf_stream_pos
*pos
;
2012 struct ctf_packet_index
*ctf_index
= NULL
;
2013 struct ctf_packet_index_file_hdr index_hdr
;
2014 struct packet_index index
;
2015 uint32_t packet_index_len
, index_minor
;
2017 int first_packet
= 1;
2020 pos
= &file_stream
->pos
;
2022 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
2024 perror("read index file header");
2028 /* Check the index header */
2029 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
2030 fprintf(stderr
, "[error] wrong index magic\n");
2034 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
2035 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
2036 ".%" PRIu32
", supported %d.%d\n",
2037 be32toh(index_hdr
.index_major
),
2038 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
2043 index_minor
= be32toh(index_hdr
.index_minor
);
2045 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
2046 if (packet_index_len
== 0) {
2047 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2052 * Allocate the index length found in header, not internal
2055 ctf_index
= g_malloc0(packet_index_len
);
2056 while (fread(ctf_index
, packet_index_len
, 1,
2057 pos
->index_fp
) == 1) {
2059 struct ctf_stream_declaration
*stream
= NULL
;
2061 memset(&index
, 0, sizeof(index
));
2062 index
.offset
= be64toh(ctf_index
->offset
);
2063 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2064 index
.content_size
= be64toh(ctf_index
->content_size
);
2065 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2066 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2067 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2068 index
.events_discarded_len
= 64;
2069 index
.data_offset
= -1;
2070 stream_id
= be64toh(ctf_index
->stream_id
);
2071 if (index_minor
>= 1) {
2072 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2073 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2076 if (!first_packet
) {
2077 /* add index to packet array */
2078 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2082 file_stream
->parent
.stream_id
= stream_id
;
2083 if (stream_id
< td
->streams
->len
) {
2084 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2087 fprintf(stderr
, "[error] Stream %" PRIu64
2088 " is not declared in metadata.\n",
2093 file_stream
->parent
.stream_class
= stream
;
2094 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2098 /* add index to packet array */
2099 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2102 /* Index containing only the header. */
2103 if (!file_stream
->parent
.stream_class
) {
2116 * Note: many file streams can inherit from the same stream class
2117 * description (metadata).
2120 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2121 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2124 int ret
, fd
, closeret
;
2125 struct ctf_file_stream
*file_stream
;
2126 struct stat statbuf
;
2129 fd
= openat(td
->dirfd
, path
, flags
);
2131 perror("File stream openat()");
2136 /* Don't try to mmap subdirectories. Skip them, return success. */
2137 ret
= fstat(fd
, &statbuf
);
2139 perror("File stream fstat()");
2142 if (S_ISDIR(statbuf
.st_mode
)) {
2143 if (strncmp(path
, "index", 5) != 0) {
2144 fprintf(stderr
, "[warning] Skipping directory '%s' "
2145 "found in trace\n", path
);
2150 if (!statbuf
.st_size
) {
2151 /** Skip empty files. */
2153 goto fd_is_empty_file
;
2156 file_stream
= g_new0(struct ctf_file_stream
, 1);
2157 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2158 file_stream
->pos
.fd
= -1;
2159 file_stream
->pos
.index_fp
= NULL
;
2161 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2162 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2165 file_stream
->pos
.packet_seek
= packet_seek
;
2167 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2172 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2175 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2179 * For now, only a single clock per trace is supported.
2181 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2184 * Allocate the index name for this stream and try to open it.
2186 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2188 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2192 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2195 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2196 ret
= create_stream_packet_index(td
, file_stream
);
2198 fprintf(stderr
, "[error] Stream index creation error.\n");
2202 ret
= openat(td
->dirfd
, index_name
, flags
);
2204 perror("Index file openat()");
2208 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2209 if (!file_stream
->pos
.index_fp
) {
2210 perror("fdopen() error");
2213 ret
= import_stream_packet_index(td
, file_stream
);
2218 ret
= fclose(file_stream
->pos
.index_fp
);
2220 perror("close index");
2226 /* Add stream file to stream class */
2227 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2228 &file_stream
->parent
);
2232 if (file_stream
->pos
.index_fp
) {
2233 ret
= fclose(file_stream
->pos
.index_fp
);
2235 perror("close index");
2238 if (file_stream
->parent
.trace_packet_header
)
2239 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2243 closeret
= ctf_fini_pos(&file_stream
->pos
);
2245 fprintf(stderr
, "Error on ctf_fini_pos\n");
2247 g_free(file_stream
);
2251 closeret
= close(fd
);
2253 perror("Error on fd close");
2260 int ctf_open_trace_read(struct ctf_trace
*td
,
2261 const char *path
, int flags
,
2262 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2263 int whence
), FILE *metadata_fp
)
2265 struct ctf_scanner
*scanner
;
2267 struct dirent
*dirent
;
2268 struct dirent
*diriter
;
2275 /* Open trace directory */
2276 td
->dir
= opendir(path
);
2278 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2283 td
->dirfd
= open(path
, 0);
2284 if (td
->dirfd
< 0) {
2285 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2286 perror("Trace directory open");
2290 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2291 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2294 * Keep the metadata file separate.
2295 * Keep scanner object local to the open. We don't support
2296 * incremental metadata append for on-disk traces.
2298 scanner
= ctf_scanner_alloc();
2300 fprintf(stderr
, "[error] Error allocating scanner\n");
2302 goto error_metadata
;
2304 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2305 ctf_scanner_free(scanner
);
2307 if (ret
== -ENOENT
) {
2308 fprintf(stderr
, "[warning] Empty metadata.\n");
2310 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2311 goto error_metadata
;
2315 * Open each stream: for each file, try to open, check magic
2316 * number, and get the stream ID to add to the right location in
2320 pc_name_max
= fpathconf(td
->dirfd
, _PC_NAME_MAX
);
2321 if (pc_name_max
< 0) {
2322 perror("Error on fpathconf");
2323 fprintf(stderr
, "[error] Failed to get _PC_NAME_MAX for path \"%s\".\n", path
);
2325 goto error_metadata
;
2328 dirent_len
= offsetof(struct dirent
, d_name
) + pc_name_max
+ 1;
2330 dirent
= malloc(dirent_len
);
2333 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2335 fprintf(stderr
, "[error] Readdir error.\n");
2340 /* Ignore hidden files, ., .. and metadata. */
2341 if (!strncmp(diriter
->d_name
, ".", 1)
2342 || !strcmp(diriter
->d_name
, "..")
2343 || !strcmp(diriter
->d_name
, "metadata"))
2346 /* Ignore index files : *.idx */
2347 ext
= strrchr(diriter
->d_name
, '.');
2348 if (ext
&& (!strcmp(ext
, ".idx"))) {
2352 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2353 flags
, packet_seek
);
2355 fprintf(stderr
, "[error] Open file stream error.\n");
2366 closeret
= close(td
->dirfd
);
2368 perror("Error on fd close");
2371 closeret
= closedir(td
->dir
);
2373 perror("Error on closedir");
2380 * ctf_open_trace: Open a CTF trace and index it.
2381 * Note that the user must seek the trace after the open (using the iterator)
2382 * since the index creation read it entirely.
2385 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2386 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2387 int whence
), FILE *metadata_fp
)
2389 struct ctf_trace
*td
;
2393 * If packet_seek is NULL, we provide our default version.
2396 packet_seek
= ctf_packet_seek
;
2398 td
= g_new0(struct ctf_trace
, 1);
2400 switch (flags
& O_ACCMODE
) {
2403 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2406 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2411 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2414 fprintf(stderr
, "[error] Incorrect open flags.\n");
2418 ret
= trace_debug_info_create(td
);
2425 trace_debug_info_destroy(td
);
2431 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2432 struct bt_mmap_stream
*mmap_info
)
2434 pos
->mmap_offset
= 0;
2435 pos
->packet_size
= 0;
2436 pos
->content_size
= 0;
2437 pos
->content_size_loc
= NULL
;
2438 pos
->fd
= mmap_info
->fd
;
2439 pos
->base_mma
= NULL
;
2443 pos
->prot
= PROT_READ
;
2444 pos
->flags
= MAP_PRIVATE
;
2445 pos
->parent
.rw_table
= read_dispatch_table
;
2446 pos
->parent
.event_cb
= ctf_read_event
;
2447 pos
->priv
= mmap_info
->priv
;
2448 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2449 sizeof(struct packet_index
));
2453 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2454 struct ctf_file_stream
*file_stream
,
2455 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2458 struct ctf_stream_declaration
*stream
;
2462 /* Ask for the first packet to get the stream_id. */
2463 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2464 stream_id
= file_stream
->parent
.stream_id
;
2465 if (stream_id
>= td
->streams
->len
) {
2466 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2467 "in metadata.\n", stream_id
);
2471 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2473 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2474 "in metadata.\n", stream_id
);
2478 file_stream
->parent
.stream_class
= stream
;
2479 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2485 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2486 struct bt_mmap_stream
*mmap_info
,
2487 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2491 struct ctf_file_stream
*file_stream
;
2493 file_stream
= g_new0(struct ctf_file_stream
, 1);
2494 file_stream
->parent
.stream_id
= -1ULL;
2495 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2496 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2498 file_stream
->pos
.packet_seek
= packet_seek
;
2500 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2505 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2510 * For now, only a single clock per trace is supported.
2512 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2514 /* Add stream file to stream class */
2515 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2516 &file_stream
->parent
);
2520 if (file_stream
->parent
.trace_packet_header
)
2521 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2523 g_free(file_stream
);
2528 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2529 struct bt_mmap_stream_list
*mmap_list
,
2530 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2535 struct bt_mmap_stream
*mmap_info
;
2537 td
->scanner
= ctf_scanner_alloc();
2539 fprintf(stderr
, "[error] Error allocating scanner\n");
2543 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2545 if (ret
== -ENOENT
) {
2546 fprintf(stderr
, "[warning] Empty metadata.\n");
2552 * for each stream, try to open, check magic number, and get the
2553 * stream ID to add to the right location in the stream array.
2555 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2556 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2558 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2565 ctf_scanner_free(td
->scanner
);
2570 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2571 struct bt_mmap_stream_list
*mmap_list
,
2572 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2576 struct ctf_trace
*td
;
2580 fprintf(stderr
, "[error] No metadata file pointer associated, "
2581 "required for mmap parsing\n");
2585 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2588 td
= g_new0(struct ctf_trace
, 1);
2590 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2594 ret
= trace_debug_info_create(td
);
2607 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2610 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2616 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2619 /* for each stream_class */
2620 for (i
= 0; i
< td
->streams
->len
; i
++) {
2621 struct ctf_stream_declaration
*stream_class
;
2623 stream_class
= g_ptr_array_index(td
->streams
, i
);
2626 /* for each stream */
2627 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2628 struct ctf_stream_definition
*stream
;
2630 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2633 ret
= copy_event_declarations_stream_class_to_stream(td
,
2634 stream_class
, stream
);
2643 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2646 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2648 /* for each stream_class */
2649 for (i
= 0; i
< td
->streams
->len
; i
++) {
2650 struct ctf_stream_declaration
*stream_class
;
2652 stream_class
= g_ptr_array_index(td
->streams
, i
);
2655 /* for each file_stream */
2656 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2657 struct ctf_stream_definition
*stream
;
2658 struct ctf_stream_pos
*stream_pos
;
2659 struct ctf_file_stream
*cfs
;
2661 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2664 cfs
= container_of(stream
, struct ctf_file_stream
,
2666 stream_pos
= &cfs
->pos
;
2667 if (!stream_pos
->packet_index
)
2670 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2671 struct packet_index
*index
;
2673 index
= &g_array_index(stream_pos
->packet_index
,
2674 struct packet_index
, k
);
2675 index
->ts_real
.timestamp_begin
=
2676 ctf_get_real_timestamp(stream
,
2677 index
->ts_cycles
.timestamp_begin
);
2678 index
->ts_real
.timestamp_end
=
2679 ctf_get_real_timestamp(stream
,
2680 index
->ts_cycles
.timestamp_end
);
2688 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2692 ret
= ctf_fini_pos(&file_stream
->pos
);
2694 fprintf(stderr
, "Error on ctf_fini_pos\n");
2697 if (file_stream
->pos
.fd
>= 0) {
2698 ret
= close(file_stream
->pos
.fd
);
2700 perror("Error closing file fd");
2708 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2710 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2716 for (i
= 0; i
< td
->streams
->len
; i
++) {
2717 struct ctf_stream_declaration
*stream
;
2720 stream
= g_ptr_array_index(td
->streams
, i
);
2723 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2724 struct ctf_file_stream
*file_stream
;
2725 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2726 struct ctf_file_stream
, parent
);
2727 ret
= ctf_close_file_stream(file_stream
);
2733 ctf_destroy_metadata(td
);
2734 ctf_scanner_free(td
->scanner
);
2735 if (td
->dirfd
>= 0) {
2736 ret
= close(td
->dirfd
);
2738 perror("Error closing dirfd");
2743 ret
= closedir(td
->dir
);
2745 perror("Error closedir");
2749 free(td
->metadata_string
);
2750 trace_debug_info_destroy(td
);
2756 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2757 struct bt_context
*ctx
)
2759 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2762 td
->parent
.ctx
= ctx
;
2766 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2767 struct bt_trace_handle
*handle
)
2769 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2772 td
->parent
.handle
= handle
;
2776 void __attribute__((constructor
)) ctf_init(void)
2780 ctf_format
.name
= g_quark_from_static_string("ctf");
2781 ret
= bt_register_format(&ctf_format
);
2786 void __attribute__((destructor
)) ctf_exit(void)
2788 bt_unregister_format(&ctf_format
);