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 1000000000ULL
77 #define INDEX_PATH "./index/%s.idx"
84 uint64_t opt_clock_offset
;
85 uint64_t opt_clock_offset_ns
;
88 char *opt_debug_info_dir
;
89 char *opt_debug_info_target_prefix
;
92 * TODO: babeltrace_ctf_console_output ensures that we only print
93 * discarded events when ctf-text plugin is used. Should be cleaned up
94 * with the plugin system redesign.
96 int babeltrace_ctf_console_output
;
99 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
100 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
104 struct bt_trace_descriptor
*ctf_open_mmap_trace(
105 struct bt_mmap_stream_list
*mmap_list
,
106 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
110 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
111 struct bt_context
*ctx
);
113 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
114 struct bt_trace_handle
*handle
);
117 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
119 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
120 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
122 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
123 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
125 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
128 rw_dispatch read_dispatch_table
[] = {
129 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
130 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
131 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
132 [ CTF_TYPE_STRING
] = ctf_string_read
,
133 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
134 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
135 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
136 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
140 rw_dispatch write_dispatch_table
[] = {
141 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
142 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
143 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
144 [ CTF_TYPE_STRING
] = ctf_string_write
,
145 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
146 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
147 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
148 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
152 struct bt_format ctf_format
= {
153 .open_trace
= ctf_open_trace
,
154 .open_mmap_trace
= ctf_open_mmap_trace
,
155 .close_trace
= ctf_close_trace
,
156 .set_context
= ctf_set_context
,
157 .set_handle
= ctf_set_handle
,
158 .timestamp_begin
= ctf_timestamp_begin
,
159 .timestamp_end
= ctf_timestamp_end
,
160 .convert_index_timestamp
= ctf_convert_index_timestamp
,
164 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
165 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
167 struct ctf_trace
*tin
;
168 uint64_t begin
= ULLONG_MAX
;
171 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
176 /* for each stream_class */
177 for (i
= 0; i
< tin
->streams
->len
; i
++) {
178 struct ctf_stream_declaration
*stream_class
;
180 stream_class
= g_ptr_array_index(tin
->streams
, i
);
183 /* for each file_stream */
184 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
185 struct ctf_stream_definition
*stream
;
186 struct ctf_file_stream
*cfs
;
187 struct ctf_stream_pos
*stream_pos
;
188 struct packet_index
*index
;
190 stream
= g_ptr_array_index(stream_class
->streams
, j
);
191 cfs
= container_of(stream
, struct ctf_file_stream
,
193 stream_pos
= &cfs
->pos
;
195 if (!stream_pos
->packet_index
)
198 if (stream_pos
->packet_index
->len
<= 0)
201 index
= &g_array_index(stream_pos
->packet_index
,
203 stream_pos
->packet_index
->len
- 1);
204 if (type
== BT_CLOCK_REAL
) {
205 if (index
->ts_real
.timestamp_begin
< begin
)
206 begin
= index
->ts_real
.timestamp_begin
;
207 } else if (type
== BT_CLOCK_CYCLES
) {
208 if (index
->ts_cycles
.timestamp_begin
< begin
)
209 begin
= index
->ts_cycles
.timestamp_begin
;
223 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
224 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
226 struct ctf_trace
*tin
;
230 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
235 /* for each stream_class */
236 for (i
= 0; i
< tin
->streams
->len
; i
++) {
237 struct ctf_stream_declaration
*stream_class
;
239 stream_class
= g_ptr_array_index(tin
->streams
, i
);
242 /* for each file_stream */
243 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
244 struct ctf_stream_definition
*stream
;
245 struct ctf_file_stream
*cfs
;
246 struct ctf_stream_pos
*stream_pos
;
247 struct packet_index
*index
;
249 stream
= g_ptr_array_index(stream_class
->streams
, j
);
250 cfs
= container_of(stream
, struct ctf_file_stream
,
252 stream_pos
= &cfs
->pos
;
254 if (!stream_pos
->packet_index
)
257 if (stream_pos
->packet_index
->len
<= 0)
260 index
= &g_array_index(stream_pos
->packet_index
,
262 stream_pos
->packet_index
->len
- 1);
263 if (type
== BT_CLOCK_REAL
) {
264 if (index
->ts_real
.timestamp_end
> end
)
265 end
= index
->ts_real
.timestamp_end
;
266 } else if (type
== BT_CLOCK_CYCLES
) {
267 if (index
->ts_cycles
.timestamp_end
> end
)
268 end
= index
->ts_cycles
.timestamp_end
;
282 * Update stream current timestamp
285 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
286 struct definition_integer
*integer_definition
)
288 struct declaration_integer
*integer_declaration
=
289 integer_definition
->declaration
;
290 uint64_t oldval
, newval
, updateval
;
292 if (unlikely(integer_declaration
->len
== 64)) {
293 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
294 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
295 stream
->cycles_timestamp
);
299 oldval
= stream
->cycles_timestamp
;
300 oldval
&= (1ULL << integer_declaration
->len
) - 1;
301 newval
= integer_definition
->value
._unsigned
;
302 /* Test for overflow by comparing low bits */
304 newval
+= 1ULL << integer_declaration
->len
;
305 /* updateval contains old high bits, and new low bits (sum) */
306 updateval
= stream
->cycles_timestamp
;
307 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
309 stream
->cycles_timestamp
= updateval
;
311 /* convert to real timestamp */
312 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
313 stream
->cycles_timestamp
);
317 * Print timestamp, rescaling clock frequency to nanoseconds and
318 * applying offsets as needed (unix time).
321 void ctf_print_timestamp_real(FILE *fp
,
322 struct ctf_stream_definition
*stream
,
325 uint64_t ts_sec
= 0, ts_nsec
;
329 /* Add command-line offset in ns*/
330 ts_nsec
+= opt_clock_offset_ns
;
332 /* Add command-line offset */
333 ts_sec
+= opt_clock_offset
;
335 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
336 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
338 if (!opt_clock_seconds
) {
340 time_t time_s
= (time_t) ts_sec
;
342 if (!opt_clock_gmt
) {
345 res
= localtime_r(&time_s
, &tm
);
347 fprintf(stderr
, "[warning] Unable to get localtime.\n");
353 res
= gmtime_r(&time_s
, &tm
);
355 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
359 if (opt_clock_date
) {
363 /* Print date and time */
364 res
= strftime(timestr
, sizeof(timestr
),
367 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
370 fprintf(fp
, "%s", timestr
);
372 /* Print time in HH:MM:SS.ns */
373 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
374 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
378 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
386 * Print timestamp, in cycles
389 void ctf_print_timestamp_cycles(FILE *fp
,
390 struct ctf_stream_definition
*stream
,
393 fprintf(fp
, "%020" PRIu64
, timestamp
);
396 void ctf_print_timestamp(FILE *fp
,
397 struct ctf_stream_definition
*stream
,
400 if (opt_clock_cycles
) {
401 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
403 ctf_print_timestamp_real(fp
, stream
, timestamp
);
408 void print_uuid(FILE *fp
, unsigned char *uuid
)
412 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
413 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
417 * Discarded events can be either:
418 * - discarded after end of previous buffer due to buffer full:
419 * happened within range: [ prev_timestamp_end, timestamp_begin ]
420 * - discarded within current buffer due to either event too large or
421 * nested wrap-around:
422 * happened within range: [ timestamp_begin, timestamp_end ]
424 * Given we have discarded counters of those two types merged into the
425 * events_discarded counter, we need to use the union of those ranges:
426 * [ prev_timestamp_end, timestamp_end ]
428 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
429 * consumer had time to extract them. We keep track of those gaps with the
430 * packet sequence number in each packet.
433 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
435 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
436 !babeltrace_ctf_console_output
) {
440 if (stream
->events_discarded
) {
441 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
442 stream
->events_discarded
);
443 } else if (stream
->packets_lost
) {
444 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
445 stream
->packets_lost
);
447 if (opt_clock_cycles
) {
448 ctf_print_timestamp(fp
, stream
,
449 stream
->prev
.cycles
.end
);
450 fprintf(fp
, "] and [");
451 ctf_print_timestamp(fp
, stream
,
452 stream
->current
.cycles
.end
);
454 ctf_print_timestamp(fp
, stream
,
455 stream
->prev
.real
.end
);
456 fprintf(fp
, "] and [");
457 ctf_print_timestamp(fp
, stream
,
458 stream
->current
.real
.end
);
460 fprintf(fp
, "] in trace UUID ");
461 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
462 if (stream
->stream_class
->trace
->parent
.path
[0])
463 fprintf(fp
, ", at path: \"%s\"",
464 stream
->stream_class
->trace
->parent
.path
);
466 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
468 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
470 fprintf(fp
, "You should consider recording a new trace with larger "
471 "buffers or with fewer events enabled.\n");
476 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
478 struct ctf_stream_pos
*pos
=
479 container_of(ppos
, struct ctf_stream_pos
, parent
);
480 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
481 struct ctf_event_definition
*event
;
485 /* We need to check for EOF here for empty files. */
486 if (unlikely(pos
->offset
== EOF
))
489 ctf_pos_get_event(pos
);
491 /* save the current position as a restore point */
492 pos
->last_offset
= pos
->offset
;
495 * This is the EOF check after we've advanced the position in
498 if (unlikely(pos
->offset
== EOF
))
501 /* Stream is inactive for now (live reading). */
502 if (unlikely(pos
->content_size
== 0))
506 * Packet seeked to by ctf_pos_get_event() only contains
507 * headers, no event. Consider stream as inactive (live
510 if (unlikely(pos
->data_offset
== pos
->content_size
))
513 assert(pos
->offset
< pos
->content_size
);
515 /* Read event header */
516 if (likely(stream
->stream_event_header
)) {
517 struct definition_integer
*integer_definition
;
518 struct bt_definition
*variant
;
520 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
523 /* lookup event id */
524 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
525 if (integer_definition
) {
526 id
= integer_definition
->value
._unsigned
;
528 struct definition_enum
*enum_definition
;
530 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
531 if (enum_definition
) {
532 id
= enum_definition
->integer
->value
._unsigned
;
536 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
538 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
539 if (integer_definition
) {
540 id
= integer_definition
->value
._unsigned
;
543 stream
->event_id
= id
;
545 /* lookup timestamp */
546 stream
->has_timestamp
= 0;
547 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
548 if (integer_definition
) {
549 ctf_update_timestamp(stream
, integer_definition
);
550 stream
->has_timestamp
= 1;
553 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
554 if (integer_definition
) {
555 ctf_update_timestamp(stream
, integer_definition
);
556 stream
->has_timestamp
= 1;
562 /* Read stream-declared event context */
563 if (stream
->stream_event_context
) {
564 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
569 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
570 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
573 event
= g_ptr_array_index(stream
->events_by_id
, id
);
574 if (unlikely(!event
)) {
575 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
579 /* Read event-declared event context */
580 if (event
->event_context
) {
581 ret
= generic_rw(ppos
, &event
->event_context
->p
);
586 /* Read event payload */
587 if (likely(event
->event_fields
)) {
588 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
593 if (pos
->last_offset
== pos
->offset
) {
594 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
601 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
606 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
608 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
609 struct ctf_event_definition
*event
;
613 id
= stream
->event_id
;
615 /* print event header */
616 if (likely(stream
->stream_event_header
)) {
617 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
622 /* print stream-declared event context */
623 if (stream
->stream_event_context
) {
624 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
629 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
630 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
633 event
= g_ptr_array_index(stream
->events_by_id
, id
);
634 if (unlikely(!event
)) {
635 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
639 /* print event-declared event context */
640 if (event
->event_context
) {
641 ret
= generic_rw(pos
, &event
->event_context
->p
);
646 /* Read and print event payload */
647 if (likely(event
->event_fields
)) {
648 ret
= generic_rw(pos
, &event
->event_fields
->p
);
656 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
661 * One side-effect of this function is to unmap pos mmap base if one is
665 int find_data_offset(struct ctf_stream_pos
*pos
,
666 struct ctf_file_stream
*file_stream
,
667 struct packet_index
*packet_index
)
669 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
670 struct stat filestats
;
674 pos
= &file_stream
->pos
;
676 ret
= fstat(pos
->fd
, &filestats
);
679 filesize
= filestats
.st_size
;
681 /* Deal with empty files */
687 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
688 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
693 ret
= munmap_align(pos
->base_mma
);
695 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
699 pos
->base_mma
= NULL
;
701 /* map new base. Need mapping length from header. */
702 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
703 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
704 assert(pos
->base_mma
!= MAP_FAILED
);
706 pos
->content_size
= packet_map_len
;
707 pos
->packet_size
= packet_map_len
;
708 pos
->offset
= 0; /* Position of the packet header */
710 /* update trace_packet_header and stream_packet_context */
711 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
712 /* Read packet header */
713 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
719 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
720 /* Read packet context */
721 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
727 packet_index
->data_offset
= pos
->offset
;
730 ret
= munmap_align(pos
->base_mma
);
732 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
736 pos
->base_mma
= NULL
;
740 /* Retry with larger mapping */
742 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
744 * Reached EOF, but still expecting header/context data.
746 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
749 /* Double the mapping len, and retry */
750 tmp_map_len
= packet_map_len
<< 1;
751 if (tmp_map_len
>> 1 != packet_map_len
) {
753 fprintf(stderr
, "[error] Packet mapping length overflow\n");
756 packet_map_len
= tmp_map_len
;
761 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
762 int fd
, int open_flags
)
766 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
767 sizeof(struct packet_index
));
769 pos
->packet_index
= NULL
;
771 switch (open_flags
& O_ACCMODE
) {
773 pos
->prot
= PROT_READ
;
774 pos
->flags
= MAP_PRIVATE
;
775 pos
->parent
.rw_table
= read_dispatch_table
;
776 pos
->parent
.event_cb
= ctf_read_event
;
777 pos
->parent
.trace
= trace
;
780 pos
->prot
= PROT_READ
| PROT_WRITE
;
781 pos
->flags
= MAP_SHARED
;
782 pos
->parent
.rw_table
= write_dispatch_table
;
783 pos
->parent
.event_cb
= ctf_write_event
;
784 pos
->parent
.trace
= trace
;
786 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
794 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
796 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
797 *pos
->content_size_loc
= pos
->offset
;
802 ret
= munmap_align(pos
->base_mma
);
804 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
809 if (pos
->packet_index
)
810 (void) g_array_free(pos
->packet_index
, TRUE
);
814 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
815 struct packet_index
*prev_index
,
816 struct packet_index
*cur_index
)
818 uint64_t events_discarded_diff
;
819 uint64_t packets_lost_diff
= 0;
821 /* Update packet index time information */
823 /* Current packet begin/end */
824 stream
->current
.real
.begin
=
825 cur_index
->ts_real
.timestamp_begin
;
826 stream
->current
.cycles
.begin
=
827 cur_index
->ts_cycles
.timestamp_begin
;
828 stream
->current
.real
.end
=
829 cur_index
->ts_real
.timestamp_end
;
830 stream
->current
.cycles
.end
=
831 cur_index
->ts_cycles
.timestamp_end
;
833 /* Update packet index discarded event information */
834 events_discarded_diff
= cur_index
->events_discarded
;
836 /* Previous packet begin/end */
837 stream
->prev
.cycles
.begin
=
838 prev_index
->ts_cycles
.timestamp_begin
;
839 stream
->prev
.real
.begin
=
840 prev_index
->ts_real
.timestamp_begin
;
841 stream
->prev
.cycles
.end
=
842 prev_index
->ts_cycles
.timestamp_end
;
843 stream
->prev
.real
.end
=
844 prev_index
->ts_real
.timestamp_end
;
846 events_discarded_diff
-= prev_index
->events_discarded
;
847 /* packet_seq_num stays at 0 if not produced by the tracer */
848 if (cur_index
->packet_seq_num
) {
849 packets_lost_diff
= cur_index
->packet_seq_num
-
850 prev_index
->packet_seq_num
- 1;
853 * Deal with 32-bit wrap-around if the tracer provided a
856 if (prev_index
->events_discarded_len
== 32) {
857 events_discarded_diff
= (uint32_t) events_discarded_diff
;
861 * First packet: use current packet info as limits for
864 stream
->prev
.cycles
.begin
=
865 stream
->prev
.cycles
.end
=
866 stream
->current
.cycles
.begin
;
867 stream
->prev
.real
.begin
=
868 stream
->prev
.real
.end
=
869 stream
->current
.real
.begin
;
871 stream
->events_discarded
= events_discarded_diff
;
872 stream
->packets_lost
= packets_lost_diff
;
876 * Find the timerange where all the streams in the trace are active
879 * Return 0 and update begin/end if necessary on success, return 1 for
880 * empty streams and return a negative value on error.
883 int ctf_intersect_trace(struct bt_trace_descriptor
*td_read
,
884 uint64_t *begin
, uint64_t *end
)
886 struct ctf_trace
*tin
;
887 int stream_id
, ret
= 0;
889 tin
= container_of(td_read
, struct ctf_trace
, parent
);
891 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
894 struct ctf_stream_declaration
*stream_class
;
896 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
900 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
901 struct ctf_file_stream
*file_stream
;
902 struct ctf_stream_pos
*stream_pos
;
903 struct packet_index
*index
;
905 file_stream
= g_ptr_array_index(stream_class
->streams
,
910 stream_pos
= &file_stream
->pos
;
911 if (!stream_pos
->packet_index
||
912 stream_pos
->packet_index
->len
<= 0) {
916 index
= &g_array_index(stream_pos
->packet_index
,
917 struct packet_index
, 0);
918 if (index
->ts_real
.timestamp_begin
> *begin
) {
919 *begin
= index
->ts_real
.timestamp_begin
;
921 index
= &g_array_index(stream_pos
->packet_index
,
923 stream_pos
->packet_index
->len
- 1);
924 if (index
->ts_real
.timestamp_end
< *end
) {
925 *end
= index
->ts_real
.timestamp_end
;
935 * Find the timerange where all streams in the trace collection are active
938 * Return 0 on success.
939 * Return 1 if no intersections are found.
940 * Return a negative value on error.
942 int ctf_find_packets_intersection(struct bt_context
*ctx
,
943 uint64_t *ts_begin
, uint64_t *ts_end
)
947 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
952 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
953 struct bt_trace_descriptor
*td_read
;
955 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
959 ret
= ctf_intersect_trace(td_read
, ts_begin
, ts_end
);
964 if (*ts_end
< *ts_begin
) {
974 * for SEEK_CUR: go to next packet.
975 * for SEEK_SET: go to packet numer (index).
977 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
979 struct ctf_stream_pos
*pos
=
980 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
981 struct ctf_file_stream
*file_stream
=
982 container_of(pos
, struct ctf_file_stream
, pos
);
984 struct packet_index
*packet_index
, *prev_index
;
988 case SEEK_SET
: /* Fall-through */
994 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
995 *pos
->content_size_loc
= pos
->offset
;
999 ret
= munmap_align(pos
->base_mma
);
1001 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1005 pos
->base_mma
= NULL
;
1009 * The caller should never ask for ctf_move_pos across packets,
1010 * except to get exactly at the beginning of the next packet.
1012 if (pos
->prot
& PROT_WRITE
) {
1015 /* The writer will add padding */
1016 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1019 assert(index
== 0); /* only seek supported for now */
1025 pos
->content_size
= -1U; /* Unknown at this point */
1026 pos
->packet_size
= WRITE_PACKET_LEN
;
1028 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1029 pos
->packet_size
/ CHAR_BIT
);
1030 } while (ret
== EINTR
);
1038 if (pos
->offset
== EOF
) {
1041 assert(pos
->cur_index
< pos
->packet_index
->len
);
1042 /* The reader will expect us to skip padding */
1047 if (index
>= pos
->packet_index
->len
) {
1051 pos
->cur_index
= index
;
1057 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1062 packet_index
= &g_array_index(pos
->packet_index
,
1063 struct packet_index
, pos
->cur_index
);
1064 if (pos
->cur_index
> 0) {
1065 prev_index
= &g_array_index(pos
->packet_index
,
1066 struct packet_index
,
1067 pos
->cur_index
- 1);
1071 ctf_update_current_packet_index(&file_stream
->parent
,
1072 prev_index
, packet_index
);
1075 * We need to check if we are in trace read or called
1076 * from packet indexing. In this last case, the
1077 * collection is not there, so we cannot print the
1080 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1081 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1084 packet_index
= &g_array_index(pos
->packet_index
,
1085 struct packet_index
,
1087 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1089 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1091 /* Lookup context/packet size in index */
1092 if (packet_index
->data_offset
== -1) {
1093 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1098 pos
->content_size
= packet_index
->content_size
;
1099 pos
->packet_size
= packet_index
->packet_size
;
1100 pos
->mmap_offset
= packet_index
->offset
;
1101 pos
->data_offset
= packet_index
->data_offset
;
1102 if (pos
->data_offset
< packet_index
->content_size
) {
1103 pos
->offset
= 0; /* will read headers */
1104 } else if (pos
->data_offset
== packet_index
->content_size
) {
1106 pos
->offset
= packet_index
->data_offset
;
1108 goto read_next_packet
;
1114 /* map new base. Need mapping length from header. */
1115 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1116 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1117 if (pos
->base_mma
== MAP_FAILED
) {
1118 fprintf(stderr
, "[error] mmap error %s.\n",
1123 /* update trace_packet_header and stream_packet_context */
1124 if (!(pos
->prot
& PROT_WRITE
) &&
1125 file_stream
->parent
.trace_packet_header
) {
1126 /* Read packet header */
1127 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1130 if (!(pos
->prot
& PROT_WRITE
) &&
1131 file_stream
->parent
.stream_packet_context
) {
1132 /* Read packet context */
1133 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1139 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1145 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1149 if (magic
== TSDL_MAGIC
) {
1151 td
->byte_order
= BYTE_ORDER
;
1152 CTF_TRACE_SET_FIELD(td
, byte_order
);
1153 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1155 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1156 LITTLE_ENDIAN
: BIG_ENDIAN
;
1157 CTF_TRACE_SET_FIELD(td
, byte_order
);
1165 * Returns 0 on success, -1 on error.
1168 int check_version(unsigned int major
, unsigned int minor
)
1183 /* eventually return an error instead of warning */
1185 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1191 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1194 struct metadata_packet_header header
;
1195 size_t readlen
, writelen
, toread
;
1196 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1199 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1203 if (td
->byte_order
!= BYTE_ORDER
) {
1204 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1205 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1206 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1207 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1209 if (header
.checksum
)
1210 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1211 if (header
.compression_scheme
) {
1212 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1213 header
.compression_scheme
);
1216 if (header
.encryption_scheme
) {
1217 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1218 header
.encryption_scheme
);
1221 if (header
.checksum_scheme
) {
1222 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1223 header
.checksum_scheme
);
1226 if (check_version(header
.major
, header
.minor
) < 0)
1228 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1229 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1230 CTF_TRACE_SET_FIELD(td
, uuid
);
1232 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1236 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1239 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1242 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1247 if (babeltrace_debug
) {
1248 buf
[readlen
] = '\0';
1249 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1253 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1254 if (writelen
< readlen
) {
1264 ret
= 0; /* continue reading next packet */
1271 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1272 ret
= fseek(in
, toread
, SEEK_CUR
);
1274 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1281 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1285 size_t size
, buflen
;
1290 * Using strlen on *buf instead of size of open_memstream
1291 * because its size includes garbage at the end (after final
1292 * \0). This is the allocated size, not the actual string size.
1294 out
= babeltrace_open_memstream(buf
, &size
);
1296 perror("Metadata open_memstream");
1300 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1309 /* close to flush the buffer */
1310 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1314 perror("babeltrace_flush_memstream");
1316 closeret
= fclose(in
);
1318 perror("Error in fclose");
1324 perror("Error in fclose");
1326 /* open for reading */
1327 buflen
= strlen(*buf
);
1332 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1334 perror("Metadata fmemopen");
1341 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1342 struct ctf_scanner
*scanner
, int append
)
1344 struct ctf_file_stream
*metadata_stream
;
1347 int ret
= 0, closeret
;
1349 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1350 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1354 metadata_stream
->pos
.fd
= -1;
1356 td
->metadata
= &metadata_stream
->parent
;
1357 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1358 if (metadata_stream
->pos
.fd
< 0) {
1359 fprintf(stderr
, "Unable to open metadata.\n");
1364 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1366 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1367 perror("Metadata stream open");
1371 /* fd now belongs to fp */
1372 metadata_stream
->pos
.fd
= -1;
1374 if (babeltrace_debug
)
1377 if (packet_metadata(td
, fp
)) {
1378 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1382 td
->metadata_string
= buf
;
1383 td
->metadata_packetized
= 1;
1386 unsigned int major
, minor
;
1389 td
->byte_order
= BYTE_ORDER
;
1391 /* Check text-only metadata header and version */
1392 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1394 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1395 if (check_version(major
, minor
) < 0) {
1403 ret
= ctf_scanner_append_ast(scanner
, fp
);
1405 fprintf(stderr
, "[error] Error creating AST\n");
1409 if (babeltrace_debug
) {
1410 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1412 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1417 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1419 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1422 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1423 td
, td
->byte_order
);
1425 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1430 closeret
= fclose(fp
);
1432 perror("Error on fclose");
1436 if (metadata_stream
->pos
.fd
>= 0) {
1437 closeret
= close(metadata_stream
->pos
.fd
);
1439 perror("Error on metadata stream fd close");
1444 g_free(metadata_stream
);
1449 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1450 struct ctf_stream_definition
*stream
,
1451 struct ctf_event_declaration
*event
)
1453 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1455 if (event
->context_decl
) {
1456 struct bt_definition
*definition
=
1457 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1458 stream
->parent_def_scope
, 0, 0, "event.context");
1462 stream_event
->event_context
= container_of(definition
,
1463 struct definition_struct
, p
);
1464 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1466 if (event
->fields_decl
) {
1467 struct bt_definition
*definition
=
1468 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1469 stream
->parent_def_scope
, 0, 0, "event.fields");
1473 stream_event
->event_fields
= container_of(definition
,
1474 struct definition_struct
, p
);
1475 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1477 stream_event
->stream
= stream
;
1478 return stream_event
;
1481 if (stream_event
->event_fields
)
1482 bt_definition_unref(&stream_event
->event_fields
->p
);
1483 if (stream_event
->event_context
)
1484 bt_definition_unref(&stream_event
->event_context
->p
);
1485 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1486 g_quark_to_string(event
->name
));
1491 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1492 struct ctf_stream_declaration
*stream_class
,
1493 struct ctf_stream_definition
*stream
)
1495 size_t def_size
, class_size
, i
;
1498 def_size
= stream
->events_by_id
->len
;
1499 class_size
= stream_class
->events_by_id
->len
;
1501 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1502 for (i
= def_size
; i
< class_size
; i
++) {
1503 struct ctf_event_declaration
*event
=
1504 g_ptr_array_index(stream_class
->events_by_id
, i
);
1505 struct ctf_event_definition
*stream_event
;
1509 stream_event
= create_event_definitions(td
, stream
, event
);
1510 if (!stream_event
) {
1514 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1521 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1523 struct ctf_stream_declaration
*stream_class
;
1527 if (stream
->stream_definitions_created
)
1530 stream_class
= stream
->stream_class
;
1532 if (stream_class
->packet_context_decl
) {
1533 struct bt_definition
*definition
=
1534 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1535 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1540 stream
->stream_packet_context
= container_of(definition
,
1541 struct definition_struct
, p
);
1542 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1544 if (stream_class
->event_header_decl
) {
1545 struct bt_definition
*definition
=
1546 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1547 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1552 stream
->stream_event_header
=
1553 container_of(definition
, struct definition_struct
, p
);
1554 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1556 if (stream_class
->event_context_decl
) {
1557 struct bt_definition
*definition
=
1558 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1559 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1564 stream
->stream_event_context
=
1565 container_of(definition
, struct definition_struct
, p
);
1566 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1568 stream
->events_by_id
= g_ptr_array_new();
1569 ret
= copy_event_declarations_stream_class_to_stream(td
,
1570 stream_class
, stream
);
1576 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1577 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1579 g_free(stream_event
);
1581 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1583 if (stream
->stream_event_context
)
1584 bt_definition_unref(&stream
->stream_event_context
->p
);
1585 if (stream
->stream_event_header
)
1586 bt_definition_unref(&stream
->stream_event_header
->p
);
1587 if (stream
->stream_packet_context
)
1588 bt_definition_unref(&stream
->stream_packet_context
->p
);
1589 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1590 stream_class
->stream_id
, strerror(-ret
));
1595 int stream_assign_class(struct ctf_trace
*td
,
1596 struct ctf_file_stream
*file_stream
,
1599 struct ctf_stream_declaration
*stream
;
1602 file_stream
->parent
.stream_id
= stream_id
;
1603 if (stream_id
>= td
->streams
->len
) {
1604 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1607 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1609 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1612 file_stream
->parent
.stream_class
= stream
;
1613 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1620 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1621 struct ctf_trace
*td
,
1622 struct ctf_file_stream
*file_stream
,
1625 struct packet_index packet_index
;
1626 uint64_t stream_id
= 0;
1627 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1628 int first_packet
= 0;
1633 memset(&packet_index
, 0, sizeof(packet_index
));
1634 if (!pos
->mmap_offset
) {
1638 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1639 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1642 if (pos
->base_mma
) {
1643 /* unmap old base */
1644 ret
= munmap_align(pos
->base_mma
);
1646 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1650 pos
->base_mma
= NULL
;
1652 /* map new base. Need mapping length from header. */
1653 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1654 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1655 assert(pos
->base_mma
!= MAP_FAILED
);
1657 * Use current mapping size as temporary content and packet
1660 pos
->content_size
= packet_map_len
;
1661 pos
->packet_size
= packet_map_len
;
1662 pos
->offset
= 0; /* Position of the packet header */
1664 packet_index
.offset
= pos
->mmap_offset
;
1666 /* read and check header, set stream id (and check) */
1667 if (file_stream
->parent
.trace_packet_header
) {
1668 /* Read packet header */
1669 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1673 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1676 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1677 if (len_index
>= 0) {
1678 struct bt_definition
*field
;
1681 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1682 magic
= bt_get_unsigned_int(field
);
1683 if (magic
!= CTF_MAGIC
) {
1684 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1686 file_stream
->pos
.packet_index
->len
,
1687 (ssize_t
) pos
->mmap_offset
);
1693 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1694 if (len_index
>= 0) {
1695 struct definition_array
*defarray
;
1696 struct bt_definition
*field
;
1698 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1700 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1701 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1702 defarray
= container_of(field
, struct definition_array
, p
);
1703 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1705 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1706 struct bt_definition
*elem
;
1708 elem
= bt_array_index(defarray
, i
);
1709 uuidval
[i
] = bt_get_unsigned_int(elem
);
1711 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1713 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1718 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1719 if (len_index
>= 0) {
1720 struct bt_definition
*field
;
1722 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1723 stream_id
= bt_get_unsigned_int(field
);
1727 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1728 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1730 file_stream
->parent
.stream_id
);
1734 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1739 if (file_stream
->parent
.stream_packet_context
) {
1740 /* Read packet context */
1741 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1745 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1748 /* read packet size from header */
1749 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1750 if (len_index
>= 0) {
1751 struct bt_definition
*field
;
1753 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1754 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1756 /* Use file size for packet size */
1757 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1760 /* read content size from header */
1761 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1762 if (len_index
>= 0) {
1763 struct bt_definition
*field
;
1765 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1766 packet_index
.content_size
= bt_get_unsigned_int(field
);
1768 /* Use packet size if non-zero, else file size */
1769 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1772 /* read timestamp begin from header */
1773 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1774 if (len_index
>= 0) {
1775 struct bt_definition
*field
;
1777 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1778 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1779 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1780 packet_index
.ts_real
.timestamp_begin
=
1781 ctf_get_real_timestamp(
1782 &file_stream
->parent
,
1783 packet_index
.ts_cycles
.timestamp_begin
);
1787 /* read timestamp end from header */
1788 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1789 if (len_index
>= 0) {
1790 struct bt_definition
*field
;
1792 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1793 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1794 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1795 packet_index
.ts_real
.timestamp_end
=
1796 ctf_get_real_timestamp(
1797 &file_stream
->parent
,
1798 packet_index
.ts_cycles
.timestamp_end
);
1802 /* read events discarded from header */
1803 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1804 if (len_index
>= 0) {
1805 struct bt_definition
*field
;
1807 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1808 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1809 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1812 /* read packet_seq_num from header */
1813 len_index
= bt_struct_declaration_lookup_field_index(
1814 file_stream
->parent
.stream_packet_context
->declaration
,
1815 g_quark_from_static_string("packet_seq_num"));
1816 if (len_index
>= 0) {
1817 struct bt_definition
*field
;
1819 field
= bt_struct_definition_get_field_from_index(
1820 file_stream
->parent
.stream_packet_context
,
1822 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1825 /* Use file size for packet size */
1826 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1827 /* Use packet size if non-zero, else file size */
1828 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1831 /* Validate content size and packet size values */
1832 if (packet_index
.content_size
> packet_index
.packet_size
) {
1833 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1834 packet_index
.content_size
, packet_index
.packet_size
);
1838 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1839 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1840 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1844 if (packet_index
.content_size
< pos
->offset
) {
1845 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1849 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1850 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1854 /* Save position after header and context */
1855 packet_index
.data_offset
= pos
->offset
;
1857 /* add index to packet array */
1858 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1860 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1864 /* Retry with larger mapping */
1866 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1868 * Reached EOF, but still expecting header/context data.
1870 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1873 /* Double the mapping len, and retry */
1874 tmp_map_len
= packet_map_len
<< 1;
1875 if (tmp_map_len
>> 1 != packet_map_len
) {
1877 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1880 packet_map_len
= tmp_map_len
;
1885 int create_stream_packet_index(struct ctf_trace
*td
,
1886 struct ctf_file_stream
*file_stream
)
1888 struct ctf_stream_pos
*pos
;
1889 struct stat filestats
;
1892 pos
= &file_stream
->pos
;
1894 ret
= fstat(pos
->fd
, &filestats
);
1898 /* Deal with empty files */
1899 if (!filestats
.st_size
) {
1900 if (file_stream
->parent
.trace_packet_header
1901 || file_stream
->parent
.stream_packet_context
) {
1903 * We expect a trace packet header and/or stream packet
1904 * context. Since a trace needs to have at least one
1905 * packet, empty files are therefore not accepted.
1907 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1911 * Without trace packet header nor stream packet
1912 * context, a one-packet trace can indeed be empty. This
1913 * is only valid if there is only one stream class: 0.
1915 ret
= stream_assign_class(td
, file_stream
, 0);
1922 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1923 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1932 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1936 if (td
->packet_header_decl
) {
1937 struct bt_definition
*definition
=
1938 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1939 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1944 stream
->trace_packet_header
=
1945 container_of(definition
, struct definition_struct
, p
);
1946 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1952 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1957 int import_stream_packet_index(struct ctf_trace
*td
,
1958 struct ctf_file_stream
*file_stream
)
1960 struct ctf_stream_pos
*pos
;
1961 struct ctf_packet_index
*ctf_index
= NULL
;
1962 struct ctf_packet_index_file_hdr index_hdr
;
1963 struct packet_index index
;
1964 uint32_t packet_index_len
, index_minor
;
1966 int first_packet
= 1;
1969 pos
= &file_stream
->pos
;
1971 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
1973 perror("read index file header");
1977 /* Check the index header */
1978 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
1979 fprintf(stderr
, "[error] wrong index magic\n");
1983 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
1984 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
1985 ".%" PRIu32
", supported %d.%d\n",
1986 be32toh(index_hdr
.index_major
),
1987 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
1992 index_minor
= be32toh(index_hdr
.index_minor
);
1994 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
1995 if (packet_index_len
== 0) {
1996 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2001 * Allocate the index length found in header, not internal
2004 ctf_index
= g_malloc0(packet_index_len
);
2005 while (fread(ctf_index
, packet_index_len
, 1,
2006 pos
->index_fp
) == 1) {
2008 struct ctf_stream_declaration
*stream
= NULL
;
2010 memset(&index
, 0, sizeof(index
));
2011 index
.offset
= be64toh(ctf_index
->offset
);
2012 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2013 index
.content_size
= be64toh(ctf_index
->content_size
);
2014 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2015 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2016 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2017 index
.events_discarded_len
= 64;
2018 index
.data_offset
= -1;
2019 stream_id
= be64toh(ctf_index
->stream_id
);
2020 if (index_minor
>= 1) {
2021 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2022 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2025 if (!first_packet
) {
2026 /* add index to packet array */
2027 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2031 file_stream
->parent
.stream_id
= stream_id
;
2032 if (stream_id
< td
->streams
->len
) {
2033 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2036 fprintf(stderr
, "[error] Stream %" PRIu64
2037 " is not declared in metadata.\n",
2042 file_stream
->parent
.stream_class
= stream
;
2043 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2047 /* add index to packet array */
2048 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2051 /* Index containing only the header. */
2052 if (!file_stream
->parent
.stream_class
) {
2065 * Note: many file streams can inherit from the same stream class
2066 * description (metadata).
2069 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2070 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2073 int ret
, fd
, closeret
;
2074 struct ctf_file_stream
*file_stream
;
2075 struct stat statbuf
;
2078 fd
= openat(td
->dirfd
, path
, flags
);
2080 perror("File stream openat()");
2085 /* Don't try to mmap subdirectories. Skip them, return success. */
2086 ret
= fstat(fd
, &statbuf
);
2088 perror("File stream fstat()");
2091 if (S_ISDIR(statbuf
.st_mode
)) {
2092 if (strncmp(path
, "index", 5) != 0) {
2093 fprintf(stderr
, "[warning] Skipping directory '%s' "
2094 "found in trace\n", path
);
2099 if (!statbuf
.st_size
) {
2100 /** Skip empty files. */
2102 goto fd_is_empty_file
;
2105 file_stream
= g_new0(struct ctf_file_stream
, 1);
2106 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2107 file_stream
->pos
.fd
= -1;
2108 file_stream
->pos
.index_fp
= NULL
;
2110 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2111 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2114 file_stream
->pos
.packet_seek
= packet_seek
;
2116 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2121 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2124 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2128 * For now, only a single clock per trace is supported.
2130 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2133 * Allocate the index name for this stream and try to open it.
2135 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2137 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2141 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2144 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2145 ret
= create_stream_packet_index(td
, file_stream
);
2147 fprintf(stderr
, "[error] Stream index creation error.\n");
2151 ret
= openat(td
->dirfd
, index_name
, flags
);
2153 perror("Index file openat()");
2157 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2158 if (!file_stream
->pos
.index_fp
) {
2159 perror("fdopen() error");
2162 ret
= import_stream_packet_index(td
, file_stream
);
2167 ret
= fclose(file_stream
->pos
.index_fp
);
2169 perror("close index");
2175 /* Add stream file to stream class */
2176 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2177 &file_stream
->parent
);
2181 if (file_stream
->pos
.index_fp
) {
2182 ret
= fclose(file_stream
->pos
.index_fp
);
2184 perror("close index");
2187 if (file_stream
->parent
.trace_packet_header
)
2188 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2192 closeret
= ctf_fini_pos(&file_stream
->pos
);
2194 fprintf(stderr
, "Error on ctf_fini_pos\n");
2196 g_free(file_stream
);
2200 closeret
= close(fd
);
2202 perror("Error on fd close");
2209 int ctf_open_trace_read(struct ctf_trace
*td
,
2210 const char *path
, int flags
,
2211 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2212 int whence
), FILE *metadata_fp
)
2214 struct ctf_scanner
*scanner
;
2216 struct dirent
*dirent
;
2217 struct dirent
*diriter
;
2223 /* Open trace directory */
2224 td
->dir
= opendir(path
);
2226 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2231 td
->dirfd
= open(path
, 0);
2232 if (td
->dirfd
< 0) {
2233 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2234 perror("Trace directory open");
2238 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2239 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2242 * Keep the metadata file separate.
2243 * Keep scanner object local to the open. We don't support
2244 * incremental metadata append for on-disk traces.
2246 scanner
= ctf_scanner_alloc();
2248 fprintf(stderr
, "[error] Error allocating scanner\n");
2250 goto error_metadata
;
2252 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2253 ctf_scanner_free(scanner
);
2255 if (ret
== -ENOENT
) {
2256 fprintf(stderr
, "[warning] Empty metadata.\n");
2258 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2259 goto error_metadata
;
2263 * Open each stream: for each file, try to open, check magic
2264 * number, and get the stream ID to add to the right location in
2268 dirent_len
= offsetof(struct dirent
, d_name
) +
2269 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
2271 dirent
= malloc(dirent_len
);
2274 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2276 fprintf(stderr
, "[error] Readdir error.\n");
2281 /* Ignore hidden files, ., .. and metadata. */
2282 if (!strncmp(diriter
->d_name
, ".", 1)
2283 || !strcmp(diriter
->d_name
, "..")
2284 || !strcmp(diriter
->d_name
, "metadata"))
2287 /* Ignore index files : *.idx */
2288 ext
= strrchr(diriter
->d_name
, '.');
2289 if (ext
&& (!strcmp(ext
, ".idx"))) {
2293 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2294 flags
, packet_seek
);
2296 fprintf(stderr
, "[error] Open file stream error.\n");
2307 closeret
= close(td
->dirfd
);
2309 perror("Error on fd close");
2312 closeret
= closedir(td
->dir
);
2314 perror("Error on closedir");
2321 * ctf_open_trace: Open a CTF trace and index it.
2322 * Note that the user must seek the trace after the open (using the iterator)
2323 * since the index creation read it entirely.
2326 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2327 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2328 int whence
), FILE *metadata_fp
)
2330 struct ctf_trace
*td
;
2334 * If packet_seek is NULL, we provide our default version.
2337 packet_seek
= ctf_packet_seek
;
2339 td
= g_new0(struct ctf_trace
, 1);
2341 switch (flags
& O_ACCMODE
) {
2344 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2347 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2352 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2355 fprintf(stderr
, "[error] Incorrect open flags.\n");
2359 ret
= trace_debug_info_create(td
);
2366 trace_debug_info_destroy(td
);
2372 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2373 struct bt_mmap_stream
*mmap_info
)
2375 pos
->mmap_offset
= 0;
2376 pos
->packet_size
= 0;
2377 pos
->content_size
= 0;
2378 pos
->content_size_loc
= NULL
;
2379 pos
->fd
= mmap_info
->fd
;
2380 pos
->base_mma
= NULL
;
2384 pos
->prot
= PROT_READ
;
2385 pos
->flags
= MAP_PRIVATE
;
2386 pos
->parent
.rw_table
= read_dispatch_table
;
2387 pos
->parent
.event_cb
= ctf_read_event
;
2388 pos
->priv
= mmap_info
->priv
;
2389 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2390 sizeof(struct packet_index
));
2394 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2395 struct ctf_file_stream
*file_stream
,
2396 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2399 struct ctf_stream_declaration
*stream
;
2403 /* Ask for the first packet to get the stream_id. */
2404 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2405 stream_id
= file_stream
->parent
.stream_id
;
2406 if (stream_id
>= td
->streams
->len
) {
2407 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2408 "in metadata.\n", stream_id
);
2412 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2414 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2415 "in metadata.\n", stream_id
);
2419 file_stream
->parent
.stream_class
= stream
;
2420 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2426 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2427 struct bt_mmap_stream
*mmap_info
,
2428 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2432 struct ctf_file_stream
*file_stream
;
2434 file_stream
= g_new0(struct ctf_file_stream
, 1);
2435 file_stream
->parent
.stream_id
= -1ULL;
2436 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2437 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2439 file_stream
->pos
.packet_seek
= packet_seek
;
2441 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2446 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2451 * For now, only a single clock per trace is supported.
2453 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2455 /* Add stream file to stream class */
2456 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2457 &file_stream
->parent
);
2461 if (file_stream
->parent
.trace_packet_header
)
2462 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2464 g_free(file_stream
);
2469 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2470 struct bt_mmap_stream_list
*mmap_list
,
2471 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2476 struct bt_mmap_stream
*mmap_info
;
2478 td
->scanner
= ctf_scanner_alloc();
2480 fprintf(stderr
, "[error] Error allocating scanner\n");
2484 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2486 if (ret
== -ENOENT
) {
2487 fprintf(stderr
, "[warning] Empty metadata.\n");
2493 * for each stream, try to open, check magic number, and get the
2494 * stream ID to add to the right location in the stream array.
2496 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2497 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2499 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2506 ctf_scanner_free(td
->scanner
);
2511 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2512 struct bt_mmap_stream_list
*mmap_list
,
2513 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2517 struct ctf_trace
*td
;
2521 fprintf(stderr
, "[error] No metadata file pointer associated, "
2522 "required for mmap parsing\n");
2526 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2529 td
= g_new0(struct ctf_trace
, 1);
2531 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2535 ret
= trace_debug_info_create(td
);
2548 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2551 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2557 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2560 /* for each stream_class */
2561 for (i
= 0; i
< td
->streams
->len
; i
++) {
2562 struct ctf_stream_declaration
*stream_class
;
2564 stream_class
= g_ptr_array_index(td
->streams
, i
);
2567 /* for each stream */
2568 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2569 struct ctf_stream_definition
*stream
;
2571 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2574 ret
= copy_event_declarations_stream_class_to_stream(td
,
2575 stream_class
, stream
);
2584 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2587 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2589 /* for each stream_class */
2590 for (i
= 0; i
< td
->streams
->len
; i
++) {
2591 struct ctf_stream_declaration
*stream_class
;
2593 stream_class
= g_ptr_array_index(td
->streams
, i
);
2596 /* for each file_stream */
2597 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2598 struct ctf_stream_definition
*stream
;
2599 struct ctf_stream_pos
*stream_pos
;
2600 struct ctf_file_stream
*cfs
;
2602 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2605 cfs
= container_of(stream
, struct ctf_file_stream
,
2607 stream_pos
= &cfs
->pos
;
2608 if (!stream_pos
->packet_index
)
2611 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2612 struct packet_index
*index
;
2614 index
= &g_array_index(stream_pos
->packet_index
,
2615 struct packet_index
, k
);
2616 index
->ts_real
.timestamp_begin
=
2617 ctf_get_real_timestamp(stream
,
2618 index
->ts_cycles
.timestamp_begin
);
2619 index
->ts_real
.timestamp_end
=
2620 ctf_get_real_timestamp(stream
,
2621 index
->ts_cycles
.timestamp_end
);
2629 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2633 ret
= ctf_fini_pos(&file_stream
->pos
);
2635 fprintf(stderr
, "Error on ctf_fini_pos\n");
2638 if (file_stream
->pos
.fd
>= 0) {
2639 ret
= close(file_stream
->pos
.fd
);
2641 perror("Error closing file fd");
2649 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2651 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2657 for (i
= 0; i
< td
->streams
->len
; i
++) {
2658 struct ctf_stream_declaration
*stream
;
2661 stream
= g_ptr_array_index(td
->streams
, i
);
2664 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2665 struct ctf_file_stream
*file_stream
;
2666 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2667 struct ctf_file_stream
, parent
);
2668 ret
= ctf_close_file_stream(file_stream
);
2674 ctf_destroy_metadata(td
);
2675 ctf_scanner_free(td
->scanner
);
2676 if (td
->dirfd
>= 0) {
2677 ret
= close(td
->dirfd
);
2679 perror("Error closing dirfd");
2684 ret
= closedir(td
->dir
);
2686 perror("Error closedir");
2690 free(td
->metadata_string
);
2691 trace_debug_info_destroy(td
);
2697 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2698 struct bt_context
*ctx
)
2700 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2703 td
->parent
.ctx
= ctx
;
2707 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2708 struct bt_trace_handle
*handle
)
2710 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2713 td
->parent
.handle
= handle
;
2717 void __attribute__((constructor
)) ctf_init(void)
2721 ctf_format
.name
= g_quark_from_static_string("ctf");
2722 ret
= bt_register_format(&ctf_format
);
2727 void __attribute__((destructor
)) ctf_exit(void)
2729 bt_unregister_format(&ctf_format
);