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/format-internal.h>
31 #include <babeltrace/ctf/types.h>
32 #include <babeltrace/ctf/metadata.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/ctf/events-internal.h>
35 #include <babeltrace/trace-handle-internal.h>
36 #include <babeltrace/context-internal.h>
37 #include <babeltrace/compat/uuid.h>
38 #include <babeltrace/endian.h>
39 #include <babeltrace/trace-debug-info.h>
40 #include <babeltrace/ctf/ctf-index.h>
45 #include <sys/types.h>
53 #include "metadata/ctf-scanner.h"
54 #include "metadata/ctf-parser.h"
55 #include "metadata/ctf-ast.h"
56 #include "events-private.h"
57 #include <babeltrace/compat/memstream.h>
58 #include <babeltrace/compat/fcntl.h>
60 #define LOG2_CHAR_BIT 3
63 * Length of first attempt at mapping a packet header, in bits.
65 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
68 * Lenght of packet to write, in bits.
70 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
72 #define NSEC_PER_SEC 1000000000ULL
74 #define INDEX_PATH "./index/%s.idx"
81 uint64_t opt_clock_offset
;
82 uint64_t opt_clock_offset_ns
;
85 char *opt_debug_info_dir
;
86 char *opt_debug_info_target_prefix
;
89 * TODO: babeltrace_ctf_console_output ensures that we only print
90 * discarded events when ctf-text plugin is used. Should be cleaned up
91 * with the plugin system redesign.
93 int babeltrace_ctf_console_output
;
96 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
97 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
101 struct bt_trace_descriptor
*ctf_open_mmap_trace(
102 struct bt_mmap_stream_list
*mmap_list
,
103 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
107 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
108 struct bt_context
*ctx
);
110 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
111 struct bt_trace_handle
*handle
);
114 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
116 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
117 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
119 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
120 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
122 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
125 rw_dispatch read_dispatch_table
[] = {
126 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
127 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
128 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
129 [ CTF_TYPE_STRING
] = ctf_string_read
,
130 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
131 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
132 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
133 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
137 rw_dispatch write_dispatch_table
[] = {
138 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
139 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
140 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
141 [ CTF_TYPE_STRING
] = ctf_string_write
,
142 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
143 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
144 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
145 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
149 struct bt_format ctf_format
= {
150 .open_trace
= ctf_open_trace
,
151 .open_mmap_trace
= ctf_open_mmap_trace
,
152 .close_trace
= ctf_close_trace
,
153 .set_context
= ctf_set_context
,
154 .set_handle
= ctf_set_handle
,
155 .timestamp_begin
= ctf_timestamp_begin
,
156 .timestamp_end
= ctf_timestamp_end
,
157 .convert_index_timestamp
= ctf_convert_index_timestamp
,
160 void bt_ctf_hook(void)
163 * Dummy function to prevent the linker from discarding this format as
164 * "unused" in static builds.
169 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
170 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
172 struct ctf_trace
*tin
;
173 uint64_t begin
= ULLONG_MAX
;
176 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
181 /* for each stream_class */
182 for (i
= 0; i
< tin
->streams
->len
; i
++) {
183 struct ctf_stream_declaration
*stream_class
;
185 stream_class
= g_ptr_array_index(tin
->streams
, i
);
188 /* for each file_stream */
189 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
190 struct ctf_stream_definition
*stream
;
191 struct ctf_file_stream
*cfs
;
192 struct ctf_stream_pos
*stream_pos
;
193 struct packet_index
*index
;
195 stream
= g_ptr_array_index(stream_class
->streams
, j
);
196 cfs
= container_of(stream
, struct ctf_file_stream
,
198 stream_pos
= &cfs
->pos
;
200 if (!stream_pos
->packet_index
)
203 if (stream_pos
->packet_index
->len
<= 0)
206 index
= &g_array_index(stream_pos
->packet_index
,
208 stream_pos
->packet_index
->len
- 1);
209 if (type
== BT_CLOCK_REAL
) {
210 if (index
->ts_real
.timestamp_begin
< begin
)
211 begin
= index
->ts_real
.timestamp_begin
;
212 } else if (type
== BT_CLOCK_CYCLES
) {
213 if (index
->ts_cycles
.timestamp_begin
< begin
)
214 begin
= index
->ts_cycles
.timestamp_begin
;
228 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
229 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
231 struct ctf_trace
*tin
;
235 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
240 /* for each stream_class */
241 for (i
= 0; i
< tin
->streams
->len
; i
++) {
242 struct ctf_stream_declaration
*stream_class
;
244 stream_class
= g_ptr_array_index(tin
->streams
, i
);
247 /* for each file_stream */
248 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
249 struct ctf_stream_definition
*stream
;
250 struct ctf_file_stream
*cfs
;
251 struct ctf_stream_pos
*stream_pos
;
252 struct packet_index
*index
;
254 stream
= g_ptr_array_index(stream_class
->streams
, j
);
255 cfs
= container_of(stream
, struct ctf_file_stream
,
257 stream_pos
= &cfs
->pos
;
259 if (!stream_pos
->packet_index
)
262 if (stream_pos
->packet_index
->len
<= 0)
265 index
= &g_array_index(stream_pos
->packet_index
,
267 stream_pos
->packet_index
->len
- 1);
268 if (type
== BT_CLOCK_REAL
) {
269 if (index
->ts_real
.timestamp_end
> end
)
270 end
= index
->ts_real
.timestamp_end
;
271 } else if (type
== BT_CLOCK_CYCLES
) {
272 if (index
->ts_cycles
.timestamp_end
> end
)
273 end
= index
->ts_cycles
.timestamp_end
;
287 * Update stream current timestamp
290 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
291 struct definition_integer
*integer_definition
)
293 struct declaration_integer
*integer_declaration
=
294 integer_definition
->declaration
;
295 uint64_t oldval
, newval
, updateval
;
297 if (unlikely(integer_declaration
->len
== 64)) {
298 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
299 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
300 stream
->cycles_timestamp
);
304 oldval
= stream
->cycles_timestamp
;
305 oldval
&= (1ULL << integer_declaration
->len
) - 1;
306 newval
= integer_definition
->value
._unsigned
;
307 /* Test for overflow by comparing low bits */
309 newval
+= 1ULL << integer_declaration
->len
;
310 /* updateval contains old high bits, and new low bits (sum) */
311 updateval
= stream
->cycles_timestamp
;
312 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
314 stream
->cycles_timestamp
= updateval
;
316 /* convert to real timestamp */
317 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
318 stream
->cycles_timestamp
);
322 * Print timestamp, rescaling clock frequency to nanoseconds and
323 * applying offsets as needed (unix time).
326 void ctf_print_timestamp_real(FILE *fp
,
327 struct ctf_stream_definition
*stream
,
330 uint64_t ts_sec
= 0, ts_nsec
;
334 /* Add command-line offset in ns*/
335 ts_nsec
+= opt_clock_offset_ns
;
337 /* Add command-line offset */
338 ts_sec
+= opt_clock_offset
;
340 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
341 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
343 if (!opt_clock_seconds
) {
345 time_t time_s
= (time_t) ts_sec
;
347 if (!opt_clock_gmt
) {
350 res
= localtime_r(&time_s
, &tm
);
352 fprintf(stderr
, "[warning] Unable to get localtime.\n");
358 res
= gmtime_r(&time_s
, &tm
);
360 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
364 if (opt_clock_date
) {
368 /* Print date and time */
369 res
= strftime(timestr
, sizeof(timestr
),
372 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
375 fprintf(fp
, "%s", timestr
);
377 /* Print time in HH:MM:SS.ns */
378 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
379 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
383 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
391 * Print timestamp, in cycles
394 void ctf_print_timestamp_cycles(FILE *fp
,
395 struct ctf_stream_definition
*stream
,
398 fprintf(fp
, "%020" PRIu64
, timestamp
);
401 void ctf_print_timestamp(FILE *fp
,
402 struct ctf_stream_definition
*stream
,
405 if (opt_clock_cycles
) {
406 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
408 ctf_print_timestamp_real(fp
, stream
, timestamp
);
413 void print_uuid(FILE *fp
, unsigned char *uuid
)
417 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
418 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
422 * Discarded events can be either:
423 * - discarded after end of previous buffer due to buffer full:
424 * happened within range: [ prev_timestamp_end, timestamp_begin ]
425 * - discarded within current buffer due to either event too large or
426 * nested wrap-around:
427 * happened within range: [ timestamp_begin, timestamp_end ]
429 * Given we have discarded counters of those two types merged into the
430 * events_discarded counter, we need to use the union of those ranges:
431 * [ prev_timestamp_end, timestamp_end ]
433 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
434 * consumer had time to extract them. We keep track of those gaps with the
435 * packet sequence number in each packet.
438 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
440 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
441 !babeltrace_ctf_console_output
) {
445 if (stream
->events_discarded
) {
446 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
447 stream
->events_discarded
);
448 } else if (stream
->packets_lost
) {
449 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
450 stream
->packets_lost
);
452 if (opt_clock_cycles
) {
453 ctf_print_timestamp(fp
, stream
,
454 stream
->prev
.cycles
.end
);
455 fprintf(fp
, "] and [");
456 ctf_print_timestamp(fp
, stream
,
457 stream
->current
.cycles
.end
);
459 ctf_print_timestamp(fp
, stream
,
460 stream
->prev
.real
.end
);
461 fprintf(fp
, "] and [");
462 ctf_print_timestamp(fp
, stream
,
463 stream
->current
.real
.end
);
465 fprintf(fp
, "] in trace UUID ");
466 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
467 if (stream
->stream_class
->trace
->parent
.path
[0])
468 fprintf(fp
, ", at path: \"%s\"",
469 stream
->stream_class
->trace
->parent
.path
);
471 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
473 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
475 fprintf(fp
, "You should consider recording a new trace with larger "
476 "buffers or with fewer events enabled.\n");
481 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
483 struct ctf_stream_pos
*pos
=
484 container_of(ppos
, struct ctf_stream_pos
, parent
);
485 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
486 struct ctf_event_definition
*event
;
490 /* We need to check for EOF here for empty files. */
491 if (unlikely(pos
->offset
== EOF
))
494 ctf_pos_get_event(pos
);
496 /* save the current position as a restore point */
497 pos
->last_offset
= pos
->offset
;
500 * This is the EOF check after we've advanced the position in
503 if (unlikely(pos
->offset
== EOF
))
506 /* Stream is inactive for now (live reading). */
507 if (unlikely(pos
->content_size
== 0))
511 * Packet seeked to by ctf_pos_get_event() only contains
512 * headers, no event. Consider stream as inactive (live
515 if (unlikely(pos
->data_offset
== pos
->content_size
))
518 assert(pos
->offset
< pos
->content_size
);
520 /* Read event header */
521 if (likely(stream
->stream_event_header
)) {
522 struct definition_integer
*integer_definition
;
523 struct bt_definition
*variant
;
525 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
528 /* lookup event id */
529 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
530 if (integer_definition
) {
531 id
= integer_definition
->value
._unsigned
;
533 struct definition_enum
*enum_definition
;
535 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
536 if (enum_definition
) {
537 id
= enum_definition
->integer
->value
._unsigned
;
541 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
543 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
544 if (integer_definition
) {
545 id
= integer_definition
->value
._unsigned
;
548 stream
->event_id
= id
;
550 /* lookup timestamp */
551 stream
->has_timestamp
= 0;
552 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
553 if (integer_definition
) {
554 ctf_update_timestamp(stream
, integer_definition
);
555 stream
->has_timestamp
= 1;
558 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
559 if (integer_definition
) {
560 ctf_update_timestamp(stream
, integer_definition
);
561 stream
->has_timestamp
= 1;
567 /* Read stream-declared event context */
568 if (stream
->stream_event_context
) {
569 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
574 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
575 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
578 event
= g_ptr_array_index(stream
->events_by_id
, id
);
579 if (unlikely(!event
)) {
580 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
584 /* Read event-declared event context */
585 if (event
->event_context
) {
586 ret
= generic_rw(ppos
, &event
->event_context
->p
);
591 /* Read event payload */
592 if (likely(event
->event_fields
)) {
593 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
598 if (pos
->last_offset
== pos
->offset
) {
599 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
606 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
611 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
613 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
614 struct ctf_event_definition
*event
;
618 id
= stream
->event_id
;
620 /* print event header */
621 if (likely(stream
->stream_event_header
)) {
622 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
627 /* print stream-declared event context */
628 if (stream
->stream_event_context
) {
629 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
634 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
635 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
638 event
= g_ptr_array_index(stream
->events_by_id
, id
);
639 if (unlikely(!event
)) {
640 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
644 /* print event-declared event context */
645 if (event
->event_context
) {
646 ret
= generic_rw(pos
, &event
->event_context
->p
);
651 /* Read and print event payload */
652 if (likely(event
->event_fields
)) {
653 ret
= generic_rw(pos
, &event
->event_fields
->p
);
661 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
666 * One side-effect of this function is to unmap pos mmap base if one is
670 int find_data_offset(struct ctf_stream_pos
*pos
,
671 struct ctf_file_stream
*file_stream
,
672 struct packet_index
*packet_index
)
674 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
675 struct stat filestats
;
679 pos
= &file_stream
->pos
;
681 ret
= fstat(pos
->fd
, &filestats
);
684 filesize
= filestats
.st_size
;
686 /* Deal with empty files */
692 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
693 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
698 ret
= munmap_align(pos
->base_mma
);
700 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
704 pos
->base_mma
= NULL
;
706 /* map new base. Need mapping length from header. */
707 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
708 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
709 assert(pos
->base_mma
!= MAP_FAILED
);
711 pos
->content_size
= packet_map_len
;
712 pos
->packet_size
= packet_map_len
;
713 pos
->offset
= 0; /* Position of the packet header */
715 /* update trace_packet_header and stream_packet_context */
716 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
717 /* Read packet header */
718 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
724 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
725 /* Read packet context */
726 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
732 packet_index
->data_offset
= pos
->offset
;
735 ret
= munmap_align(pos
->base_mma
);
737 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
741 pos
->base_mma
= NULL
;
745 /* Retry with larger mapping */
747 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
749 * Reached EOF, but still expecting header/context data.
751 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
754 /* Double the mapping len, and retry */
755 tmp_map_len
= packet_map_len
<< 1;
756 if (tmp_map_len
>> 1 != packet_map_len
) {
758 fprintf(stderr
, "[error] Packet mapping length overflow\n");
761 packet_map_len
= tmp_map_len
;
766 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
767 int fd
, int open_flags
)
771 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
772 sizeof(struct packet_index
));
774 pos
->packet_index
= NULL
;
776 switch (open_flags
& O_ACCMODE
) {
778 pos
->prot
= PROT_READ
;
779 pos
->flags
= MAP_PRIVATE
;
780 pos
->parent
.rw_table
= read_dispatch_table
;
781 pos
->parent
.event_cb
= ctf_read_event
;
782 pos
->parent
.trace
= trace
;
785 pos
->prot
= PROT_READ
| PROT_WRITE
;
786 pos
->flags
= MAP_SHARED
;
787 pos
->parent
.rw_table
= write_dispatch_table
;
788 pos
->parent
.event_cb
= ctf_write_event
;
789 pos
->parent
.trace
= trace
;
797 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
799 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
800 *pos
->content_size_loc
= pos
->offset
;
805 ret
= munmap_align(pos
->base_mma
);
807 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
812 if (pos
->packet_index
)
813 (void) g_array_free(pos
->packet_index
, TRUE
);
817 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
818 struct packet_index
*prev_index
,
819 struct packet_index
*cur_index
)
821 uint64_t events_discarded_diff
;
822 uint64_t packets_lost_diff
= 0;
824 /* Update packet index time information */
826 /* Current packet begin/end */
827 stream
->current
.real
.begin
=
828 cur_index
->ts_real
.timestamp_begin
;
829 stream
->current
.cycles
.begin
=
830 cur_index
->ts_cycles
.timestamp_begin
;
831 stream
->current
.real
.end
=
832 cur_index
->ts_real
.timestamp_end
;
833 stream
->current
.cycles
.end
=
834 cur_index
->ts_cycles
.timestamp_end
;
836 /* Update packet index discarded event information */
837 events_discarded_diff
= cur_index
->events_discarded
;
839 /* Previous packet begin/end */
840 stream
->prev
.cycles
.begin
=
841 prev_index
->ts_cycles
.timestamp_begin
;
842 stream
->prev
.real
.begin
=
843 prev_index
->ts_real
.timestamp_begin
;
844 stream
->prev
.cycles
.end
=
845 prev_index
->ts_cycles
.timestamp_end
;
846 stream
->prev
.real
.end
=
847 prev_index
->ts_real
.timestamp_end
;
849 events_discarded_diff
-= prev_index
->events_discarded
;
850 /* packet_seq_num stays at 0 if not produced by the tracer */
851 if (cur_index
->packet_seq_num
) {
852 packets_lost_diff
= cur_index
->packet_seq_num
-
853 prev_index
->packet_seq_num
- 1;
856 * Deal with 32-bit wrap-around if the tracer provided a
859 if (prev_index
->events_discarded_len
== 32) {
860 events_discarded_diff
= (uint32_t) events_discarded_diff
;
864 * First packet: use current packet info as limits for
867 stream
->prev
.cycles
.begin
=
868 stream
->prev
.cycles
.end
=
869 stream
->current
.cycles
.begin
;
870 stream
->prev
.real
.begin
=
871 stream
->prev
.real
.end
=
872 stream
->current
.real
.begin
;
874 stream
->events_discarded
= events_discarded_diff
;
875 stream
->packets_lost
= packets_lost_diff
;
879 * Find the timerange where all the streams in the trace are active
882 * Return 0 and update begin/end if necessary on success, return 1 for
883 * empty streams and return a negative value on error.
886 int ctf_find_stream_intersection(struct bt_trace_descriptor
*td_read
,
887 struct packet_index_time
*_real
,
888 struct packet_index_time
*_cycles
)
890 int stream_id
, ret
= 0;
891 struct packet_index_time real
= { 0, UINT64_MAX
},
892 cycles
= { 0, UINT64_MAX
};
893 struct ctf_trace
*tin
= container_of(td_read
, struct ctf_trace
, parent
);
895 /* At least one of the two return args must be provided. */
896 if (!_real
&& !_cycles
) {
901 if (tin
->streams
->len
== 0) {
906 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
909 struct ctf_stream_declaration
*stream_class
;
911 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
915 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
916 struct ctf_file_stream
*file_stream
;
917 struct ctf_stream_pos
*stream_pos
;
918 struct packet_index
*index
;
920 file_stream
= g_ptr_array_index(stream_class
->streams
,
925 stream_pos
= &file_stream
->pos
;
926 if (!stream_pos
->packet_index
||
927 stream_pos
->packet_index
->len
<= 0) {
931 index
= &g_array_index(stream_pos
->packet_index
,
932 struct packet_index
, 0);
933 real
.timestamp_begin
= max(real
.timestamp_begin
,
934 index
->ts_real
.timestamp_begin
);
935 cycles
.timestamp_begin
= max(cycles
.timestamp_begin
,
936 index
->ts_cycles
.timestamp_begin
);
938 index
= &g_array_index(stream_pos
->packet_index
,
940 stream_pos
->packet_index
->len
- 1);
941 real
.timestamp_end
= min(real
.timestamp_end
,
942 index
->ts_real
.timestamp_end
);
943 cycles
.timestamp_end
= min(cycles
.timestamp_end
,
944 index
->ts_cycles
.timestamp_end
);
960 * Find the union of all active regions in the trace collection's traces.
961 * Returns "real" timestamps.
963 * Return 0 on success.
964 * Return 1 if no intersections are found.
965 * Return a negative value on error.
967 int ctf_find_tc_stream_packet_intersection_union(struct bt_context
*ctx
,
968 uint64_t *_ts_begin
, uint64_t *_ts_end
)
971 uint64_t ts_begin
= UINT64_MAX
, ts_end
= 0;
973 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
|| !_ts_begin
|| !_ts_end
) {
978 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
979 struct bt_trace_descriptor
*td_read
;
980 struct packet_index_time intersection_real
;
982 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
986 ret
= ctf_find_stream_intersection(td_read
, &intersection_real
,
989 /* Empty trace or no stream intersection. */
991 } else if (ret
< 0) {
995 ts_begin
= min(intersection_real
.timestamp_begin
, ts_begin
);
996 ts_end
= max(intersection_real
.timestamp_end
, ts_end
);
999 if (ts_end
< ts_begin
) {
1003 *_ts_begin
= ts_begin
;
1009 int ctf_tc_set_stream_intersection_mode(struct bt_context
*ctx
)
1013 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
1018 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
1019 struct bt_trace_descriptor
*td_read
;
1020 struct packet_index_time intersection_real
;
1022 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
1027 ret
= ctf_find_stream_intersection(td_read
, &intersection_real
,
1030 /* Empty trace or no stream intersection. */
1032 } else if (ret
< 0) {
1036 td_read
->interval_real
= intersection_real
;
1037 td_read
->interval_set
= true;
1044 * for SEEK_CUR: go to next packet.
1045 * for SEEK_SET: go to packet numer (index).
1047 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
1049 struct ctf_stream_pos
*pos
=
1050 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
1051 struct ctf_file_stream
*file_stream
=
1052 container_of(pos
, struct ctf_file_stream
, pos
);
1054 struct packet_index
*packet_index
, *prev_index
;
1058 case SEEK_SET
: /* Fall-through */
1064 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
1065 *pos
->content_size_loc
= pos
->offset
;
1067 if (pos
->base_mma
) {
1068 /* unmap old base */
1069 ret
= munmap_align(pos
->base_mma
);
1071 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1075 pos
->base_mma
= NULL
;
1079 * The caller should never ask for ctf_move_pos across packets,
1080 * except to get exactly at the beginning of the next packet.
1082 if (pos
->prot
& PROT_WRITE
) {
1085 /* The writer will add padding */
1086 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1089 assert(index
== 0); /* only seek supported for now */
1095 pos
->content_size
= -1U; /* Unknown at this point */
1096 pos
->packet_size
= WRITE_PACKET_LEN
;
1098 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1099 pos
->packet_size
/ CHAR_BIT
);
1100 } while (ret
== EINTR
);
1108 if (pos
->offset
== EOF
) {
1111 assert(pos
->cur_index
< pos
->packet_index
->len
);
1112 /* The reader will expect us to skip padding */
1117 if (index
>= pos
->packet_index
->len
) {
1121 pos
->cur_index
= index
;
1127 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1132 packet_index
= &g_array_index(pos
->packet_index
,
1133 struct packet_index
, pos
->cur_index
);
1134 if (pos
->cur_index
> 0) {
1135 prev_index
= &g_array_index(pos
->packet_index
,
1136 struct packet_index
,
1137 pos
->cur_index
- 1);
1141 ctf_update_current_packet_index(&file_stream
->parent
,
1142 prev_index
, packet_index
);
1145 * We need to check if we are in trace read or called
1146 * from packet indexing. In this last case, the
1147 * collection is not there, so we cannot print the
1150 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1151 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1154 packet_index
= &g_array_index(pos
->packet_index
,
1155 struct packet_index
,
1157 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1159 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1161 /* Lookup context/packet size in index */
1162 if (packet_index
->data_offset
== -1) {
1163 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1168 pos
->content_size
= packet_index
->content_size
;
1169 pos
->packet_size
= packet_index
->packet_size
;
1170 pos
->mmap_offset
= packet_index
->offset
;
1171 pos
->data_offset
= packet_index
->data_offset
;
1172 if (pos
->data_offset
< packet_index
->content_size
) {
1173 pos
->offset
= 0; /* will read headers */
1174 } else if (pos
->data_offset
== packet_index
->content_size
) {
1176 pos
->offset
= packet_index
->data_offset
;
1178 goto read_next_packet
;
1184 /* map new base. Need mapping length from header. */
1185 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1186 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1187 if (pos
->base_mma
== MAP_FAILED
) {
1188 fprintf(stderr
, "[error] mmap error %s.\n",
1193 /* update trace_packet_header and stream_packet_context */
1194 if (!(pos
->prot
& PROT_WRITE
) &&
1195 file_stream
->parent
.trace_packet_header
) {
1196 /* Read packet header */
1197 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1200 if (!(pos
->prot
& PROT_WRITE
) &&
1201 file_stream
->parent
.stream_packet_context
) {
1202 /* Read packet context */
1203 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1209 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1215 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1219 if (magic
== TSDL_MAGIC
) {
1221 td
->byte_order
= BYTE_ORDER
;
1222 CTF_TRACE_SET_FIELD(td
, byte_order
);
1223 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1225 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1226 LITTLE_ENDIAN
: BIG_ENDIAN
;
1227 CTF_TRACE_SET_FIELD(td
, byte_order
);
1235 * Returns 0 on success, -1 on error.
1238 int check_version(unsigned int major
, unsigned int minor
)
1253 /* eventually return an error instead of warning */
1255 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1261 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1264 struct metadata_packet_header header
;
1265 size_t readlen
, writelen
, toread
;
1266 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1269 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1273 if (td
->byte_order
!= BYTE_ORDER
) {
1274 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1275 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1276 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1277 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1279 if (header
.checksum
)
1280 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1281 if (header
.compression_scheme
) {
1282 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1283 header
.compression_scheme
);
1286 if (header
.encryption_scheme
) {
1287 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1288 header
.encryption_scheme
);
1291 if (header
.checksum_scheme
) {
1292 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1293 header
.checksum_scheme
);
1296 if (check_version(header
.major
, header
.minor
) < 0)
1298 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1299 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1300 CTF_TRACE_SET_FIELD(td
, uuid
);
1302 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1306 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1309 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1312 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1317 if (babeltrace_debug
) {
1318 buf
[readlen
] = '\0';
1319 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1323 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1324 if (writelen
< readlen
) {
1334 ret
= 0; /* continue reading next packet */
1341 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1342 ret
= fseek(in
, toread
, SEEK_CUR
);
1344 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1351 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1355 size_t size
, buflen
;
1360 * Using strlen on *buf instead of size of open_memstream
1361 * because its size includes garbage at the end (after final
1362 * \0). This is the allocated size, not the actual string size.
1364 out
= babeltrace_open_memstream(buf
, &size
);
1366 perror("Metadata open_memstream");
1370 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1379 /* close to flush the buffer */
1380 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1384 perror("babeltrace_flush_memstream");
1386 closeret
= fclose(in
);
1388 perror("Error in fclose");
1394 perror("Error in fclose");
1396 /* open for reading */
1397 buflen
= strlen(*buf
);
1402 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1404 perror("Metadata fmemopen");
1411 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1412 struct ctf_scanner
*scanner
, int append
)
1414 struct ctf_file_stream
*metadata_stream
;
1417 int ret
= 0, closeret
;
1419 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1420 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1424 metadata_stream
->pos
.fd
= -1;
1426 td
->metadata
= &metadata_stream
->parent
;
1427 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1428 if (metadata_stream
->pos
.fd
< 0) {
1429 fprintf(stderr
, "Unable to open metadata.\n");
1434 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1436 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1437 perror("Metadata stream open");
1441 /* fd now belongs to fp */
1442 metadata_stream
->pos
.fd
= -1;
1444 if (babeltrace_debug
)
1447 if (packet_metadata(td
, fp
)) {
1448 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1452 td
->metadata_string
= buf
;
1453 td
->metadata_packetized
= 1;
1456 unsigned int major
, minor
;
1459 td
->byte_order
= BYTE_ORDER
;
1461 /* Check text-only metadata header and version */
1462 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1464 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1465 if (check_version(major
, minor
) < 0) {
1473 ret
= ctf_scanner_append_ast(scanner
, fp
);
1475 fprintf(stderr
, "[error] Error creating AST\n");
1479 if (babeltrace_debug
) {
1480 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1482 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1487 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1489 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1492 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1493 td
, td
->byte_order
);
1495 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1500 closeret
= fclose(fp
);
1502 perror("Error on fclose");
1506 if (metadata_stream
->pos
.fd
>= 0) {
1507 closeret
= close(metadata_stream
->pos
.fd
);
1509 perror("Error on metadata stream fd close");
1514 g_free(metadata_stream
);
1519 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1520 struct ctf_stream_definition
*stream
,
1521 struct ctf_event_declaration
*event
)
1523 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1525 if (event
->context_decl
) {
1526 struct bt_definition
*definition
=
1527 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1528 stream
->parent_def_scope
, 0, 0, "event.context");
1532 stream_event
->event_context
= container_of(definition
,
1533 struct definition_struct
, p
);
1534 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1536 if (event
->fields_decl
) {
1537 struct bt_definition
*definition
=
1538 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1539 stream
->parent_def_scope
, 0, 0, "event.fields");
1543 stream_event
->event_fields
= container_of(definition
,
1544 struct definition_struct
, p
);
1545 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1547 stream_event
->stream
= stream
;
1548 return stream_event
;
1551 if (stream_event
->event_fields
)
1552 bt_definition_unref(&stream_event
->event_fields
->p
);
1553 if (stream_event
->event_context
)
1554 bt_definition_unref(&stream_event
->event_context
->p
);
1555 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1556 g_quark_to_string(event
->name
));
1561 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1562 struct ctf_stream_declaration
*stream_class
,
1563 struct ctf_stream_definition
*stream
)
1565 size_t def_size
, class_size
, i
;
1568 def_size
= stream
->events_by_id
->len
;
1569 class_size
= stream_class
->events_by_id
->len
;
1571 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1572 for (i
= def_size
; i
< class_size
; i
++) {
1573 struct ctf_event_declaration
*event
=
1574 g_ptr_array_index(stream_class
->events_by_id
, i
);
1575 struct ctf_event_definition
*stream_event
;
1579 stream_event
= create_event_definitions(td
, stream
, event
);
1580 if (!stream_event
) {
1584 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1591 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1593 struct ctf_stream_declaration
*stream_class
;
1597 if (stream
->stream_definitions_created
)
1600 stream_class
= stream
->stream_class
;
1602 if (stream_class
->packet_context_decl
) {
1603 struct bt_definition
*definition
=
1604 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1605 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1610 stream
->stream_packet_context
= container_of(definition
,
1611 struct definition_struct
, p
);
1612 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1614 if (stream_class
->event_header_decl
) {
1615 struct bt_definition
*definition
=
1616 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1617 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1622 stream
->stream_event_header
=
1623 container_of(definition
, struct definition_struct
, p
);
1624 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1626 if (stream_class
->event_context_decl
) {
1627 struct bt_definition
*definition
=
1628 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1629 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1634 stream
->stream_event_context
=
1635 container_of(definition
, struct definition_struct
, p
);
1636 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1638 stream
->events_by_id
= g_ptr_array_new();
1639 ret
= copy_event_declarations_stream_class_to_stream(td
,
1640 stream_class
, stream
);
1646 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1647 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1649 g_free(stream_event
);
1651 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1653 if (stream
->stream_event_context
)
1654 bt_definition_unref(&stream
->stream_event_context
->p
);
1655 if (stream
->stream_event_header
)
1656 bt_definition_unref(&stream
->stream_event_header
->p
);
1657 if (stream
->stream_packet_context
)
1658 bt_definition_unref(&stream
->stream_packet_context
->p
);
1659 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1660 stream_class
->stream_id
, strerror(-ret
));
1665 int stream_assign_class(struct ctf_trace
*td
,
1666 struct ctf_file_stream
*file_stream
,
1669 struct ctf_stream_declaration
*stream
;
1672 file_stream
->parent
.stream_id
= stream_id
;
1673 if (stream_id
>= td
->streams
->len
) {
1674 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1677 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1679 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1682 file_stream
->parent
.stream_class
= stream
;
1683 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1690 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1691 struct ctf_trace
*td
,
1692 struct ctf_file_stream
*file_stream
,
1695 struct packet_index packet_index
;
1696 uint64_t stream_id
= 0;
1697 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1698 int first_packet
= 0;
1703 memset(&packet_index
, 0, sizeof(packet_index
));
1704 if (!pos
->mmap_offset
) {
1708 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1709 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1712 if (pos
->base_mma
) {
1713 /* unmap old base */
1714 ret
= munmap_align(pos
->base_mma
);
1716 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1720 pos
->base_mma
= NULL
;
1722 /* map new base. Need mapping length from header. */
1723 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1724 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1725 assert(pos
->base_mma
!= MAP_FAILED
);
1727 * Use current mapping size as temporary content and packet
1730 pos
->content_size
= packet_map_len
;
1731 pos
->packet_size
= packet_map_len
;
1732 pos
->offset
= 0; /* Position of the packet header */
1734 packet_index
.offset
= pos
->mmap_offset
;
1736 /* read and check header, set stream id (and check) */
1737 if (file_stream
->parent
.trace_packet_header
) {
1738 /* Read packet header */
1739 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1743 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1746 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1747 if (len_index
>= 0) {
1748 struct bt_definition
*field
;
1751 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1752 magic
= bt_get_unsigned_int(field
);
1753 if (magic
!= CTF_MAGIC
) {
1754 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1756 file_stream
->pos
.packet_index
->len
,
1757 (ssize_t
) pos
->mmap_offset
);
1763 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1764 if (len_index
>= 0) {
1765 struct definition_array
*defarray
;
1766 struct bt_definition
*field
;
1768 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1770 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1771 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1772 defarray
= container_of(field
, struct definition_array
, p
);
1773 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1775 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1776 struct bt_definition
*elem
;
1778 elem
= bt_array_index(defarray
, i
);
1779 uuidval
[i
] = bt_get_unsigned_int(elem
);
1781 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1783 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1788 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1789 if (len_index
>= 0) {
1790 struct bt_definition
*field
;
1792 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1793 stream_id
= bt_get_unsigned_int(field
);
1797 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1798 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1800 file_stream
->parent
.stream_id
);
1804 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1809 if (file_stream
->parent
.stream_packet_context
) {
1810 /* Read packet context */
1811 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1815 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1818 /* read packet size from header */
1819 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1820 if (len_index
>= 0) {
1821 struct bt_definition
*field
;
1823 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1824 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1826 /* Use file size for packet size */
1827 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1830 /* read content size from header */
1831 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1832 if (len_index
>= 0) {
1833 struct bt_definition
*field
;
1835 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1836 packet_index
.content_size
= bt_get_unsigned_int(field
);
1838 /* Use packet size if non-zero, else file size */
1839 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1842 /* read timestamp begin from header */
1843 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1844 if (len_index
>= 0) {
1845 struct bt_definition
*field
;
1847 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1848 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1849 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1850 packet_index
.ts_real
.timestamp_begin
=
1851 ctf_get_real_timestamp(
1852 &file_stream
->parent
,
1853 packet_index
.ts_cycles
.timestamp_begin
);
1857 /* read timestamp end from header */
1858 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1859 if (len_index
>= 0) {
1860 struct bt_definition
*field
;
1862 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1863 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1864 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1865 packet_index
.ts_real
.timestamp_end
=
1866 ctf_get_real_timestamp(
1867 &file_stream
->parent
,
1868 packet_index
.ts_cycles
.timestamp_end
);
1872 /* read events discarded from header */
1873 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1874 if (len_index
>= 0) {
1875 struct bt_definition
*field
;
1877 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1878 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1879 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1882 /* read packet_seq_num from header */
1883 len_index
= bt_struct_declaration_lookup_field_index(
1884 file_stream
->parent
.stream_packet_context
->declaration
,
1885 g_quark_from_static_string("packet_seq_num"));
1886 if (len_index
>= 0) {
1887 struct bt_definition
*field
;
1889 field
= bt_struct_definition_get_field_from_index(
1890 file_stream
->parent
.stream_packet_context
,
1892 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1895 /* Use file size for packet size */
1896 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1897 /* Use packet size if non-zero, else file size */
1898 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1901 /* Validate content size and packet size values */
1902 if (packet_index
.content_size
> packet_index
.packet_size
) {
1903 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1904 packet_index
.content_size
, packet_index
.packet_size
);
1908 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1909 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1910 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1914 if (packet_index
.content_size
< pos
->offset
) {
1915 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1919 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1920 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1924 /* Save position after header and context */
1925 packet_index
.data_offset
= pos
->offset
;
1927 /* add index to packet array */
1928 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1930 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1934 /* Retry with larger mapping */
1936 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1938 * Reached EOF, but still expecting header/context data.
1940 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1943 /* Double the mapping len, and retry */
1944 tmp_map_len
= packet_map_len
<< 1;
1945 if (tmp_map_len
>> 1 != packet_map_len
) {
1947 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1950 packet_map_len
= tmp_map_len
;
1955 int create_stream_packet_index(struct ctf_trace
*td
,
1956 struct ctf_file_stream
*file_stream
)
1958 struct ctf_stream_pos
*pos
;
1959 struct stat filestats
;
1962 pos
= &file_stream
->pos
;
1964 ret
= fstat(pos
->fd
, &filestats
);
1968 /* Deal with empty files */
1969 if (!filestats
.st_size
) {
1970 if (file_stream
->parent
.trace_packet_header
1971 || file_stream
->parent
.stream_packet_context
) {
1973 * We expect a trace packet header and/or stream packet
1974 * context. Since a trace needs to have at least one
1975 * packet, empty files are therefore not accepted.
1977 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1981 * Without trace packet header nor stream packet
1982 * context, a one-packet trace can indeed be empty. This
1983 * is only valid if there is only one stream class: 0.
1985 ret
= stream_assign_class(td
, file_stream
, 0);
1992 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1993 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
2002 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
2006 if (td
->packet_header_decl
) {
2007 struct bt_definition
*definition
=
2008 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
2009 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
2014 stream
->trace_packet_header
=
2015 container_of(definition
, struct definition_struct
, p
);
2016 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
2022 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
2027 int import_stream_packet_index(struct ctf_trace
*td
,
2028 struct ctf_file_stream
*file_stream
)
2030 struct ctf_stream_pos
*pos
;
2031 struct ctf_packet_index
*ctf_index
= NULL
;
2032 struct ctf_packet_index_file_hdr index_hdr
;
2033 struct packet_index index
;
2034 uint32_t packet_index_len
, index_minor
;
2036 int first_packet
= 1;
2039 pos
= &file_stream
->pos
;
2041 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
2043 perror("read index file header");
2047 /* Check the index header */
2048 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
2049 fprintf(stderr
, "[error] wrong index magic\n");
2053 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
2054 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
2055 ".%" PRIu32
", supported %d.%d\n",
2056 be32toh(index_hdr
.index_major
),
2057 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
2062 index_minor
= be32toh(index_hdr
.index_minor
);
2064 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
2065 if (packet_index_len
== 0) {
2066 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2071 * Allocate the index length found in header, not internal
2074 ctf_index
= g_malloc0(packet_index_len
);
2075 while (fread(ctf_index
, packet_index_len
, 1,
2076 pos
->index_fp
) == 1) {
2078 struct ctf_stream_declaration
*stream
= NULL
;
2080 memset(&index
, 0, sizeof(index
));
2081 index
.offset
= be64toh(ctf_index
->offset
);
2082 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2083 index
.content_size
= be64toh(ctf_index
->content_size
);
2084 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2085 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2086 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2087 index
.events_discarded_len
= 64;
2088 index
.data_offset
= -1;
2089 stream_id
= be64toh(ctf_index
->stream_id
);
2090 if (index_minor
>= 1) {
2091 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2092 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2095 if (!first_packet
) {
2096 /* add index to packet array */
2097 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2101 file_stream
->parent
.stream_id
= stream_id
;
2102 if (stream_id
< td
->streams
->len
) {
2103 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2106 fprintf(stderr
, "[error] Stream %" PRIu64
2107 " is not declared in metadata.\n",
2112 file_stream
->parent
.stream_class
= stream
;
2113 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2117 /* add index to packet array */
2118 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2121 /* Index containing only the header. */
2122 if (!file_stream
->parent
.stream_class
) {
2135 * Note: many file streams can inherit from the same stream class
2136 * description (metadata).
2139 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2140 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2143 int ret
, fd
, closeret
;
2144 struct ctf_file_stream
*file_stream
;
2145 struct stat statbuf
;
2148 fd
= openat(td
->dirfd
, path
, flags
);
2150 perror("File stream openat()");
2155 /* Don't try to mmap subdirectories. Skip them, return success. */
2156 ret
= fstat(fd
, &statbuf
);
2158 perror("File stream fstat()");
2161 if (S_ISDIR(statbuf
.st_mode
)) {
2162 if (strncmp(path
, "index", 5) != 0) {
2163 fprintf(stderr
, "[warning] Skipping directory '%s' "
2164 "found in trace\n", path
);
2169 if (!statbuf
.st_size
) {
2170 /** Skip empty files. */
2172 goto fd_is_empty_file
;
2175 file_stream
= g_new0(struct ctf_file_stream
, 1);
2176 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2177 file_stream
->pos
.fd
= -1;
2178 file_stream
->pos
.index_fp
= NULL
;
2180 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2181 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2184 file_stream
->pos
.packet_seek
= packet_seek
;
2186 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2191 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2194 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2198 * For now, only a single clock per trace is supported.
2200 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2203 * Allocate the index name for this stream and try to open it.
2205 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2207 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2211 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2214 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2215 ret
= create_stream_packet_index(td
, file_stream
);
2217 fprintf(stderr
, "[error] Stream index creation error.\n");
2221 ret
= openat(td
->dirfd
, index_name
, flags
);
2223 perror("Index file openat()");
2227 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2228 if (!file_stream
->pos
.index_fp
) {
2229 perror("fdopen() error");
2232 ret
= import_stream_packet_index(td
, file_stream
);
2237 ret
= fclose(file_stream
->pos
.index_fp
);
2239 perror("close index");
2245 /* Add stream file to stream class */
2246 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2247 &file_stream
->parent
);
2251 if (file_stream
->pos
.index_fp
) {
2252 ret
= fclose(file_stream
->pos
.index_fp
);
2254 perror("close index");
2257 if (file_stream
->parent
.trace_packet_header
)
2258 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2262 closeret
= ctf_fini_pos(&file_stream
->pos
);
2264 fprintf(stderr
, "Error on ctf_fini_pos\n");
2266 g_free(file_stream
);
2270 closeret
= close(fd
);
2272 perror("Error on fd close");
2279 int ctf_open_trace_read(struct ctf_trace
*td
,
2280 const char *path
, int flags
,
2281 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2282 int whence
), FILE *metadata_fp
)
2284 struct ctf_scanner
*scanner
;
2286 struct dirent
*dirent
;
2287 struct dirent
*diriter
;
2293 /* Open trace directory */
2294 td
->dir
= opendir(path
);
2296 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2301 td
->dirfd
= open(path
, 0);
2302 if (td
->dirfd
< 0) {
2303 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2304 perror("Trace directory open");
2308 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2309 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2312 * Keep the metadata file separate.
2313 * Keep scanner object local to the open. We don't support
2314 * incremental metadata append for on-disk traces.
2316 scanner
= ctf_scanner_alloc();
2318 fprintf(stderr
, "[error] Error allocating scanner\n");
2320 goto error_metadata
;
2322 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2323 ctf_scanner_free(scanner
);
2325 if (ret
== -ENOENT
) {
2326 fprintf(stderr
, "[warning] Empty metadata.\n");
2328 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2329 goto error_metadata
;
2333 * Open each stream: for each file, try to open, check magic
2334 * number, and get the stream ID to add to the right location in
2338 dirent_len
= offsetof(struct dirent
, d_name
) +
2339 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
2341 dirent
= malloc(dirent_len
);
2344 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2346 fprintf(stderr
, "[error] Readdir error.\n");
2351 /* Ignore hidden files, ., .. and metadata. */
2352 if (!strncmp(diriter
->d_name
, ".", 1)
2353 || !strcmp(diriter
->d_name
, "..")
2354 || !strcmp(diriter
->d_name
, "metadata"))
2357 /* Ignore index files : *.idx */
2358 ext
= strrchr(diriter
->d_name
, '.');
2359 if (ext
&& (!strcmp(ext
, ".idx"))) {
2363 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2364 flags
, packet_seek
);
2366 fprintf(stderr
, "[error] Open file stream error.\n");
2377 closeret
= close(td
->dirfd
);
2379 perror("Error on fd close");
2382 closeret
= closedir(td
->dir
);
2384 perror("Error on closedir");
2391 * ctf_open_trace: Open a CTF trace and index it.
2392 * Note that the user must seek the trace after the open (using the iterator)
2393 * since the index creation read it entirely.
2396 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2397 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2398 int whence
), FILE *metadata_fp
)
2400 struct ctf_trace
*td
;
2404 * If packet_seek is NULL, we provide our default version.
2407 packet_seek
= ctf_packet_seek
;
2409 td
= g_new0(struct ctf_trace
, 1);
2413 init_trace_descriptor(&td
->parent
);
2415 switch (flags
& O_ACCMODE
) {
2418 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2421 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2426 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2429 fprintf(stderr
, "[error] Incorrect open flags.\n");
2433 ret
= trace_debug_info_create(td
);
2441 trace_debug_info_destroy(td
);
2448 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2449 struct bt_mmap_stream
*mmap_info
)
2451 pos
->mmap_offset
= 0;
2452 pos
->packet_size
= 0;
2453 pos
->content_size
= 0;
2454 pos
->content_size_loc
= NULL
;
2455 pos
->fd
= mmap_info
->fd
;
2456 pos
->base_mma
= NULL
;
2460 pos
->prot
= PROT_READ
;
2461 pos
->flags
= MAP_PRIVATE
;
2462 pos
->parent
.rw_table
= read_dispatch_table
;
2463 pos
->parent
.event_cb
= ctf_read_event
;
2464 pos
->priv
= mmap_info
->priv
;
2465 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2466 sizeof(struct packet_index
));
2470 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2471 struct ctf_file_stream
*file_stream
,
2472 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2475 struct ctf_stream_declaration
*stream
;
2479 /* Ask for the first packet to get the stream_id. */
2480 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2481 stream_id
= file_stream
->parent
.stream_id
;
2482 if (stream_id
>= td
->streams
->len
) {
2483 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2484 "in metadata.\n", stream_id
);
2488 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2490 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2491 "in metadata.\n", stream_id
);
2495 file_stream
->parent
.stream_class
= stream
;
2496 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2502 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2503 struct bt_mmap_stream
*mmap_info
,
2504 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2508 struct ctf_file_stream
*file_stream
;
2510 file_stream
= g_new0(struct ctf_file_stream
, 1);
2511 file_stream
->parent
.stream_id
= -1ULL;
2512 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2513 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2515 file_stream
->pos
.packet_seek
= packet_seek
;
2517 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2522 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2527 * For now, only a single clock per trace is supported.
2529 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2531 /* Add stream file to stream class */
2532 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2533 &file_stream
->parent
);
2537 if (file_stream
->parent
.trace_packet_header
)
2538 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2540 g_free(file_stream
);
2545 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2546 struct bt_mmap_stream_list
*mmap_list
,
2547 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2552 struct bt_mmap_stream
*mmap_info
;
2554 td
->scanner
= ctf_scanner_alloc();
2556 fprintf(stderr
, "[error] Error allocating scanner\n");
2560 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2562 if (ret
== -ENOENT
) {
2563 fprintf(stderr
, "[warning] Empty metadata.\n");
2569 * for each stream, try to open, check magic number, and get the
2570 * stream ID to add to the right location in the stream array.
2572 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2573 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2575 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2582 ctf_scanner_free(td
->scanner
);
2587 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2588 struct bt_mmap_stream_list
*mmap_list
,
2589 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2593 struct ctf_trace
*td
;
2597 fprintf(stderr
, "[error] No metadata file pointer associated, "
2598 "required for mmap parsing\n");
2602 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2605 td
= g_new0(struct ctf_trace
, 1);
2607 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2611 ret
= trace_debug_info_create(td
);
2624 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2627 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2633 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2636 /* for each stream_class */
2637 for (i
= 0; i
< td
->streams
->len
; i
++) {
2638 struct ctf_stream_declaration
*stream_class
;
2640 stream_class
= g_ptr_array_index(td
->streams
, i
);
2643 /* for each stream */
2644 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2645 struct ctf_stream_definition
*stream
;
2647 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2650 ret
= copy_event_declarations_stream_class_to_stream(td
,
2651 stream_class
, stream
);
2660 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2663 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2665 /* for each stream_class */
2666 for (i
= 0; i
< td
->streams
->len
; i
++) {
2667 struct ctf_stream_declaration
*stream_class
;
2669 stream_class
= g_ptr_array_index(td
->streams
, i
);
2672 /* for each file_stream */
2673 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2674 struct ctf_stream_definition
*stream
;
2675 struct ctf_stream_pos
*stream_pos
;
2676 struct ctf_file_stream
*cfs
;
2678 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2681 cfs
= container_of(stream
, struct ctf_file_stream
,
2683 stream_pos
= &cfs
->pos
;
2684 if (!stream_pos
->packet_index
)
2687 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2688 struct packet_index
*index
;
2690 index
= &g_array_index(stream_pos
->packet_index
,
2691 struct packet_index
, k
);
2692 index
->ts_real
.timestamp_begin
=
2693 ctf_get_real_timestamp(stream
,
2694 index
->ts_cycles
.timestamp_begin
);
2695 index
->ts_real
.timestamp_end
=
2696 ctf_get_real_timestamp(stream
,
2697 index
->ts_cycles
.timestamp_end
);
2705 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2709 ret
= ctf_fini_pos(&file_stream
->pos
);
2711 fprintf(stderr
, "Error on ctf_fini_pos\n");
2714 if (file_stream
->pos
.fd
>= 0) {
2715 ret
= close(file_stream
->pos
.fd
);
2717 perror("Error closing file fd");
2725 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2727 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2733 for (i
= 0; i
< td
->streams
->len
; i
++) {
2734 struct ctf_stream_declaration
*stream
;
2737 stream
= g_ptr_array_index(td
->streams
, i
);
2740 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2741 struct ctf_file_stream
*file_stream
;
2742 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2743 struct ctf_file_stream
, parent
);
2744 ret
= ctf_close_file_stream(file_stream
);
2750 ctf_destroy_metadata(td
);
2751 ctf_scanner_free(td
->scanner
);
2752 if (td
->dirfd
>= 0) {
2753 ret
= close(td
->dirfd
);
2755 perror("Error closing dirfd");
2760 ret
= closedir(td
->dir
);
2762 perror("Error closedir");
2766 free(td
->metadata_string
);
2767 trace_debug_info_destroy(td
);
2773 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2774 struct bt_context
*ctx
)
2776 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2779 td
->parent
.ctx
= ctx
;
2783 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2784 struct bt_trace_handle
*handle
)
2786 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2789 td
->parent
.handle
= handle
;
2793 void __attribute__((constructor
)) ctf_init(void)
2797 ctf_format
.name
= g_quark_from_static_string("ctf");
2798 ret
= bt_register_format(&ctf_format
);
2803 void __attribute__((destructor
)) ctf_exit(void)
2805 bt_unregister_format(&ctf_format
);