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
;
91 * TODO: babeltrace_ctf_console_output ensures that we only print
92 * discarded events when ctf-text plugin is used. Should be cleaned up
93 * with the plugin system redesign.
95 int babeltrace_ctf_console_output
;
98 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
99 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
103 struct bt_trace_descriptor
*ctf_open_mmap_trace(
104 struct bt_mmap_stream_list
*mmap_list
,
105 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
109 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
110 struct bt_context
*ctx
);
112 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
113 struct bt_trace_handle
*handle
);
116 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
118 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
119 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
121 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
122 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
124 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
127 rw_dispatch read_dispatch_table
[] = {
128 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
129 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
130 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
131 [ CTF_TYPE_STRING
] = ctf_string_read
,
132 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
133 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
134 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
135 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
139 rw_dispatch write_dispatch_table
[] = {
140 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
141 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
142 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
143 [ CTF_TYPE_STRING
] = ctf_string_write
,
144 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
145 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
146 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
147 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
151 struct bt_format ctf_format
= {
152 .open_trace
= ctf_open_trace
,
153 .open_mmap_trace
= ctf_open_mmap_trace
,
154 .close_trace
= ctf_close_trace
,
155 .set_context
= ctf_set_context
,
156 .set_handle
= ctf_set_handle
,
157 .timestamp_begin
= ctf_timestamp_begin
,
158 .timestamp_end
= ctf_timestamp_end
,
159 .convert_index_timestamp
= ctf_convert_index_timestamp
,
163 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
164 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
166 struct ctf_trace
*tin
;
167 uint64_t begin
= ULLONG_MAX
;
170 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
175 /* for each stream_class */
176 for (i
= 0; i
< tin
->streams
->len
; i
++) {
177 struct ctf_stream_declaration
*stream_class
;
179 stream_class
= g_ptr_array_index(tin
->streams
, i
);
182 /* for each file_stream */
183 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
184 struct ctf_stream_definition
*stream
;
185 struct ctf_file_stream
*cfs
;
186 struct ctf_stream_pos
*stream_pos
;
187 struct packet_index
*index
;
189 stream
= g_ptr_array_index(stream_class
->streams
, j
);
190 cfs
= container_of(stream
, struct ctf_file_stream
,
192 stream_pos
= &cfs
->pos
;
194 if (!stream_pos
->packet_index
)
197 if (stream_pos
->packet_index
->len
<= 0)
200 index
= &g_array_index(stream_pos
->packet_index
,
202 stream_pos
->packet_index
->len
- 1);
203 if (type
== BT_CLOCK_REAL
) {
204 if (index
->ts_real
.timestamp_begin
< begin
)
205 begin
= index
->ts_real
.timestamp_begin
;
206 } else if (type
== BT_CLOCK_CYCLES
) {
207 if (index
->ts_cycles
.timestamp_begin
< begin
)
208 begin
= index
->ts_cycles
.timestamp_begin
;
222 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
223 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
225 struct ctf_trace
*tin
;
229 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
234 /* for each stream_class */
235 for (i
= 0; i
< tin
->streams
->len
; i
++) {
236 struct ctf_stream_declaration
*stream_class
;
238 stream_class
= g_ptr_array_index(tin
->streams
, i
);
241 /* for each file_stream */
242 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
243 struct ctf_stream_definition
*stream
;
244 struct ctf_file_stream
*cfs
;
245 struct ctf_stream_pos
*stream_pos
;
246 struct packet_index
*index
;
248 stream
= g_ptr_array_index(stream_class
->streams
, j
);
249 cfs
= container_of(stream
, struct ctf_file_stream
,
251 stream_pos
= &cfs
->pos
;
253 if (!stream_pos
->packet_index
)
256 if (stream_pos
->packet_index
->len
<= 0)
259 index
= &g_array_index(stream_pos
->packet_index
,
261 stream_pos
->packet_index
->len
- 1);
262 if (type
== BT_CLOCK_REAL
) {
263 if (index
->ts_real
.timestamp_end
> end
)
264 end
= index
->ts_real
.timestamp_end
;
265 } else if (type
== BT_CLOCK_CYCLES
) {
266 if (index
->ts_cycles
.timestamp_end
> end
)
267 end
= index
->ts_cycles
.timestamp_end
;
281 * Update stream current timestamp
284 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
285 struct definition_integer
*integer_definition
)
287 struct declaration_integer
*integer_declaration
=
288 integer_definition
->declaration
;
289 uint64_t oldval
, newval
, updateval
;
291 if (unlikely(integer_declaration
->len
== 64)) {
292 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
293 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
294 stream
->cycles_timestamp
);
298 oldval
= stream
->cycles_timestamp
;
299 oldval
&= (1ULL << integer_declaration
->len
) - 1;
300 newval
= integer_definition
->value
._unsigned
;
301 /* Test for overflow by comparing low bits */
303 newval
+= 1ULL << integer_declaration
->len
;
304 /* updateval contains old high bits, and new low bits (sum) */
305 updateval
= stream
->cycles_timestamp
;
306 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
308 stream
->cycles_timestamp
= updateval
;
310 /* convert to real timestamp */
311 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
312 stream
->cycles_timestamp
);
316 * Print timestamp, rescaling clock frequency to nanoseconds and
317 * applying offsets as needed (unix time).
320 void ctf_print_timestamp_real(FILE *fp
,
321 struct ctf_stream_definition
*stream
,
324 uint64_t ts_sec
= 0, ts_nsec
;
328 /* Add command-line offset in ns*/
329 ts_nsec
+= opt_clock_offset_ns
;
331 /* Add command-line offset */
332 ts_sec
+= opt_clock_offset
;
334 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
335 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
337 if (!opt_clock_seconds
) {
339 time_t time_s
= (time_t) ts_sec
;
341 if (!opt_clock_gmt
) {
344 res
= localtime_r(&time_s
, &tm
);
346 fprintf(stderr
, "[warning] Unable to get localtime.\n");
352 res
= gmtime_r(&time_s
, &tm
);
354 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
358 if (opt_clock_date
) {
362 /* Print date and time */
363 res
= strftime(timestr
, sizeof(timestr
),
366 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
369 fprintf(fp
, "%s", timestr
);
371 /* Print time in HH:MM:SS.ns */
372 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
373 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
377 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
385 * Print timestamp, in cycles
388 void ctf_print_timestamp_cycles(FILE *fp
,
389 struct ctf_stream_definition
*stream
,
392 fprintf(fp
, "%020" PRIu64
, timestamp
);
395 void ctf_print_timestamp(FILE *fp
,
396 struct ctf_stream_definition
*stream
,
399 if (opt_clock_cycles
) {
400 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
402 ctf_print_timestamp_real(fp
, stream
, timestamp
);
407 void print_uuid(FILE *fp
, unsigned char *uuid
)
411 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
412 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
416 * Discarded events can be either:
417 * - discarded after end of previous buffer due to buffer full:
418 * happened within range: [ prev_timestamp_end, timestamp_begin ]
419 * - discarded within current buffer due to either event too large or
420 * nested wrap-around:
421 * happened within range: [ timestamp_begin, timestamp_end ]
423 * Given we have discarded counters of those two types merged into the
424 * events_discarded counter, we need to use the union of those ranges:
425 * [ prev_timestamp_end, timestamp_end ]
427 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
428 * consumer had time to extract them. We keep track of those gaps with the
429 * packet sequence number in each packet.
432 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
434 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
435 !babeltrace_ctf_console_output
) {
439 if (stream
->events_discarded
) {
440 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
441 stream
->events_discarded
);
442 } else if (stream
->packets_lost
) {
443 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
444 stream
->packets_lost
);
446 if (opt_clock_cycles
) {
447 ctf_print_timestamp(fp
, stream
,
448 stream
->prev
.cycles
.end
);
449 fprintf(fp
, "] and [");
450 ctf_print_timestamp(fp
, stream
,
451 stream
->current
.cycles
.end
);
453 ctf_print_timestamp(fp
, stream
,
454 stream
->prev
.real
.end
);
455 fprintf(fp
, "] and [");
456 ctf_print_timestamp(fp
, stream
,
457 stream
->current
.real
.end
);
459 fprintf(fp
, "] in trace UUID ");
460 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
461 if (stream
->stream_class
->trace
->parent
.path
[0])
462 fprintf(fp
, ", at path: \"%s\"",
463 stream
->stream_class
->trace
->parent
.path
);
465 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
467 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
469 fprintf(fp
, "You should consider recording a new trace with larger "
470 "buffers or with fewer events enabled.\n");
475 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
477 struct ctf_stream_pos
*pos
=
478 container_of(ppos
, struct ctf_stream_pos
, parent
);
479 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
480 struct ctf_event_definition
*event
;
484 /* We need to check for EOF here for empty files. */
485 if (unlikely(pos
->offset
== EOF
))
488 ctf_pos_get_event(pos
);
490 /* save the current position as a restore point */
491 pos
->last_offset
= pos
->offset
;
494 * This is the EOF check after we've advanced the position in
497 if (unlikely(pos
->offset
== EOF
))
500 /* Stream is inactive for now (live reading). */
501 if (unlikely(pos
->content_size
== 0))
505 * Packet seeked to by ctf_pos_get_event() only contains
506 * headers, no event. Consider stream as inactive (live
509 if (unlikely(pos
->data_offset
== pos
->content_size
))
512 assert(pos
->offset
< pos
->content_size
);
514 /* Read event header */
515 if (likely(stream
->stream_event_header
)) {
516 struct definition_integer
*integer_definition
;
517 struct bt_definition
*variant
;
519 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
522 /* lookup event id */
523 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
524 if (integer_definition
) {
525 id
= integer_definition
->value
._unsigned
;
527 struct definition_enum
*enum_definition
;
529 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
530 if (enum_definition
) {
531 id
= enum_definition
->integer
->value
._unsigned
;
535 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
537 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
538 if (integer_definition
) {
539 id
= integer_definition
->value
._unsigned
;
542 stream
->event_id
= id
;
544 /* lookup timestamp */
545 stream
->has_timestamp
= 0;
546 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
547 if (integer_definition
) {
548 ctf_update_timestamp(stream
, integer_definition
);
549 stream
->has_timestamp
= 1;
552 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
553 if (integer_definition
) {
554 ctf_update_timestamp(stream
, integer_definition
);
555 stream
->has_timestamp
= 1;
561 /* Read stream-declared event context */
562 if (stream
->stream_event_context
) {
563 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
568 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
569 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
572 event
= g_ptr_array_index(stream
->events_by_id
, id
);
573 if (unlikely(!event
)) {
574 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
578 /* Read event-declared event context */
579 if (event
->event_context
) {
580 ret
= generic_rw(ppos
, &event
->event_context
->p
);
585 /* Read event payload */
586 if (likely(event
->event_fields
)) {
587 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
592 if (pos
->last_offset
== pos
->offset
) {
593 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
600 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
605 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
607 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
608 struct ctf_event_definition
*event
;
612 id
= stream
->event_id
;
614 /* print event header */
615 if (likely(stream
->stream_event_header
)) {
616 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
621 /* print stream-declared event context */
622 if (stream
->stream_event_context
) {
623 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
628 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
629 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
632 event
= g_ptr_array_index(stream
->events_by_id
, id
);
633 if (unlikely(!event
)) {
634 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
638 /* print event-declared event context */
639 if (event
->event_context
) {
640 ret
= generic_rw(pos
, &event
->event_context
->p
);
645 /* Read and print event payload */
646 if (likely(event
->event_fields
)) {
647 ret
= generic_rw(pos
, &event
->event_fields
->p
);
655 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
660 * One side-effect of this function is to unmap pos mmap base if one is
664 int find_data_offset(struct ctf_stream_pos
*pos
,
665 struct ctf_file_stream
*file_stream
,
666 struct packet_index
*packet_index
)
668 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
669 struct stat filestats
;
673 pos
= &file_stream
->pos
;
675 ret
= fstat(pos
->fd
, &filestats
);
678 filesize
= filestats
.st_size
;
680 /* Deal with empty files */
686 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
687 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
692 ret
= munmap_align(pos
->base_mma
);
694 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
698 pos
->base_mma
= NULL
;
700 /* map new base. Need mapping length from header. */
701 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
702 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
703 assert(pos
->base_mma
!= MAP_FAILED
);
705 pos
->content_size
= packet_map_len
;
706 pos
->packet_size
= packet_map_len
;
707 pos
->offset
= 0; /* Position of the packet header */
709 /* update trace_packet_header and stream_packet_context */
710 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
711 /* Read packet header */
712 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
718 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
719 /* Read packet context */
720 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
726 packet_index
->data_offset
= pos
->offset
;
729 ret
= munmap_align(pos
->base_mma
);
731 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
735 pos
->base_mma
= NULL
;
739 /* Retry with larger mapping */
741 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
743 * Reached EOF, but still expecting header/context data.
745 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
748 /* Double the mapping len, and retry */
749 tmp_map_len
= packet_map_len
<< 1;
750 if (tmp_map_len
>> 1 != packet_map_len
) {
752 fprintf(stderr
, "[error] Packet mapping length overflow\n");
755 packet_map_len
= tmp_map_len
;
760 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
761 int fd
, int open_flags
)
765 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
766 sizeof(struct packet_index
));
768 pos
->packet_index
= NULL
;
770 switch (open_flags
& O_ACCMODE
) {
772 pos
->prot
= PROT_READ
;
773 pos
->flags
= MAP_PRIVATE
;
774 pos
->parent
.rw_table
= read_dispatch_table
;
775 pos
->parent
.event_cb
= ctf_read_event
;
776 pos
->parent
.trace
= trace
;
779 pos
->prot
= PROT_READ
| PROT_WRITE
;
780 pos
->flags
= MAP_SHARED
;
781 pos
->parent
.rw_table
= write_dispatch_table
;
782 pos
->parent
.event_cb
= ctf_write_event
;
783 pos
->parent
.trace
= trace
;
785 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
793 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
795 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
796 *pos
->content_size_loc
= pos
->offset
;
801 ret
= munmap_align(pos
->base_mma
);
803 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
808 if (pos
->packet_index
)
809 (void) g_array_free(pos
->packet_index
, TRUE
);
813 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
814 struct packet_index
*prev_index
,
815 struct packet_index
*cur_index
)
817 uint64_t events_discarded_diff
;
818 uint64_t packets_lost_diff
= 0;
820 /* Update packet index time information */
822 /* Current packet begin/end */
823 stream
->current
.real
.begin
=
824 cur_index
->ts_real
.timestamp_begin
;
825 stream
->current
.cycles
.begin
=
826 cur_index
->ts_cycles
.timestamp_begin
;
827 stream
->current
.real
.end
=
828 cur_index
->ts_real
.timestamp_end
;
829 stream
->current
.cycles
.end
=
830 cur_index
->ts_cycles
.timestamp_end
;
832 /* Update packet index discarded event information */
833 events_discarded_diff
= cur_index
->events_discarded
;
835 /* Previous packet begin/end */
836 stream
->prev
.cycles
.begin
=
837 prev_index
->ts_cycles
.timestamp_begin
;
838 stream
->prev
.real
.begin
=
839 prev_index
->ts_real
.timestamp_begin
;
840 stream
->prev
.cycles
.end
=
841 prev_index
->ts_cycles
.timestamp_end
;
842 stream
->prev
.real
.end
=
843 prev_index
->ts_real
.timestamp_end
;
845 events_discarded_diff
-= prev_index
->events_discarded
;
846 /* packet_seq_num stays at 0 if not produced by the tracer */
847 if (cur_index
->packet_seq_num
) {
848 packets_lost_diff
= cur_index
->packet_seq_num
-
849 prev_index
->packet_seq_num
- 1;
852 * Deal with 32-bit wrap-around if the tracer provided a
855 if (prev_index
->events_discarded_len
== 32) {
856 events_discarded_diff
= (uint32_t) events_discarded_diff
;
860 * First packet: use current packet info as limits for
863 stream
->prev
.cycles
.begin
=
864 stream
->prev
.cycles
.end
=
865 stream
->current
.cycles
.begin
;
866 stream
->prev
.real
.begin
=
867 stream
->prev
.real
.end
=
868 stream
->current
.real
.begin
;
870 stream
->events_discarded
= events_discarded_diff
;
871 stream
->packets_lost
= packets_lost_diff
;
875 * Find the timerange where all the streams in the trace are active
878 * Return 0 and update begin/end if necessary on success, return 1 for
879 * empty streams and return a negative value on error.
882 int ctf_intersect_trace(struct bt_trace_descriptor
*td_read
,
883 uint64_t *begin
, uint64_t *end
)
885 struct ctf_trace
*tin
;
886 int stream_id
, ret
= 0;
888 tin
= container_of(td_read
, struct ctf_trace
, parent
);
890 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
893 struct ctf_stream_declaration
*stream_class
;
895 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
899 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
900 struct ctf_file_stream
*file_stream
;
901 struct ctf_stream_pos
*stream_pos
;
902 struct packet_index
*index
;
904 file_stream
= g_ptr_array_index(stream_class
->streams
,
909 stream_pos
= &file_stream
->pos
;
910 if (!stream_pos
->packet_index
||
911 stream_pos
->packet_index
->len
<= 0) {
915 index
= &g_array_index(stream_pos
->packet_index
,
916 struct packet_index
, 0);
917 if (index
->ts_real
.timestamp_begin
> *begin
) {
918 *begin
= index
->ts_real
.timestamp_begin
;
920 index
= &g_array_index(stream_pos
->packet_index
,
922 stream_pos
->packet_index
->len
- 1);
923 if (index
->ts_real
.timestamp_end
< *end
) {
924 *end
= index
->ts_real
.timestamp_end
;
934 * Find the timerange where all streams in the trace collection are active
937 * Return 0 on success.
938 * Return 1 if no intersections are found.
939 * Return a negative value on error.
941 int ctf_find_packets_intersection(struct bt_context
*ctx
,
942 uint64_t *ts_begin
, uint64_t *ts_end
)
946 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
951 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
952 struct bt_trace_descriptor
*td_read
;
954 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
958 ret
= ctf_intersect_trace(td_read
, ts_begin
, ts_end
);
963 if (*ts_end
< *ts_begin
) {
973 * for SEEK_CUR: go to next packet.
974 * for SEEK_SET: go to packet numer (index).
976 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
978 struct ctf_stream_pos
*pos
=
979 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
980 struct ctf_file_stream
*file_stream
=
981 container_of(pos
, struct ctf_file_stream
, pos
);
983 struct packet_index
*packet_index
, *prev_index
;
987 case SEEK_SET
: /* Fall-through */
993 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
994 *pos
->content_size_loc
= pos
->offset
;
998 ret
= munmap_align(pos
->base_mma
);
1000 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1004 pos
->base_mma
= NULL
;
1008 * The caller should never ask for ctf_move_pos across packets,
1009 * except to get exactly at the beginning of the next packet.
1011 if (pos
->prot
& PROT_WRITE
) {
1014 /* The writer will add padding */
1015 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1018 assert(index
== 0); /* only seek supported for now */
1024 pos
->content_size
= -1U; /* Unknown at this point */
1025 pos
->packet_size
= WRITE_PACKET_LEN
;
1027 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1028 pos
->packet_size
/ CHAR_BIT
);
1029 } while (ret
== EINTR
);
1037 if (pos
->offset
== EOF
) {
1040 assert(pos
->cur_index
< pos
->packet_index
->len
);
1041 /* The reader will expect us to skip padding */
1046 if (index
>= pos
->packet_index
->len
) {
1050 pos
->cur_index
= index
;
1056 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1061 packet_index
= &g_array_index(pos
->packet_index
,
1062 struct packet_index
, pos
->cur_index
);
1063 if (pos
->cur_index
> 0) {
1064 prev_index
= &g_array_index(pos
->packet_index
,
1065 struct packet_index
,
1066 pos
->cur_index
- 1);
1070 ctf_update_current_packet_index(&file_stream
->parent
,
1071 prev_index
, packet_index
);
1074 * We need to check if we are in trace read or called
1075 * from packet indexing. In this last case, the
1076 * collection is not there, so we cannot print the
1079 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1080 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1083 packet_index
= &g_array_index(pos
->packet_index
,
1084 struct packet_index
,
1086 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1088 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1090 /* Lookup context/packet size in index */
1091 if (packet_index
->data_offset
== -1) {
1092 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1097 pos
->content_size
= packet_index
->content_size
;
1098 pos
->packet_size
= packet_index
->packet_size
;
1099 pos
->mmap_offset
= packet_index
->offset
;
1100 pos
->data_offset
= packet_index
->data_offset
;
1101 if (pos
->data_offset
< packet_index
->content_size
) {
1102 pos
->offset
= 0; /* will read headers */
1103 } else if (pos
->data_offset
== packet_index
->content_size
) {
1105 pos
->offset
= packet_index
->data_offset
;
1107 goto read_next_packet
;
1113 /* map new base. Need mapping length from header. */
1114 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1115 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1116 if (pos
->base_mma
== MAP_FAILED
) {
1117 fprintf(stderr
, "[error] mmap error %s.\n",
1122 /* update trace_packet_header and stream_packet_context */
1123 if (!(pos
->prot
& PROT_WRITE
) &&
1124 file_stream
->parent
.trace_packet_header
) {
1125 /* Read packet header */
1126 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1129 if (!(pos
->prot
& PROT_WRITE
) &&
1130 file_stream
->parent
.stream_packet_context
) {
1131 /* Read packet context */
1132 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1138 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1144 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1148 if (magic
== TSDL_MAGIC
) {
1150 td
->byte_order
= BYTE_ORDER
;
1151 CTF_TRACE_SET_FIELD(td
, byte_order
);
1152 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1154 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1155 LITTLE_ENDIAN
: BIG_ENDIAN
;
1156 CTF_TRACE_SET_FIELD(td
, byte_order
);
1164 * Returns 0 on success, -1 on error.
1167 int check_version(unsigned int major
, unsigned int minor
)
1182 /* eventually return an error instead of warning */
1184 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1190 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1193 struct metadata_packet_header header
;
1194 size_t readlen
, writelen
, toread
;
1195 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1198 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1202 if (td
->byte_order
!= BYTE_ORDER
) {
1203 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1204 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1205 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1206 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1208 if (header
.checksum
)
1209 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1210 if (header
.compression_scheme
) {
1211 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1212 header
.compression_scheme
);
1215 if (header
.encryption_scheme
) {
1216 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1217 header
.encryption_scheme
);
1220 if (header
.checksum_scheme
) {
1221 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1222 header
.checksum_scheme
);
1225 if (check_version(header
.major
, header
.minor
) < 0)
1227 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1228 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1229 CTF_TRACE_SET_FIELD(td
, uuid
);
1231 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1235 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1238 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1241 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1246 if (babeltrace_debug
) {
1247 buf
[readlen
] = '\0';
1248 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1252 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1253 if (writelen
< readlen
) {
1263 ret
= 0; /* continue reading next packet */
1270 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1271 ret
= fseek(in
, toread
, SEEK_CUR
);
1273 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1280 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1284 size_t size
, buflen
;
1289 * Using strlen on *buf instead of size of open_memstream
1290 * because its size includes garbage at the end (after final
1291 * \0). This is the allocated size, not the actual string size.
1293 out
= babeltrace_open_memstream(buf
, &size
);
1295 perror("Metadata open_memstream");
1299 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1308 /* close to flush the buffer */
1309 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1313 perror("babeltrace_flush_memstream");
1315 closeret
= fclose(in
);
1317 perror("Error in fclose");
1323 perror("Error in fclose");
1325 /* open for reading */
1326 buflen
= strlen(*buf
);
1331 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1333 perror("Metadata fmemopen");
1340 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1341 struct ctf_scanner
*scanner
, int append
)
1343 struct ctf_file_stream
*metadata_stream
;
1346 int ret
= 0, closeret
;
1348 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1349 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1353 metadata_stream
->pos
.fd
= -1;
1355 td
->metadata
= &metadata_stream
->parent
;
1356 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1357 if (metadata_stream
->pos
.fd
< 0) {
1358 fprintf(stderr
, "Unable to open metadata.\n");
1363 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1365 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1366 perror("Metadata stream open");
1370 /* fd now belongs to fp */
1371 metadata_stream
->pos
.fd
= -1;
1373 if (babeltrace_debug
)
1376 if (packet_metadata(td
, fp
)) {
1377 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1381 td
->metadata_string
= buf
;
1382 td
->metadata_packetized
= 1;
1385 unsigned int major
, minor
;
1388 td
->byte_order
= BYTE_ORDER
;
1390 /* Check text-only metadata header and version */
1391 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1393 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1394 if (check_version(major
, minor
) < 0) {
1402 ret
= ctf_scanner_append_ast(scanner
, fp
);
1404 fprintf(stderr
, "[error] Error creating AST\n");
1408 if (babeltrace_debug
) {
1409 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1411 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1416 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1418 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1421 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1422 td
, td
->byte_order
);
1424 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1429 closeret
= fclose(fp
);
1431 perror("Error on fclose");
1435 if (metadata_stream
->pos
.fd
>= 0) {
1436 closeret
= close(metadata_stream
->pos
.fd
);
1438 perror("Error on metadata stream fd close");
1443 g_free(metadata_stream
);
1448 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1449 struct ctf_stream_definition
*stream
,
1450 struct ctf_event_declaration
*event
)
1452 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1454 if (event
->context_decl
) {
1455 struct bt_definition
*definition
=
1456 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1457 stream
->parent_def_scope
, 0, 0, "event.context");
1461 stream_event
->event_context
= container_of(definition
,
1462 struct definition_struct
, p
);
1463 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1465 if (event
->fields_decl
) {
1466 struct bt_definition
*definition
=
1467 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1468 stream
->parent_def_scope
, 0, 0, "event.fields");
1472 stream_event
->event_fields
= container_of(definition
,
1473 struct definition_struct
, p
);
1474 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1476 stream_event
->stream
= stream
;
1477 return stream_event
;
1480 if (stream_event
->event_fields
)
1481 bt_definition_unref(&stream_event
->event_fields
->p
);
1482 if (stream_event
->event_context
)
1483 bt_definition_unref(&stream_event
->event_context
->p
);
1484 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1485 g_quark_to_string(event
->name
));
1490 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1491 struct ctf_stream_declaration
*stream_class
,
1492 struct ctf_stream_definition
*stream
)
1494 size_t def_size
, class_size
, i
;
1497 def_size
= stream
->events_by_id
->len
;
1498 class_size
= stream_class
->events_by_id
->len
;
1500 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1501 for (i
= def_size
; i
< class_size
; i
++) {
1502 struct ctf_event_declaration
*event
=
1503 g_ptr_array_index(stream_class
->events_by_id
, i
);
1504 struct ctf_event_definition
*stream_event
;
1508 stream_event
= create_event_definitions(td
, stream
, event
);
1509 if (!stream_event
) {
1513 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1520 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1522 struct ctf_stream_declaration
*stream_class
;
1526 if (stream
->stream_definitions_created
)
1529 stream_class
= stream
->stream_class
;
1531 if (stream_class
->packet_context_decl
) {
1532 struct bt_definition
*definition
=
1533 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1534 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1539 stream
->stream_packet_context
= container_of(definition
,
1540 struct definition_struct
, p
);
1541 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1543 if (stream_class
->event_header_decl
) {
1544 struct bt_definition
*definition
=
1545 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1546 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1551 stream
->stream_event_header
=
1552 container_of(definition
, struct definition_struct
, p
);
1553 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1555 if (stream_class
->event_context_decl
) {
1556 struct bt_definition
*definition
=
1557 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1558 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1563 stream
->stream_event_context
=
1564 container_of(definition
, struct definition_struct
, p
);
1565 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1567 stream
->events_by_id
= g_ptr_array_new();
1568 ret
= copy_event_declarations_stream_class_to_stream(td
,
1569 stream_class
, stream
);
1575 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1576 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1578 g_free(stream_event
);
1580 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1582 if (stream
->stream_event_context
)
1583 bt_definition_unref(&stream
->stream_event_context
->p
);
1584 if (stream
->stream_event_header
)
1585 bt_definition_unref(&stream
->stream_event_header
->p
);
1586 if (stream
->stream_packet_context
)
1587 bt_definition_unref(&stream
->stream_packet_context
->p
);
1588 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1589 stream_class
->stream_id
, strerror(-ret
));
1594 int stream_assign_class(struct ctf_trace
*td
,
1595 struct ctf_file_stream
*file_stream
,
1598 struct ctf_stream_declaration
*stream
;
1601 file_stream
->parent
.stream_id
= stream_id
;
1602 if (stream_id
>= td
->streams
->len
) {
1603 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1606 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1608 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1611 file_stream
->parent
.stream_class
= stream
;
1612 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1619 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1620 struct ctf_trace
*td
,
1621 struct ctf_file_stream
*file_stream
,
1624 struct packet_index packet_index
;
1625 uint64_t stream_id
= 0;
1626 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1627 int first_packet
= 0;
1632 memset(&packet_index
, 0, sizeof(packet_index
));
1633 if (!pos
->mmap_offset
) {
1637 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1638 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1641 if (pos
->base_mma
) {
1642 /* unmap old base */
1643 ret
= munmap_align(pos
->base_mma
);
1645 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1649 pos
->base_mma
= NULL
;
1651 /* map new base. Need mapping length from header. */
1652 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1653 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1654 assert(pos
->base_mma
!= MAP_FAILED
);
1656 * Use current mapping size as temporary content and packet
1659 pos
->content_size
= packet_map_len
;
1660 pos
->packet_size
= packet_map_len
;
1661 pos
->offset
= 0; /* Position of the packet header */
1663 packet_index
.offset
= pos
->mmap_offset
;
1665 /* read and check header, set stream id (and check) */
1666 if (file_stream
->parent
.trace_packet_header
) {
1667 /* Read packet header */
1668 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1672 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1675 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1676 if (len_index
>= 0) {
1677 struct bt_definition
*field
;
1680 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1681 magic
= bt_get_unsigned_int(field
);
1682 if (magic
!= CTF_MAGIC
) {
1683 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1685 file_stream
->pos
.packet_index
->len
,
1686 (ssize_t
) pos
->mmap_offset
);
1692 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1693 if (len_index
>= 0) {
1694 struct definition_array
*defarray
;
1695 struct bt_definition
*field
;
1697 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1699 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1700 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1701 defarray
= container_of(field
, struct definition_array
, p
);
1702 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1704 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1705 struct bt_definition
*elem
;
1707 elem
= bt_array_index(defarray
, i
);
1708 uuidval
[i
] = bt_get_unsigned_int(elem
);
1710 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1712 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1717 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1718 if (len_index
>= 0) {
1719 struct bt_definition
*field
;
1721 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1722 stream_id
= bt_get_unsigned_int(field
);
1726 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1727 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1729 file_stream
->parent
.stream_id
);
1733 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1738 if (file_stream
->parent
.stream_packet_context
) {
1739 /* Read packet context */
1740 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1744 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1747 /* read packet size from header */
1748 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1749 if (len_index
>= 0) {
1750 struct bt_definition
*field
;
1752 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1753 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1755 /* Use file size for packet size */
1756 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1759 /* read content size from header */
1760 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1761 if (len_index
>= 0) {
1762 struct bt_definition
*field
;
1764 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1765 packet_index
.content_size
= bt_get_unsigned_int(field
);
1767 /* Use packet size if non-zero, else file size */
1768 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1771 /* read timestamp begin from header */
1772 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1773 if (len_index
>= 0) {
1774 struct bt_definition
*field
;
1776 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1777 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1778 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1779 packet_index
.ts_real
.timestamp_begin
=
1780 ctf_get_real_timestamp(
1781 &file_stream
->parent
,
1782 packet_index
.ts_cycles
.timestamp_begin
);
1786 /* read timestamp end from header */
1787 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1788 if (len_index
>= 0) {
1789 struct bt_definition
*field
;
1791 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1792 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1793 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1794 packet_index
.ts_real
.timestamp_end
=
1795 ctf_get_real_timestamp(
1796 &file_stream
->parent
,
1797 packet_index
.ts_cycles
.timestamp_end
);
1801 /* read events discarded from header */
1802 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1803 if (len_index
>= 0) {
1804 struct bt_definition
*field
;
1806 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1807 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1808 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1811 /* read packet_seq_num from header */
1812 len_index
= bt_struct_declaration_lookup_field_index(
1813 file_stream
->parent
.stream_packet_context
->declaration
,
1814 g_quark_from_static_string("packet_seq_num"));
1815 if (len_index
>= 0) {
1816 struct bt_definition
*field
;
1818 field
= bt_struct_definition_get_field_from_index(
1819 file_stream
->parent
.stream_packet_context
,
1821 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1824 /* Use file size for packet size */
1825 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1826 /* Use packet size if non-zero, else file size */
1827 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1830 /* Validate content size and packet size values */
1831 if (packet_index
.content_size
> packet_index
.packet_size
) {
1832 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1833 packet_index
.content_size
, packet_index
.packet_size
);
1837 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1838 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1839 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1843 if (packet_index
.content_size
< pos
->offset
) {
1844 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1848 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1849 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1853 /* Save position after header and context */
1854 packet_index
.data_offset
= pos
->offset
;
1856 /* add index to packet array */
1857 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1859 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1863 /* Retry with larger mapping */
1865 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1867 * Reached EOF, but still expecting header/context data.
1869 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1872 /* Double the mapping len, and retry */
1873 tmp_map_len
= packet_map_len
<< 1;
1874 if (tmp_map_len
>> 1 != packet_map_len
) {
1876 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1879 packet_map_len
= tmp_map_len
;
1884 int create_stream_packet_index(struct ctf_trace
*td
,
1885 struct ctf_file_stream
*file_stream
)
1887 struct ctf_stream_pos
*pos
;
1888 struct stat filestats
;
1891 pos
= &file_stream
->pos
;
1893 ret
= fstat(pos
->fd
, &filestats
);
1897 /* Deal with empty files */
1898 if (!filestats
.st_size
) {
1899 if (file_stream
->parent
.trace_packet_header
1900 || file_stream
->parent
.stream_packet_context
) {
1902 * We expect a trace packet header and/or stream packet
1903 * context. Since a trace needs to have at least one
1904 * packet, empty files are therefore not accepted.
1906 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1910 * Without trace packet header nor stream packet
1911 * context, a one-packet trace can indeed be empty. This
1912 * is only valid if there is only one stream class: 0.
1914 ret
= stream_assign_class(td
, file_stream
, 0);
1921 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1922 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1931 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1935 if (td
->packet_header_decl
) {
1936 struct bt_definition
*definition
=
1937 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1938 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1943 stream
->trace_packet_header
=
1944 container_of(definition
, struct definition_struct
, p
);
1945 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1951 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1956 int import_stream_packet_index(struct ctf_trace
*td
,
1957 struct ctf_file_stream
*file_stream
)
1959 struct ctf_stream_pos
*pos
;
1960 struct ctf_packet_index
*ctf_index
= NULL
;
1961 struct ctf_packet_index_file_hdr index_hdr
;
1962 struct packet_index index
;
1963 uint32_t packet_index_len
, index_minor
;
1965 int first_packet
= 1;
1968 pos
= &file_stream
->pos
;
1970 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
1972 perror("read index file header");
1976 /* Check the index header */
1977 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
1978 fprintf(stderr
, "[error] wrong index magic\n");
1982 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
1983 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
1984 ".%" PRIu32
", supported %d.%d\n",
1985 be32toh(index_hdr
.index_major
),
1986 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
1991 index_minor
= be32toh(index_hdr
.index_minor
);
1993 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
1994 if (packet_index_len
== 0) {
1995 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2000 * Allocate the index length found in header, not internal
2003 ctf_index
= g_malloc0(packet_index_len
);
2004 while (fread(ctf_index
, packet_index_len
, 1,
2005 pos
->index_fp
) == 1) {
2007 struct ctf_stream_declaration
*stream
= NULL
;
2009 memset(&index
, 0, sizeof(index
));
2010 index
.offset
= be64toh(ctf_index
->offset
);
2011 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2012 index
.content_size
= be64toh(ctf_index
->content_size
);
2013 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2014 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2015 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2016 index
.events_discarded_len
= 64;
2017 index
.data_offset
= -1;
2018 stream_id
= be64toh(ctf_index
->stream_id
);
2019 if (index_minor
>= 1) {
2020 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2021 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2024 if (!first_packet
) {
2025 /* add index to packet array */
2026 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2030 file_stream
->parent
.stream_id
= stream_id
;
2031 if (stream_id
< td
->streams
->len
) {
2032 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2035 fprintf(stderr
, "[error] Stream %" PRIu64
2036 " is not declared in metadata.\n",
2041 file_stream
->parent
.stream_class
= stream
;
2042 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2046 /* add index to packet array */
2047 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2050 /* Index containing only the header. */
2051 if (!file_stream
->parent
.stream_class
) {
2064 * Note: many file streams can inherit from the same stream class
2065 * description (metadata).
2068 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2069 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2072 int ret
, fd
, closeret
;
2073 struct ctf_file_stream
*file_stream
;
2074 struct stat statbuf
;
2077 fd
= openat(td
->dirfd
, path
, flags
);
2079 perror("File stream openat()");
2084 /* Don't try to mmap subdirectories. Skip them, return success. */
2085 ret
= fstat(fd
, &statbuf
);
2087 perror("File stream fstat()");
2090 if (S_ISDIR(statbuf
.st_mode
)) {
2091 if (strncmp(path
, "index", 5) != 0) {
2092 fprintf(stderr
, "[warning] Skipping directory '%s' "
2093 "found in trace\n", path
);
2098 if (!statbuf
.st_size
) {
2099 /** Skip empty files. */
2101 goto fd_is_empty_file
;
2104 file_stream
= g_new0(struct ctf_file_stream
, 1);
2105 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2106 file_stream
->pos
.fd
= -1;
2107 file_stream
->pos
.index_fp
= NULL
;
2109 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2110 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2113 file_stream
->pos
.packet_seek
= packet_seek
;
2115 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2120 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2123 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2127 * For now, only a single clock per trace is supported.
2129 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2132 * Allocate the index name for this stream and try to open it.
2134 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2136 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2140 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2143 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2144 ret
= create_stream_packet_index(td
, file_stream
);
2146 fprintf(stderr
, "[error] Stream index creation error.\n");
2150 ret
= openat(td
->dirfd
, index_name
, flags
);
2152 perror("Index file openat()");
2156 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2157 if (!file_stream
->pos
.index_fp
) {
2158 perror("fdopen() error");
2161 ret
= import_stream_packet_index(td
, file_stream
);
2166 ret
= fclose(file_stream
->pos
.index_fp
);
2168 perror("close index");
2174 /* Add stream file to stream class */
2175 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2176 &file_stream
->parent
);
2180 if (file_stream
->pos
.index_fp
) {
2181 ret
= fclose(file_stream
->pos
.index_fp
);
2183 perror("close index");
2186 if (file_stream
->parent
.trace_packet_header
)
2187 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2191 closeret
= ctf_fini_pos(&file_stream
->pos
);
2193 fprintf(stderr
, "Error on ctf_fini_pos\n");
2195 g_free(file_stream
);
2199 closeret
= close(fd
);
2201 perror("Error on fd close");
2208 int ctf_open_trace_read(struct ctf_trace
*td
,
2209 const char *path
, int flags
,
2210 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2211 int whence
), FILE *metadata_fp
)
2213 struct ctf_scanner
*scanner
;
2215 struct dirent
*dirent
;
2216 struct dirent
*diriter
;
2222 /* Open trace directory */
2223 td
->dir
= opendir(path
);
2225 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2230 td
->dirfd
= open(path
, 0);
2231 if (td
->dirfd
< 0) {
2232 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2233 perror("Trace directory open");
2237 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2238 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2241 * Keep the metadata file separate.
2242 * Keep scanner object local to the open. We don't support
2243 * incremental metadata append for on-disk traces.
2245 scanner
= ctf_scanner_alloc();
2247 fprintf(stderr
, "[error] Error allocating scanner\n");
2249 goto error_metadata
;
2251 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2252 ctf_scanner_free(scanner
);
2254 if (ret
== -ENOENT
) {
2255 fprintf(stderr
, "[warning] Empty metadata.\n");
2257 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2258 goto error_metadata
;
2262 * Open each stream: for each file, try to open, check magic
2263 * number, and get the stream ID to add to the right location in
2267 dirent_len
= offsetof(struct dirent
, d_name
) +
2268 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
2270 dirent
= malloc(dirent_len
);
2273 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2275 fprintf(stderr
, "[error] Readdir error.\n");
2280 /* Ignore hidden files, ., .. and metadata. */
2281 if (!strncmp(diriter
->d_name
, ".", 1)
2282 || !strcmp(diriter
->d_name
, "..")
2283 || !strcmp(diriter
->d_name
, "metadata"))
2286 /* Ignore index files : *.idx */
2287 ext
= strrchr(diriter
->d_name
, '.');
2288 if (ext
&& (!strcmp(ext
, ".idx"))) {
2292 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2293 flags
, packet_seek
);
2295 fprintf(stderr
, "[error] Open file stream error.\n");
2306 closeret
= close(td
->dirfd
);
2308 perror("Error on fd close");
2311 closeret
= closedir(td
->dir
);
2313 perror("Error on closedir");
2320 * ctf_open_trace: Open a CTF trace and index it.
2321 * Note that the user must seek the trace after the open (using the iterator)
2322 * since the index creation read it entirely.
2325 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2326 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2327 int whence
), FILE *metadata_fp
)
2329 struct ctf_trace
*td
;
2333 * If packet_seek is NULL, we provide our default version.
2336 packet_seek
= ctf_packet_seek
;
2338 td
= g_new0(struct ctf_trace
, 1);
2340 switch (flags
& O_ACCMODE
) {
2343 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2346 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2351 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2354 fprintf(stderr
, "[error] Incorrect open flags.\n");
2358 ret
= trace_debug_info_create(td
);
2365 trace_debug_info_destroy(td
);
2371 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2372 struct bt_mmap_stream
*mmap_info
)
2374 pos
->mmap_offset
= 0;
2375 pos
->packet_size
= 0;
2376 pos
->content_size
= 0;
2377 pos
->content_size_loc
= NULL
;
2378 pos
->fd
= mmap_info
->fd
;
2379 pos
->base_mma
= NULL
;
2383 pos
->prot
= PROT_READ
;
2384 pos
->flags
= MAP_PRIVATE
;
2385 pos
->parent
.rw_table
= read_dispatch_table
;
2386 pos
->parent
.event_cb
= ctf_read_event
;
2387 pos
->priv
= mmap_info
->priv
;
2388 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2389 sizeof(struct packet_index
));
2393 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2394 struct ctf_file_stream
*file_stream
,
2395 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2398 struct ctf_stream_declaration
*stream
;
2402 /* Ask for the first packet to get the stream_id. */
2403 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2404 stream_id
= file_stream
->parent
.stream_id
;
2405 if (stream_id
>= td
->streams
->len
) {
2406 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2407 "in metadata.\n", stream_id
);
2411 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2413 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2414 "in metadata.\n", stream_id
);
2418 file_stream
->parent
.stream_class
= stream
;
2419 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2425 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2426 struct bt_mmap_stream
*mmap_info
,
2427 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2431 struct ctf_file_stream
*file_stream
;
2433 file_stream
= g_new0(struct ctf_file_stream
, 1);
2434 file_stream
->parent
.stream_id
= -1ULL;
2435 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2436 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2438 file_stream
->pos
.packet_seek
= packet_seek
;
2440 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2445 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2450 * For now, only a single clock per trace is supported.
2452 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2454 /* Add stream file to stream class */
2455 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2456 &file_stream
->parent
);
2460 if (file_stream
->parent
.trace_packet_header
)
2461 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2463 g_free(file_stream
);
2468 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2469 struct bt_mmap_stream_list
*mmap_list
,
2470 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2475 struct bt_mmap_stream
*mmap_info
;
2477 td
->scanner
= ctf_scanner_alloc();
2479 fprintf(stderr
, "[error] Error allocating scanner\n");
2483 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2485 if (ret
== -ENOENT
) {
2486 fprintf(stderr
, "[warning] Empty metadata.\n");
2492 * for each stream, try to open, check magic number, and get the
2493 * stream ID to add to the right location in the stream array.
2495 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2496 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2498 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2505 ctf_scanner_free(td
->scanner
);
2510 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2511 struct bt_mmap_stream_list
*mmap_list
,
2512 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2516 struct ctf_trace
*td
;
2520 fprintf(stderr
, "[error] No metadata file pointer associated, "
2521 "required for mmap parsing\n");
2525 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2528 td
= g_new0(struct ctf_trace
, 1);
2530 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2534 ret
= trace_debug_info_create(td
);
2547 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2550 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2556 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2559 /* for each stream_class */
2560 for (i
= 0; i
< td
->streams
->len
; i
++) {
2561 struct ctf_stream_declaration
*stream_class
;
2563 stream_class
= g_ptr_array_index(td
->streams
, i
);
2566 /* for each stream */
2567 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2568 struct ctf_stream_definition
*stream
;
2570 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2573 ret
= copy_event_declarations_stream_class_to_stream(td
,
2574 stream_class
, stream
);
2583 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2586 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2588 /* for each stream_class */
2589 for (i
= 0; i
< td
->streams
->len
; i
++) {
2590 struct ctf_stream_declaration
*stream_class
;
2592 stream_class
= g_ptr_array_index(td
->streams
, i
);
2595 /* for each file_stream */
2596 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2597 struct ctf_stream_definition
*stream
;
2598 struct ctf_stream_pos
*stream_pos
;
2599 struct ctf_file_stream
*cfs
;
2601 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2604 cfs
= container_of(stream
, struct ctf_file_stream
,
2606 stream_pos
= &cfs
->pos
;
2607 if (!stream_pos
->packet_index
)
2610 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2611 struct packet_index
*index
;
2613 index
= &g_array_index(stream_pos
->packet_index
,
2614 struct packet_index
, k
);
2615 index
->ts_real
.timestamp_begin
=
2616 ctf_get_real_timestamp(stream
,
2617 index
->ts_cycles
.timestamp_begin
);
2618 index
->ts_real
.timestamp_end
=
2619 ctf_get_real_timestamp(stream
,
2620 index
->ts_cycles
.timestamp_end
);
2628 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2632 ret
= ctf_fini_pos(&file_stream
->pos
);
2634 fprintf(stderr
, "Error on ctf_fini_pos\n");
2637 if (file_stream
->pos
.fd
>= 0) {
2638 ret
= close(file_stream
->pos
.fd
);
2640 perror("Error closing file fd");
2648 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2650 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2656 for (i
= 0; i
< td
->streams
->len
; i
++) {
2657 struct ctf_stream_declaration
*stream
;
2660 stream
= g_ptr_array_index(td
->streams
, i
);
2663 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2664 struct ctf_file_stream
*file_stream
;
2665 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2666 struct ctf_file_stream
, parent
);
2667 ret
= ctf_close_file_stream(file_stream
);
2673 ctf_destroy_metadata(td
);
2674 ctf_scanner_free(td
->scanner
);
2675 if (td
->dirfd
>= 0) {
2676 ret
= close(td
->dirfd
);
2678 perror("Error closing dirfd");
2683 ret
= closedir(td
->dir
);
2685 perror("Error closedir");
2689 free(td
->metadata_string
);
2690 trace_debug_info_destroy(td
);
2696 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2697 struct bt_context
*ctx
)
2699 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2702 td
->parent
.ctx
= ctx
;
2706 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2707 struct bt_trace_handle
*handle
)
2709 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2712 td
->parent
.handle
= handle
;
2716 void __attribute__((constructor
)) ctf_init(void)
2720 ctf_format
.name
= g_quark_from_static_string("ctf");
2721 ret
= bt_register_format(&ctf_format
);
2726 void __attribute__((destructor
)) ctf_exit(void)
2728 bt_unregister_format(&ctf_format
);