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 1000000000LL
74 #define INDEX_PATH "./index/%s.idx"
81 int64_t opt_clock_offset
;
82 int64_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 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
117 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
120 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
121 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 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_read
,
129 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_read
,
130 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_read
,
131 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_read
,
132 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
133 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
134 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_read
,
135 [ BT_CTF_TYPE_ID_SEQUENCE
] = ctf_sequence_read
,
139 rw_dispatch write_dispatch_table
[] = {
140 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_write
,
141 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_write
,
142 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_write
,
143 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_write
,
144 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
145 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
146 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_write
,
147 [ BT_CTF_TYPE_ID_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
,
162 void bt_ctf_hook(void)
165 * Dummy function to prevent the linker from discarding this format as
166 * "unused" in static builds.
171 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
172 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
175 struct ctf_trace
*tin
;
176 int64_t begin
= LLONG_MAX
;
179 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
181 if (!tin
|| !timestamp
) {
186 /* for each stream_class */
187 for (i
= 0; i
< tin
->streams
->len
; i
++) {
188 struct ctf_stream_declaration
*stream_class
;
190 stream_class
= g_ptr_array_index(tin
->streams
, i
);
193 /* for each file_stream */
194 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
195 struct ctf_stream_definition
*stream
;
196 struct ctf_file_stream
*cfs
;
197 struct ctf_stream_pos
*stream_pos
;
198 struct packet_index
*index
;
200 stream
= g_ptr_array_index(stream_class
->streams
, j
);
201 cfs
= container_of(stream
, struct ctf_file_stream
,
203 stream_pos
= &cfs
->pos
;
205 if (!stream_pos
->packet_index
) {
210 if (stream_pos
->packet_index
->len
<= 0)
213 index
= &g_array_index(stream_pos
->packet_index
,
215 stream_pos
->packet_index
->len
- 1);
216 if (type
== BT_CLOCK_REAL
) {
217 if (index
->ts_real
.timestamp_begin
< begin
)
218 begin
= index
->ts_real
.timestamp_begin
;
219 } else if (type
== BT_CLOCK_CYCLES
) {
220 if (index
->ts_cycles
.timestamp_begin
< begin
)
221 begin
= index
->ts_cycles
.timestamp_begin
;
228 if (begin
== LLONG_MAX
) {
240 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
241 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
244 struct ctf_trace
*tin
;
245 int64_t end
= LLONG_MIN
;
248 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
250 if (!tin
|| !timestamp
) {
255 /* for each stream_class */
256 for (i
= 0; i
< tin
->streams
->len
; i
++) {
257 struct ctf_stream_declaration
*stream_class
;
259 stream_class
= g_ptr_array_index(tin
->streams
, i
);
262 /* for each file_stream */
263 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
264 struct ctf_stream_definition
*stream
;
265 struct ctf_file_stream
*cfs
;
266 struct ctf_stream_pos
*stream_pos
;
267 struct packet_index
*index
;
269 stream
= g_ptr_array_index(stream_class
->streams
, j
);
270 cfs
= container_of(stream
, struct ctf_file_stream
,
272 stream_pos
= &cfs
->pos
;
274 if (!stream_pos
->packet_index
) {
279 if (stream_pos
->packet_index
->len
<= 0)
282 index
= &g_array_index(stream_pos
->packet_index
,
284 stream_pos
->packet_index
->len
- 1);
285 if (type
== BT_CLOCK_REAL
) {
286 if (index
->ts_real
.timestamp_end
> end
)
287 end
= index
->ts_real
.timestamp_end
;
288 } else if (type
== BT_CLOCK_CYCLES
) {
289 if (index
->ts_cycles
.timestamp_end
> end
)
290 end
= index
->ts_cycles
.timestamp_end
;
297 if (end
== LLONG_MIN
) {
309 * Update stream current timestamp
312 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
313 struct definition_integer
*integer_definition
)
315 struct declaration_integer
*integer_declaration
=
316 integer_definition
->declaration
;
317 uint64_t oldval
, newval
, updateval
;
319 if (unlikely(integer_declaration
->len
== 64)) {
320 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
321 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
322 stream
->cycles_timestamp
);
326 oldval
= stream
->cycles_timestamp
;
327 oldval
&= (1ULL << integer_declaration
->len
) - 1;
328 newval
= integer_definition
->value
._unsigned
;
329 /* Test for overflow by comparing low bits */
331 newval
+= 1ULL << integer_declaration
->len
;
332 /* updateval contains old high bits, and new low bits (sum) */
333 updateval
= stream
->cycles_timestamp
;
334 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
336 stream
->cycles_timestamp
= updateval
;
338 /* convert to real timestamp */
339 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
340 stream
->cycles_timestamp
);
344 * Print timestamp, rescaling clock frequency to nanoseconds and
345 * applying offsets as needed (unix time).
348 void ctf_print_timestamp_real(FILE *fp
,
349 struct ctf_stream_definition
*stream
,
352 int64_t ts_sec
= 0, ts_nsec
;
353 uint64_t ts_sec_abs
, ts_nsec_abs
;
358 /* Add command-line offset in ns */
359 ts_nsec
+= opt_clock_offset_ns
;
361 /* Add command-line offset */
362 ts_sec
+= opt_clock_offset
;
364 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
365 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
366 if (ts_sec
>= 0 && ts_nsec
>= 0) {
369 ts_nsec_abs
= ts_nsec
;
370 } else if (ts_sec
> 0 && ts_nsec
< 0) {
372 ts_sec_abs
= ts_sec
- 1;
373 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
374 } else if (ts_sec
== 0 && ts_nsec
< 0) {
377 ts_nsec_abs
= -ts_nsec
;
378 } else if (ts_sec
< 0 && ts_nsec
> 0) {
380 ts_sec_abs
= -(ts_sec
+ 1);
381 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
382 } else if (ts_sec
< 0 && ts_nsec
== 0) {
384 ts_sec_abs
= -ts_sec
;
385 ts_nsec_abs
= ts_nsec
;
386 } else { /* (ts_sec < 0 && ts_nsec < 0) */
388 ts_sec_abs
= -ts_sec
;
389 ts_nsec_abs
= -ts_nsec
;
392 if (!opt_clock_seconds
) {
394 time_t time_s
= (time_t) ts_sec_abs
;
397 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
401 if (!opt_clock_gmt
) {
404 res
= localtime_r(&time_s
, &tm
);
406 fprintf(stderr
, "[warning] Unable to get localtime.\n");
412 res
= gmtime_r(&time_s
, &tm
);
414 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
418 if (opt_clock_date
) {
422 /* Print date and time */
423 res
= strftime(timestr
, sizeof(timestr
),
426 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
429 fprintf(fp
, "%s", timestr
);
431 /* Print time in HH:MM:SS.ns */
432 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
433 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec_abs
);
437 fprintf(fp
, "%s%" PRId64
".%09" PRIu64
,
438 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
445 * Print timestamp, in cycles
448 void ctf_print_timestamp_cycles(FILE *fp
,
449 struct ctf_stream_definition
*stream
,
452 fprintf(fp
, "%020" PRIu64
, timestamp
);
455 void ctf_print_timestamp(FILE *fp
,
456 struct ctf_stream_definition
*stream
,
459 if (opt_clock_cycles
) {
460 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
462 ctf_print_timestamp_real(fp
, stream
, timestamp
);
467 void print_uuid(FILE *fp
, unsigned char *uuid
)
471 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
472 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
476 * Discarded events can be either:
477 * - discarded after end of previous buffer due to buffer full:
478 * happened within range: [ prev_timestamp_end, timestamp_begin ]
479 * - discarded within current buffer due to either event too large or
480 * nested wrap-around:
481 * happened within range: [ timestamp_begin, timestamp_end ]
483 * Given we have discarded counters of those two types merged into the
484 * events_discarded counter, we need to use the union of those ranges:
485 * [ prev_timestamp_end, timestamp_end ]
487 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
488 * consumer had time to extract them. We keep track of those gaps with the
489 * packet sequence number in each packet.
492 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
494 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
495 !babeltrace_ctf_console_output
) {
499 if (stream
->events_discarded
) {
500 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
501 stream
->events_discarded
);
502 } else if (stream
->packets_lost
) {
503 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
504 stream
->packets_lost
);
506 if (opt_clock_cycles
) {
507 ctf_print_timestamp(fp
, stream
,
508 stream
->prev
.cycles
.end
);
509 fprintf(fp
, "] and [");
510 ctf_print_timestamp(fp
, stream
,
511 stream
->current
.cycles
.end
);
513 ctf_print_timestamp(fp
, stream
,
514 stream
->prev
.real
.end
);
515 fprintf(fp
, "] and [");
516 ctf_print_timestamp(fp
, stream
,
517 stream
->current
.real
.end
);
519 fprintf(fp
, "] in trace UUID ");
520 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
521 if (stream
->stream_class
->trace
->parent
.path
[0])
522 fprintf(fp
, ", at path: \"%s\"",
523 stream
->stream_class
->trace
->parent
.path
);
525 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
527 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
529 fprintf(fp
, "You should consider recording a new trace with larger "
530 "buffers or with fewer events enabled.\n");
535 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
537 struct ctf_stream_pos
*pos
=
538 container_of(ppos
, struct ctf_stream_pos
, parent
);
539 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
540 struct ctf_event_definition
*event
;
544 /* We need to check for EOF here for empty files. */
545 if (unlikely(pos
->offset
== EOF
))
548 ctf_pos_get_event(pos
);
550 /* save the current position as a restore point */
551 pos
->last_offset
= pos
->offset
;
554 * This is the EOF check after we've advanced the position in
557 if (unlikely(pos
->offset
== EOF
))
560 /* Stream is inactive for now (live reading). */
561 if (unlikely(pos
->content_size
== 0))
565 * Packet seeked to by ctf_pos_get_event() only contains
566 * headers, no event. Consider stream as inactive (live
569 if (unlikely(pos
->data_offset
== pos
->content_size
))
572 assert(pos
->offset
< pos
->content_size
);
574 /* Read event header */
575 if (likely(stream
->stream_event_header
)) {
576 struct definition_integer
*integer_definition
;
577 struct bt_definition
*variant
;
579 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
582 /* lookup event id */
583 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
584 if (integer_definition
) {
585 id
= integer_definition
->value
._unsigned
;
587 struct definition_enum
*enum_definition
;
589 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
590 if (enum_definition
) {
591 id
= enum_definition
->integer
->value
._unsigned
;
595 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
597 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
598 if (integer_definition
) {
599 id
= integer_definition
->value
._unsigned
;
602 stream
->event_id
= id
;
604 /* lookup timestamp */
605 stream
->has_timestamp
= 0;
606 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
607 if (integer_definition
) {
608 ctf_update_timestamp(stream
, integer_definition
);
609 stream
->has_timestamp
= 1;
612 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
613 if (integer_definition
) {
614 ctf_update_timestamp(stream
, integer_definition
);
615 stream
->has_timestamp
= 1;
621 /* Read stream-declared event context */
622 if (stream
->stream_event_context
) {
623 ret
= generic_rw(ppos
, &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 /* Read event-declared event context */
639 if (event
->event_context
) {
640 ret
= generic_rw(ppos
, &event
->event_context
->p
);
645 /* Read event payload */
646 if (likely(event
->event_fields
)) {
647 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
652 if (pos
->last_offset
== pos
->offset
) {
653 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
660 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
665 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
667 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
668 struct ctf_event_definition
*event
;
672 id
= stream
->event_id
;
674 /* print event header */
675 if (likely(stream
->stream_event_header
)) {
676 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
681 /* print stream-declared event context */
682 if (stream
->stream_event_context
) {
683 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
688 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
689 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
692 event
= g_ptr_array_index(stream
->events_by_id
, id
);
693 if (unlikely(!event
)) {
694 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
698 /* print event-declared event context */
699 if (event
->event_context
) {
700 ret
= generic_rw(pos
, &event
->event_context
->p
);
705 /* Read and print event payload */
706 if (likely(event
->event_fields
)) {
707 ret
= generic_rw(pos
, &event
->event_fields
->p
);
715 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
720 * One side-effect of this function is to unmap pos mmap base if one is
724 int find_data_offset(struct ctf_stream_pos
*pos
,
725 struct ctf_file_stream
*file_stream
,
726 struct packet_index
*packet_index
)
728 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
729 struct stat filestats
;
733 pos
= &file_stream
->pos
;
735 ret
= fstat(pos
->fd
, &filestats
);
738 filesize
= filestats
.st_size
;
740 /* Deal with empty files */
746 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
747 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
752 ret
= munmap_align(pos
->base_mma
);
754 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
758 pos
->base_mma
= NULL
;
760 /* map new base. Need mapping length from header. */
761 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
762 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
763 assert(pos
->base_mma
!= MAP_FAILED
);
765 pos
->content_size
= packet_map_len
;
766 pos
->packet_size
= packet_map_len
;
767 pos
->offset
= 0; /* Position of the packet header */
769 /* update trace_packet_header and stream_packet_context */
770 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
771 /* Read packet header */
772 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
778 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
779 /* Read packet context */
780 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
786 packet_index
->data_offset
= pos
->offset
;
789 ret
= munmap_align(pos
->base_mma
);
791 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
795 pos
->base_mma
= NULL
;
799 /* Retry with larger mapping */
801 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
803 * Reached EOF, but still expecting header/context data.
805 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
808 /* Double the mapping len, and retry */
809 tmp_map_len
= packet_map_len
<< 1;
810 if (tmp_map_len
>> 1 != packet_map_len
) {
812 fprintf(stderr
, "[error] Packet mapping length overflow\n");
815 packet_map_len
= tmp_map_len
;
820 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
821 int fd
, int open_flags
)
825 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
826 sizeof(struct packet_index
));
828 pos
->packet_index
= NULL
;
830 switch (open_flags
& O_ACCMODE
) {
832 pos
->prot
= PROT_READ
;
833 pos
->flags
= MAP_PRIVATE
;
834 pos
->parent
.rw_table
= read_dispatch_table
;
835 pos
->parent
.event_cb
= ctf_read_event
;
836 pos
->parent
.trace
= trace
;
839 pos
->prot
= PROT_READ
| PROT_WRITE
;
840 pos
->flags
= MAP_SHARED
;
841 pos
->parent
.rw_table
= write_dispatch_table
;
842 pos
->parent
.event_cb
= ctf_write_event
;
843 pos
->parent
.trace
= trace
;
851 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
853 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
854 *pos
->content_size_loc
= pos
->offset
;
859 ret
= munmap_align(pos
->base_mma
);
861 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
866 if (pos
->packet_index
)
867 (void) g_array_free(pos
->packet_index
, TRUE
);
871 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
872 struct packet_index
*prev_index
,
873 struct packet_index
*cur_index
)
875 uint64_t events_discarded_diff
;
876 uint64_t packets_lost_diff
= 0;
878 /* Update packet index time information */
880 /* Current packet begin/end */
881 stream
->current
.real
.begin
=
882 cur_index
->ts_real
.timestamp_begin
;
883 stream
->current
.cycles
.begin
=
884 cur_index
->ts_cycles
.timestamp_begin
;
885 stream
->current
.real
.end
=
886 cur_index
->ts_real
.timestamp_end
;
887 stream
->current
.cycles
.end
=
888 cur_index
->ts_cycles
.timestamp_end
;
890 /* Update packet index discarded event information */
891 events_discarded_diff
= cur_index
->events_discarded
;
893 /* Previous packet begin/end */
894 stream
->prev
.cycles
.begin
=
895 prev_index
->ts_cycles
.timestamp_begin
;
896 stream
->prev
.real
.begin
=
897 prev_index
->ts_real
.timestamp_begin
;
898 stream
->prev
.cycles
.end
=
899 prev_index
->ts_cycles
.timestamp_end
;
900 stream
->prev
.real
.end
=
901 prev_index
->ts_real
.timestamp_end
;
903 events_discarded_diff
-= prev_index
->events_discarded
;
904 /* packet_seq_num stays at 0 if not produced by the tracer */
905 if (cur_index
->packet_seq_num
) {
906 packets_lost_diff
= cur_index
->packet_seq_num
-
907 prev_index
->packet_seq_num
- 1;
910 * Deal with 32-bit wrap-around if the tracer provided a
913 if (prev_index
->events_discarded_len
== 32) {
914 events_discarded_diff
= (uint32_t) events_discarded_diff
;
918 * First packet: use current packet info as limits for
921 stream
->prev
.cycles
.begin
=
922 stream
->prev
.cycles
.end
=
923 stream
->current
.cycles
.begin
;
924 stream
->prev
.real
.begin
=
925 stream
->prev
.real
.end
=
926 stream
->current
.real
.begin
;
928 stream
->events_discarded
= events_discarded_diff
;
929 stream
->packets_lost
= packets_lost_diff
;
933 * Find the timerange where all the streams in the trace are active
936 * Return 0 and update begin/end if necessary on success, return 1 for
937 * empty streams and return a negative value on error.
940 int ctf_find_stream_intersection(struct bt_trace_descriptor
*td_read
,
941 struct packet_index_time
*_real
,
942 struct packet_index_time
*_cycles
)
944 int stream_id
, ret
= 0;
945 struct packet_index_time real
= { INT64_MIN
, INT64_MAX
},
946 cycles
= { INT64_MIN
, INT64_MAX
};
947 struct ctf_trace
*tin
= container_of(td_read
, struct ctf_trace
, parent
);
949 /* At least one of the two return args must be provided. */
950 if (!_real
&& !_cycles
) {
955 if (tin
->streams
->len
== 0) {
960 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
963 struct ctf_stream_declaration
*stream_class
;
965 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
969 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
970 struct ctf_file_stream
*file_stream
;
971 struct ctf_stream_pos
*stream_pos
;
972 struct packet_index
*index
;
974 file_stream
= g_ptr_array_index(stream_class
->streams
,
979 stream_pos
= &file_stream
->pos
;
980 if (!stream_pos
->packet_index
||
981 stream_pos
->packet_index
->len
<= 0) {
985 index
= &g_array_index(stream_pos
->packet_index
,
986 struct packet_index
, 0);
987 real
.timestamp_begin
= max(real
.timestamp_begin
,
988 index
->ts_real
.timestamp_begin
);
989 cycles
.timestamp_begin
= max(cycles
.timestamp_begin
,
990 index
->ts_cycles
.timestamp_begin
);
992 index
= &g_array_index(stream_pos
->packet_index
,
994 stream_pos
->packet_index
->len
- 1);
995 real
.timestamp_end
= min(real
.timestamp_end
,
996 index
->ts_real
.timestamp_end
);
997 cycles
.timestamp_end
= min(cycles
.timestamp_end
,
998 index
->ts_cycles
.timestamp_end
);
1014 * Find the union of all active regions in the trace collection's traces.
1015 * Returns "real" timestamps.
1017 * Return 0 on success.
1018 * Return 1 if no intersections are found.
1019 * Return a negative value on error.
1021 int ctf_find_tc_stream_packet_intersection_union(struct bt_context
*ctx
,
1022 int64_t *_ts_begin
, int64_t *_ts_end
)
1025 int64_t ts_begin
= INT64_MAX
, ts_end
= INT64_MIN
;
1027 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
|| !_ts_begin
|| !_ts_end
) {
1032 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
1033 struct bt_trace_descriptor
*td_read
;
1034 struct packet_index_time intersection_real
;
1036 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
1040 ret
= ctf_find_stream_intersection(td_read
, &intersection_real
,
1043 /* Empty trace or no stream intersection. */
1045 } else if (ret
< 0) {
1049 ts_begin
= min(intersection_real
.timestamp_begin
, ts_begin
);
1050 ts_end
= max(intersection_real
.timestamp_end
, ts_end
);
1053 if (ts_end
< ts_begin
) {
1057 *_ts_begin
= ts_begin
;
1063 int ctf_tc_set_stream_intersection_mode(struct bt_context
*ctx
)
1067 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
1072 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
1073 struct bt_trace_descriptor
*td_read
;
1074 struct packet_index_time intersection_real
;
1076 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
1081 ret
= ctf_find_stream_intersection(td_read
, &intersection_real
,
1084 /* Empty trace or no stream intersection. */
1086 } else if (ret
< 0) {
1090 td_read
->interval_real
= intersection_real
;
1091 td_read
->interval_set
= true;
1098 * for SEEK_CUR: go to next packet.
1099 * for SEEK_SET: go to packet numer (index).
1101 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
1103 struct ctf_stream_pos
*pos
=
1104 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
1105 struct ctf_file_stream
*file_stream
=
1106 container_of(pos
, struct ctf_file_stream
, pos
);
1108 struct packet_index
*packet_index
, *prev_index
;
1112 case SEEK_SET
: /* Fall-through */
1118 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
1119 *pos
->content_size_loc
= pos
->offset
;
1121 if (pos
->base_mma
) {
1122 /* unmap old base */
1123 ret
= munmap_align(pos
->base_mma
);
1125 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1129 pos
->base_mma
= NULL
;
1133 * The caller should never ask for ctf_move_pos across packets,
1134 * except to get exactly at the beginning of the next packet.
1136 if (pos
->prot
& PROT_WRITE
) {
1139 /* The writer will add padding */
1140 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1143 assert(index
== 0); /* only seek supported for now */
1149 pos
->content_size
= -1U; /* Unknown at this point */
1150 pos
->packet_size
= WRITE_PACKET_LEN
;
1152 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1153 pos
->packet_size
/ CHAR_BIT
);
1154 } while (ret
== EINTR
);
1162 if (pos
->offset
== EOF
) {
1165 assert(pos
->cur_index
< pos
->packet_index
->len
);
1166 /* The reader will expect us to skip padding */
1171 if (index
>= pos
->packet_index
->len
) {
1175 pos
->cur_index
= index
;
1181 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1186 packet_index
= &g_array_index(pos
->packet_index
,
1187 struct packet_index
, pos
->cur_index
);
1188 if (pos
->cur_index
> 0) {
1189 prev_index
= &g_array_index(pos
->packet_index
,
1190 struct packet_index
,
1191 pos
->cur_index
- 1);
1195 ctf_update_current_packet_index(&file_stream
->parent
,
1196 prev_index
, packet_index
);
1199 * We need to check if we are in trace read or called
1200 * from packet indexing. In this last case, the
1201 * collection is not there, so we cannot print the
1204 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1205 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1208 packet_index
= &g_array_index(pos
->packet_index
,
1209 struct packet_index
,
1211 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1213 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1215 /* Lookup context/packet size in index */
1216 if (packet_index
->data_offset
== -1) {
1217 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1222 pos
->content_size
= packet_index
->content_size
;
1223 pos
->packet_size
= packet_index
->packet_size
;
1224 pos
->mmap_offset
= packet_index
->offset
;
1225 pos
->data_offset
= packet_index
->data_offset
;
1226 if (pos
->data_offset
< packet_index
->content_size
) {
1227 pos
->offset
= 0; /* will read headers */
1228 } else if (pos
->data_offset
== packet_index
->content_size
) {
1230 pos
->offset
= packet_index
->data_offset
;
1232 goto read_next_packet
;
1238 /* map new base. Need mapping length from header. */
1239 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1240 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1241 if (pos
->base_mma
== MAP_FAILED
) {
1242 fprintf(stderr
, "[error] mmap error %s.\n",
1247 /* update trace_packet_header and stream_packet_context */
1248 if (!(pos
->prot
& PROT_WRITE
) &&
1249 file_stream
->parent
.trace_packet_header
) {
1250 /* Read packet header */
1251 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1254 if (!(pos
->prot
& PROT_WRITE
) &&
1255 file_stream
->parent
.stream_packet_context
) {
1256 /* Read packet context */
1257 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1263 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1269 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1273 if (magic
== TSDL_MAGIC
) {
1275 td
->byte_order
= BYTE_ORDER
;
1276 CTF_TRACE_SET_FIELD(td
, byte_order
);
1277 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1279 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1280 LITTLE_ENDIAN
: BIG_ENDIAN
;
1281 CTF_TRACE_SET_FIELD(td
, byte_order
);
1289 * Returns 0 on success, -1 on error.
1292 int check_version(unsigned int major
, unsigned int minor
)
1307 /* eventually return an error instead of warning */
1309 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1315 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1318 struct metadata_packet_header header
;
1319 size_t readlen
, writelen
, toread
;
1320 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1323 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1327 if (td
->byte_order
!= BYTE_ORDER
) {
1328 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1329 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1330 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1331 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1333 if (header
.checksum
)
1334 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1335 if (header
.compression_scheme
) {
1336 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1337 header
.compression_scheme
);
1340 if (header
.encryption_scheme
) {
1341 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1342 header
.encryption_scheme
);
1345 if (header
.checksum_scheme
) {
1346 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1347 header
.checksum_scheme
);
1350 if (check_version(header
.major
, header
.minor
) < 0)
1352 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1353 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1354 CTF_TRACE_SET_FIELD(td
, uuid
);
1356 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1360 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1363 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1366 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1371 if (babeltrace_debug
) {
1372 buf
[readlen
] = '\0';
1373 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1377 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1378 if (writelen
< readlen
) {
1388 ret
= 0; /* continue reading next packet */
1395 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1396 ret
= fseek(in
, toread
, SEEK_CUR
);
1398 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1405 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1409 size_t size
, buflen
;
1414 * Using strlen on *buf instead of size of open_memstream
1415 * because its size includes garbage at the end (after final
1416 * \0). This is the allocated size, not the actual string size.
1418 out
= bt_open_memstream(buf
, &size
);
1420 perror("Metadata open_memstream");
1424 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1433 /* close to flush the buffer */
1434 ret
= bt_close_memstream(buf
, &size
, out
);
1438 perror("babeltrace_flush_memstream");
1440 closeret
= fclose(in
);
1442 perror("Error in fclose");
1448 perror("Error in fclose");
1450 /* open for reading */
1451 buflen
= strlen(*buf
);
1456 *fp
= bt_fmemopen(*buf
, buflen
, "rb");
1458 perror("Metadata fmemopen");
1465 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1466 struct ctf_scanner
*scanner
, int append
)
1468 struct ctf_file_stream
*metadata_stream
;
1471 int ret
= 0, closeret
;
1473 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1474 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1478 metadata_stream
->pos
.fd
= -1;
1480 td
->metadata
= &metadata_stream
->parent
;
1481 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1482 if (metadata_stream
->pos
.fd
< 0) {
1483 fprintf(stderr
, "Unable to open metadata.\n");
1488 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1490 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1491 perror("Metadata stream open");
1495 /* fd now belongs to fp */
1496 metadata_stream
->pos
.fd
= -1;
1498 if (babeltrace_debug
)
1501 if (packet_metadata(td
, fp
)) {
1502 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1506 td
->metadata_string
= buf
;
1507 td
->metadata_packetized
= 1;
1510 unsigned int major
, minor
;
1513 td
->byte_order
= BYTE_ORDER
;
1515 /* Check text-only metadata header and version */
1516 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1518 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1519 if (check_version(major
, minor
) < 0) {
1527 ret
= ctf_scanner_append_ast(scanner
, fp
);
1529 fprintf(stderr
, "[error] Error creating AST\n");
1533 if (babeltrace_debug
) {
1534 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1536 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1541 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1543 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1546 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1547 td
, td
->byte_order
);
1549 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1554 closeret
= fclose(fp
);
1556 perror("Error on fclose");
1560 if (metadata_stream
->pos
.fd
>= 0) {
1561 closeret
= close(metadata_stream
->pos
.fd
);
1563 perror("Error on metadata stream fd close");
1568 g_free(metadata_stream
);
1573 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1574 struct ctf_stream_definition
*stream
,
1575 struct ctf_event_declaration
*event
)
1577 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1579 if (event
->context_decl
) {
1580 struct bt_definition
*definition
=
1581 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1582 stream
->parent_def_scope
, 0, 0, "event.context");
1586 stream_event
->event_context
= container_of(definition
,
1587 struct definition_struct
, p
);
1588 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1590 if (event
->fields_decl
) {
1591 struct bt_definition
*definition
=
1592 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1593 stream
->parent_def_scope
, 0, 0, "event.fields");
1597 stream_event
->event_fields
= container_of(definition
,
1598 struct definition_struct
, p
);
1599 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1601 stream_event
->stream
= stream
;
1602 return stream_event
;
1605 if (stream_event
->event_fields
)
1606 bt_definition_unref(&stream_event
->event_fields
->p
);
1607 if (stream_event
->event_context
)
1608 bt_definition_unref(&stream_event
->event_context
->p
);
1609 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1610 g_quark_to_string(event
->name
));
1615 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1616 struct ctf_stream_declaration
*stream_class
,
1617 struct ctf_stream_definition
*stream
)
1619 size_t def_size
, class_size
, i
;
1622 def_size
= stream
->events_by_id
->len
;
1623 class_size
= stream_class
->events_by_id
->len
;
1625 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1626 for (i
= def_size
; i
< class_size
; i
++) {
1627 struct ctf_event_declaration
*event
=
1628 g_ptr_array_index(stream_class
->events_by_id
, i
);
1629 struct ctf_event_definition
*stream_event
;
1633 stream_event
= create_event_definitions(td
, stream
, event
);
1634 if (!stream_event
) {
1638 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1645 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1647 struct ctf_stream_declaration
*stream_class
;
1651 if (stream
->stream_definitions_created
)
1654 stream_class
= stream
->stream_class
;
1656 if (stream_class
->packet_context_decl
) {
1657 struct bt_definition
*definition
=
1658 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1659 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1664 stream
->stream_packet_context
= container_of(definition
,
1665 struct definition_struct
, p
);
1666 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1668 if (stream_class
->event_header_decl
) {
1669 struct bt_definition
*definition
=
1670 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1671 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1676 stream
->stream_event_header
=
1677 container_of(definition
, struct definition_struct
, p
);
1678 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1680 if (stream_class
->event_context_decl
) {
1681 struct bt_definition
*definition
=
1682 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1683 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1688 stream
->stream_event_context
=
1689 container_of(definition
, struct definition_struct
, p
);
1690 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1692 stream
->events_by_id
= g_ptr_array_new();
1693 ret
= copy_event_declarations_stream_class_to_stream(td
,
1694 stream_class
, stream
);
1700 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1701 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1703 g_free(stream_event
);
1705 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1707 if (stream
->stream_event_context
)
1708 bt_definition_unref(&stream
->stream_event_context
->p
);
1709 if (stream
->stream_event_header
)
1710 bt_definition_unref(&stream
->stream_event_header
->p
);
1711 if (stream
->stream_packet_context
)
1712 bt_definition_unref(&stream
->stream_packet_context
->p
);
1713 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1714 stream_class
->stream_id
, strerror(-ret
));
1719 int stream_assign_class(struct ctf_trace
*td
,
1720 struct ctf_file_stream
*file_stream
,
1723 struct ctf_stream_declaration
*stream
;
1726 file_stream
->parent
.stream_id
= stream_id
;
1727 if (stream_id
>= td
->streams
->len
) {
1728 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1731 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1733 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1736 file_stream
->parent
.stream_class
= stream
;
1737 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1744 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1745 struct ctf_trace
*td
,
1746 struct ctf_file_stream
*file_stream
,
1749 struct packet_index packet_index
;
1750 uint64_t stream_id
= 0;
1751 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1752 int first_packet
= 0;
1757 memset(&packet_index
, 0, sizeof(packet_index
));
1758 if (!pos
->mmap_offset
) {
1762 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1763 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1766 if (pos
->base_mma
) {
1767 /* unmap old base */
1768 ret
= munmap_align(pos
->base_mma
);
1770 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1774 pos
->base_mma
= NULL
;
1776 /* map new base. Need mapping length from header. */
1777 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1778 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1779 assert(pos
->base_mma
!= MAP_FAILED
);
1781 * Use current mapping size as temporary content and packet
1784 pos
->content_size
= packet_map_len
;
1785 pos
->packet_size
= packet_map_len
;
1786 pos
->offset
= 0; /* Position of the packet header */
1788 packet_index
.offset
= pos
->mmap_offset
;
1790 /* read and check header, set stream id (and check) */
1791 if (file_stream
->parent
.trace_packet_header
) {
1792 /* Read packet header */
1793 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1797 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1800 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_string("magic"));
1801 if (len_index
>= 0) {
1802 struct bt_definition
*field
;
1805 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1806 magic
= bt_get_unsigned_int(field
);
1807 if (magic
!= CTF_MAGIC
) {
1808 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1810 file_stream
->pos
.packet_index
->len
,
1811 (ssize_t
) pos
->mmap_offset
);
1817 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_string("uuid"));
1818 if (len_index
>= 0) {
1819 struct definition_array
*defarray
;
1820 struct bt_definition
*field
;
1822 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1824 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1825 assert(field
->declaration
->id
== BT_CTF_TYPE_ID_ARRAY
);
1826 defarray
= container_of(field
, struct definition_array
, p
);
1827 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1829 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1830 struct bt_definition
*elem
;
1832 elem
= bt_array_index(defarray
, i
);
1833 uuidval
[i
] = bt_get_unsigned_int(elem
);
1835 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1837 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1842 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_string("stream_id"));
1843 if (len_index
>= 0) {
1844 struct bt_definition
*field
;
1846 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1847 stream_id
= bt_get_unsigned_int(field
);
1851 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1852 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1854 file_stream
->parent
.stream_id
);
1858 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1863 if (file_stream
->parent
.stream_packet_context
) {
1864 /* Read packet context */
1865 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1869 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1872 /* read packet size from header */
1873 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_string("packet_size"));
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
.packet_size
= bt_get_unsigned_int(field
);
1880 /* Use file size for packet size */
1881 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1884 /* read content size from header */
1885 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_string("content_size"));
1886 if (len_index
>= 0) {
1887 struct bt_definition
*field
;
1889 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1890 packet_index
.content_size
= bt_get_unsigned_int(field
);
1892 /* Use packet size if non-zero, else file size */
1893 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1896 /* read timestamp begin from header */
1897 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_string("timestamp_begin"));
1898 if (len_index
>= 0) {
1899 struct bt_definition
*field
;
1901 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1902 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1903 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1904 packet_index
.ts_real
.timestamp_begin
=
1905 ctf_get_real_timestamp(
1906 &file_stream
->parent
,
1907 packet_index
.ts_cycles
.timestamp_begin
);
1911 /* read timestamp end from header */
1912 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_string("timestamp_end"));
1913 if (len_index
>= 0) {
1914 struct bt_definition
*field
;
1916 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1917 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1918 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1919 packet_index
.ts_real
.timestamp_end
=
1920 ctf_get_real_timestamp(
1921 &file_stream
->parent
,
1922 packet_index
.ts_cycles
.timestamp_end
);
1926 /* read events discarded from header */
1927 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_string("events_discarded"));
1928 if (len_index
>= 0) {
1929 struct bt_definition
*field
;
1931 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1932 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1933 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1936 /* read packet_seq_num from header */
1937 len_index
= bt_struct_declaration_lookup_field_index(
1938 file_stream
->parent
.stream_packet_context
->declaration
,
1939 g_quark_from_string("packet_seq_num"));
1940 if (len_index
>= 0) {
1941 struct bt_definition
*field
;
1943 field
= bt_struct_definition_get_field_from_index(
1944 file_stream
->parent
.stream_packet_context
,
1946 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1949 /* Use file size for packet size */
1950 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1951 /* Use packet size if non-zero, else file size */
1952 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1955 /* Validate content size and packet size values */
1956 if (packet_index
.content_size
> packet_index
.packet_size
) {
1957 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1958 packet_index
.content_size
, packet_index
.packet_size
);
1962 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1963 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1964 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1968 if (packet_index
.content_size
< pos
->offset
) {
1969 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1973 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1974 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1978 /* Save position after header and context */
1979 packet_index
.data_offset
= pos
->offset
;
1981 /* add index to packet array */
1982 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1984 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1988 /* Retry with larger mapping */
1990 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1992 * Reached EOF, but still expecting header/context data.
1994 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1997 /* Double the mapping len, and retry */
1998 tmp_map_len
= packet_map_len
<< 1;
1999 if (tmp_map_len
>> 1 != packet_map_len
) {
2001 fprintf(stderr
, "[error] Packet mapping length overflow\n");
2004 packet_map_len
= tmp_map_len
;
2009 int create_stream_packet_index(struct ctf_trace
*td
,
2010 struct ctf_file_stream
*file_stream
)
2012 struct ctf_stream_pos
*pos
;
2013 struct stat filestats
;
2016 pos
= &file_stream
->pos
;
2018 ret
= fstat(pos
->fd
, &filestats
);
2022 /* Deal with empty files */
2023 if (!filestats
.st_size
) {
2024 if (file_stream
->parent
.trace_packet_header
2025 || file_stream
->parent
.stream_packet_context
) {
2027 * We expect a trace packet header and/or stream packet
2028 * context. Since a trace needs to have at least one
2029 * packet, empty files are therefore not accepted.
2031 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
2035 * Without trace packet header nor stream packet
2036 * context, a one-packet trace can indeed be empty. This
2037 * is only valid if there is only one stream class: 0.
2039 ret
= stream_assign_class(td
, file_stream
, 0);
2046 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
2047 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
2056 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
2060 if (td
->packet_header_decl
) {
2061 struct bt_definition
*definition
=
2062 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
2063 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
2068 stream
->trace_packet_header
=
2069 container_of(definition
, struct definition_struct
, p
);
2070 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
2076 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
2081 int import_stream_packet_index(struct ctf_trace
*td
,
2082 struct ctf_file_stream
*file_stream
)
2084 struct ctf_stream_pos
*pos
;
2085 struct ctf_packet_index
*ctf_index
= NULL
;
2086 struct ctf_packet_index_file_hdr index_hdr
;
2087 struct packet_index index
;
2088 uint32_t packet_index_len
, index_minor
;
2090 int first_packet
= 1;
2093 pos
= &file_stream
->pos
;
2095 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
2097 perror("read index file header");
2101 /* Check the index header */
2102 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
2103 fprintf(stderr
, "[error] wrong index magic\n");
2107 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
2108 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
2109 ".%" PRIu32
", supported %d.%d\n",
2110 be32toh(index_hdr
.index_major
),
2111 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
2116 index_minor
= be32toh(index_hdr
.index_minor
);
2118 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
2119 if (packet_index_len
== 0) {
2120 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2125 * Allocate the index length found in header, not internal
2128 ctf_index
= g_malloc0(packet_index_len
);
2129 while (fread(ctf_index
, packet_index_len
, 1,
2130 pos
->index_fp
) == 1) {
2132 struct ctf_stream_declaration
*stream
= NULL
;
2134 memset(&index
, 0, sizeof(index
));
2135 index
.offset
= be64toh(ctf_index
->offset
);
2136 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2137 index
.content_size
= be64toh(ctf_index
->content_size
);
2138 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2139 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2140 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2141 index
.events_discarded_len
= 64;
2142 index
.data_offset
= -1;
2143 stream_id
= be64toh(ctf_index
->stream_id
);
2144 if (index_minor
>= 1) {
2145 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2146 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2149 if (!first_packet
) {
2150 /* add index to packet array */
2151 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2155 file_stream
->parent
.stream_id
= stream_id
;
2156 if (stream_id
< td
->streams
->len
) {
2157 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2160 fprintf(stderr
, "[error] Stream %" PRIu64
2161 " is not declared in metadata.\n",
2166 file_stream
->parent
.stream_class
= stream
;
2167 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2171 /* add index to packet array */
2172 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2175 /* Index containing only the header. */
2176 if (!file_stream
->parent
.stream_class
) {
2189 * Note: many file streams can inherit from the same stream class
2190 * description (metadata).
2193 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2194 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2197 int ret
, fd
, closeret
;
2198 struct ctf_file_stream
*file_stream
;
2199 struct stat statbuf
;
2202 fd
= openat(td
->dirfd
, path
, flags
);
2204 perror("File stream openat()");
2209 /* Don't try to mmap subdirectories. Skip them, return success. */
2210 ret
= fstat(fd
, &statbuf
);
2212 perror("File stream fstat()");
2215 if (S_ISDIR(statbuf
.st_mode
)) {
2216 if (strncmp(path
, "index", 5) != 0) {
2217 fprintf(stderr
, "[warning] Skipping directory '%s' "
2218 "found in trace\n", path
);
2223 if (!statbuf
.st_size
) {
2224 /** Skip empty files. */
2226 goto fd_is_empty_file
;
2229 file_stream
= g_new0(struct ctf_file_stream
, 1);
2230 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2231 file_stream
->pos
.fd
= -1;
2232 file_stream
->pos
.index_fp
= NULL
;
2234 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2235 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2238 file_stream
->pos
.packet_seek
= packet_seek
;
2240 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2245 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2248 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2252 * For now, only a single clock per trace is supported.
2254 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2257 * Allocate the index name for this stream and try to open it.
2259 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2261 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2265 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2268 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2269 ret
= create_stream_packet_index(td
, file_stream
);
2271 fprintf(stderr
, "[error] Stream index creation error.\n");
2275 ret
= openat(td
->dirfd
, index_name
, flags
);
2277 perror("Index file openat()");
2281 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2282 if (!file_stream
->pos
.index_fp
) {
2283 perror("fdopen() error");
2286 ret
= import_stream_packet_index(td
, file_stream
);
2291 ret
= fclose(file_stream
->pos
.index_fp
);
2293 perror("close index");
2299 /* Add stream file to stream class */
2300 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2301 &file_stream
->parent
);
2305 if (file_stream
->pos
.index_fp
) {
2306 ret
= fclose(file_stream
->pos
.index_fp
);
2308 perror("close index");
2311 if (file_stream
->parent
.trace_packet_header
)
2312 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2316 closeret
= ctf_fini_pos(&file_stream
->pos
);
2318 fprintf(stderr
, "Error on ctf_fini_pos\n");
2320 g_free(file_stream
);
2324 closeret
= close(fd
);
2326 perror("Error on fd close");
2333 int ctf_open_trace_read(struct ctf_trace
*td
,
2334 const char *path
, int flags
,
2335 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2336 int whence
), FILE *metadata_fp
)
2338 struct ctf_scanner
*scanner
;
2340 struct dirent
*dirent
;
2341 struct dirent
*diriter
;
2348 /* Open trace directory */
2349 td
->dir
= opendir(path
);
2351 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2356 td
->dirfd
= open(path
, 0);
2357 if (td
->dirfd
< 0) {
2358 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2359 perror("Trace directory open");
2363 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2364 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2367 * Keep the metadata file separate.
2368 * Keep scanner object local to the open. We don't support
2369 * incremental metadata append for on-disk traces.
2371 scanner
= ctf_scanner_alloc();
2373 fprintf(stderr
, "[error] Error allocating scanner\n");
2375 goto error_metadata
;
2377 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2378 ctf_scanner_free(scanner
);
2380 if (ret
== -ENOENT
) {
2381 fprintf(stderr
, "[warning] Empty metadata.\n");
2383 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2384 goto error_metadata
;
2388 * Open each stream: for each file, try to open, check magic
2389 * number, and get the stream ID to add to the right location in
2393 pc_name_max
= fpathconf(td
->dirfd
, _PC_NAME_MAX
);
2394 if (pc_name_max
< 0) {
2395 perror("Error on fpathconf");
2396 fprintf(stderr
, "[error] Failed to get _PC_NAME_MAX for path \"%s\".\n", path
);
2398 goto error_metadata
;
2401 dirent_len
= offsetof(struct dirent
, d_name
) + pc_name_max
+ 1;
2403 dirent
= malloc(dirent_len
);
2406 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2408 fprintf(stderr
, "[error] Readdir error.\n");
2413 /* Ignore hidden files, ., .. and metadata. */
2414 if (!strncmp(diriter
->d_name
, ".", 1)
2415 || !strcmp(diriter
->d_name
, "..")
2416 || !strcmp(diriter
->d_name
, "metadata"))
2419 /* Ignore index files : *.idx */
2420 ext
= strrchr(diriter
->d_name
, '.');
2421 if (ext
&& (!strcmp(ext
, ".idx"))) {
2425 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2426 flags
, packet_seek
);
2428 fprintf(stderr
, "[error] Open file stream error.\n");
2439 closeret
= close(td
->dirfd
);
2441 perror("Error on fd close");
2444 closeret
= closedir(td
->dir
);
2446 perror("Error on closedir");
2453 * ctf_open_trace: Open a CTF trace and index it.
2454 * Note that the user must seek the trace after the open (using the iterator)
2455 * since the index creation read it entirely.
2458 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2459 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2460 int whence
), FILE *metadata_fp
)
2462 struct ctf_trace
*td
;
2466 * If packet_seek is NULL, we provide our default version.
2469 packet_seek
= ctf_packet_seek
;
2471 td
= g_new0(struct ctf_trace
, 1);
2475 init_trace_descriptor(&td
->parent
);
2477 switch (flags
& O_ACCMODE
) {
2480 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2483 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2488 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2491 fprintf(stderr
, "[error] Incorrect open flags.\n");
2495 ret
= trace_debug_info_create(td
);
2503 trace_debug_info_destroy(td
);
2510 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2511 struct bt_mmap_stream
*mmap_info
)
2513 pos
->mmap_offset
= 0;
2514 pos
->packet_size
= 0;
2515 pos
->content_size
= 0;
2516 pos
->content_size_loc
= NULL
;
2517 pos
->fd
= mmap_info
->fd
;
2518 pos
->base_mma
= NULL
;
2522 pos
->prot
= PROT_READ
;
2523 pos
->flags
= MAP_PRIVATE
;
2524 pos
->parent
.rw_table
= read_dispatch_table
;
2525 pos
->parent
.event_cb
= ctf_read_event
;
2526 pos
->priv
= mmap_info
->priv
;
2527 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2528 sizeof(struct packet_index
));
2532 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2533 struct ctf_file_stream
*file_stream
,
2534 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2537 struct ctf_stream_declaration
*stream
;
2541 /* Ask for the first packet to get the stream_id. */
2542 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2543 stream_id
= file_stream
->parent
.stream_id
;
2544 if (stream_id
>= td
->streams
->len
) {
2545 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2546 "in metadata.\n", stream_id
);
2550 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2552 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2553 "in metadata.\n", stream_id
);
2557 file_stream
->parent
.stream_class
= stream
;
2558 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2564 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2565 struct bt_mmap_stream
*mmap_info
,
2566 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2570 struct ctf_file_stream
*file_stream
;
2572 file_stream
= g_new0(struct ctf_file_stream
, 1);
2573 file_stream
->parent
.stream_id
= -1ULL;
2574 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2575 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2577 file_stream
->pos
.packet_seek
= packet_seek
;
2579 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2584 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2589 * For now, only a single clock per trace is supported.
2591 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2593 /* Add stream file to stream class */
2594 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2595 &file_stream
->parent
);
2599 if (file_stream
->parent
.trace_packet_header
)
2600 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2602 g_free(file_stream
);
2607 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2608 struct bt_mmap_stream_list
*mmap_list
,
2609 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2614 struct bt_mmap_stream
*mmap_info
;
2616 td
->scanner
= ctf_scanner_alloc();
2618 fprintf(stderr
, "[error] Error allocating scanner\n");
2622 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2624 if (ret
== -ENOENT
) {
2625 fprintf(stderr
, "[warning] Empty metadata.\n");
2631 * for each stream, try to open, check magic number, and get the
2632 * stream ID to add to the right location in the stream array.
2634 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2635 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2637 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2644 ctf_scanner_free(td
->scanner
);
2649 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2650 struct bt_mmap_stream_list
*mmap_list
,
2651 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2655 struct ctf_trace
*td
;
2659 fprintf(stderr
, "[error] No metadata file pointer associated, "
2660 "required for mmap parsing\n");
2664 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2667 td
= g_new0(struct ctf_trace
, 1);
2669 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2673 ret
= trace_debug_info_create(td
);
2686 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2689 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2695 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2698 /* for each stream_class */
2699 for (i
= 0; i
< td
->streams
->len
; i
++) {
2700 struct ctf_stream_declaration
*stream_class
;
2702 stream_class
= g_ptr_array_index(td
->streams
, i
);
2705 /* for each stream */
2706 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2707 struct ctf_stream_definition
*stream
;
2709 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2712 ret
= copy_event_declarations_stream_class_to_stream(td
,
2713 stream_class
, stream
);
2722 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2725 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2727 /* for each stream_class */
2728 for (i
= 0; i
< td
->streams
->len
; i
++) {
2729 struct ctf_stream_declaration
*stream_class
;
2731 stream_class
= g_ptr_array_index(td
->streams
, i
);
2734 /* for each file_stream */
2735 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2736 struct ctf_stream_definition
*stream
;
2737 struct ctf_stream_pos
*stream_pos
;
2738 struct ctf_file_stream
*cfs
;
2740 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2743 cfs
= container_of(stream
, struct ctf_file_stream
,
2745 stream_pos
= &cfs
->pos
;
2746 if (!stream_pos
->packet_index
)
2749 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2750 struct packet_index
*index
;
2752 index
= &g_array_index(stream_pos
->packet_index
,
2753 struct packet_index
, k
);
2754 index
->ts_real
.timestamp_begin
=
2755 ctf_get_real_timestamp(stream
,
2756 index
->ts_cycles
.timestamp_begin
);
2757 index
->ts_real
.timestamp_end
=
2758 ctf_get_real_timestamp(stream
,
2759 index
->ts_cycles
.timestamp_end
);
2767 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2771 ret
= ctf_fini_pos(&file_stream
->pos
);
2773 fprintf(stderr
, "Error on ctf_fini_pos\n");
2776 if (file_stream
->pos
.fd
>= 0) {
2777 ret
= close(file_stream
->pos
.fd
);
2779 perror("Error closing file fd");
2787 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2789 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2795 for (i
= 0; i
< td
->streams
->len
; i
++) {
2796 struct ctf_stream_declaration
*stream
;
2799 stream
= g_ptr_array_index(td
->streams
, i
);
2802 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2803 struct ctf_file_stream
*file_stream
;
2804 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2805 struct ctf_file_stream
, parent
);
2806 ret
= ctf_close_file_stream(file_stream
);
2812 ctf_destroy_metadata(td
);
2813 ctf_scanner_free(td
->scanner
);
2814 if (td
->dirfd
>= 0) {
2815 ret
= close(td
->dirfd
);
2817 perror("Error closing dirfd");
2822 ret
= closedir(td
->dir
);
2824 perror("Error closedir");
2828 free(td
->metadata_string
);
2829 trace_debug_info_destroy(td
);
2835 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2836 struct bt_context
*ctx
)
2838 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2841 td
->parent
.ctx
= ctx
;
2845 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2846 struct bt_trace_handle
*handle
)
2848 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2851 td
->parent
.handle
= handle
;
2855 void __attribute__((constructor
)) ctf_init(void)
2859 ctf_format
.name
= g_quark_from_string("ctf");
2860 ret
= bt_register_format(&ctf_format
);
2865 void __attribute__((destructor
)) ctf_exit(void)
2867 bt_unregister_format(&ctf_format
);