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/ctf/ctf-index.h>
43 #include <sys/types.h>
51 #include "metadata/ctf-scanner.h"
52 #include "metadata/ctf-parser.h"
53 #include "metadata/ctf-ast.h"
54 #include "events-private.h"
55 #include <babeltrace/compat/memstream.h>
57 #define LOG2_CHAR_BIT 3
60 * Length of first attempt at mapping a packet header, in bits.
62 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
65 * Lenght of packet to write, in bits.
67 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
70 #define min(a, b) (((a) < (b)) ? (a) : (b))
73 #define NSEC_PER_SEC 1000000000ULL
75 #define INDEX_PATH "./index/%s.idx"
82 uint64_t opt_clock_offset
;
83 uint64_t opt_clock_offset_ns
;
88 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
89 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
93 struct bt_trace_descriptor
*ctf_open_mmap_trace(
94 struct bt_mmap_stream_list
*mmap_list
,
95 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
99 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
100 struct bt_context
*ctx
);
102 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
103 struct bt_trace_handle
*handle
);
106 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
108 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
109 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
111 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
112 struct bt_trace_handle
*handle
, enum bt_clock_type type
);
114 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
117 rw_dispatch read_dispatch_table
[] = {
118 [ CTF_TYPE_INTEGER
] = ctf_integer_read
,
119 [ CTF_TYPE_FLOAT
] = ctf_float_read
,
120 [ CTF_TYPE_ENUM
] = ctf_enum_read
,
121 [ CTF_TYPE_STRING
] = ctf_string_read
,
122 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
123 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
124 [ CTF_TYPE_ARRAY
] = ctf_array_read
,
125 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_read
,
129 rw_dispatch write_dispatch_table
[] = {
130 [ CTF_TYPE_INTEGER
] = ctf_integer_write
,
131 [ CTF_TYPE_FLOAT
] = ctf_float_write
,
132 [ CTF_TYPE_ENUM
] = ctf_enum_write
,
133 [ CTF_TYPE_STRING
] = ctf_string_write
,
134 [ CTF_TYPE_STRUCT
] = ctf_struct_rw
,
135 [ CTF_TYPE_VARIANT
] = ctf_variant_rw
,
136 [ CTF_TYPE_ARRAY
] = ctf_array_write
,
137 [ CTF_TYPE_SEQUENCE
] = ctf_sequence_write
,
141 struct bt_format ctf_format
= {
142 .open_trace
= ctf_open_trace
,
143 .open_mmap_trace
= ctf_open_mmap_trace
,
144 .close_trace
= ctf_close_trace
,
145 .set_context
= ctf_set_context
,
146 .set_handle
= ctf_set_handle
,
147 .timestamp_begin
= ctf_timestamp_begin
,
148 .timestamp_end
= ctf_timestamp_end
,
149 .convert_index_timestamp
= ctf_convert_index_timestamp
,
153 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
154 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
156 struct ctf_trace
*tin
;
157 uint64_t begin
= ULLONG_MAX
;
160 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
165 /* for each stream_class */
166 for (i
= 0; i
< tin
->streams
->len
; i
++) {
167 struct ctf_stream_declaration
*stream_class
;
169 stream_class
= g_ptr_array_index(tin
->streams
, i
);
172 /* for each file_stream */
173 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
174 struct ctf_stream_definition
*stream
;
175 struct ctf_file_stream
*cfs
;
176 struct ctf_stream_pos
*stream_pos
;
177 struct packet_index
*index
;
179 stream
= g_ptr_array_index(stream_class
->streams
, j
);
180 cfs
= container_of(stream
, struct ctf_file_stream
,
182 stream_pos
= &cfs
->pos
;
184 if (!stream_pos
->packet_index
)
187 if (stream_pos
->packet_index
->len
<= 0)
190 index
= &g_array_index(stream_pos
->packet_index
,
192 stream_pos
->packet_index
->len
- 1);
193 if (type
== BT_CLOCK_REAL
) {
194 if (index
->ts_real
.timestamp_begin
< begin
)
195 begin
= index
->ts_real
.timestamp_begin
;
196 } else if (type
== BT_CLOCK_CYCLES
) {
197 if (index
->ts_cycles
.timestamp_begin
< begin
)
198 begin
= index
->ts_cycles
.timestamp_begin
;
212 uint64_t ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
213 struct bt_trace_handle
*handle
, enum bt_clock_type type
)
215 struct ctf_trace
*tin
;
219 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
224 /* for each stream_class */
225 for (i
= 0; i
< tin
->streams
->len
; i
++) {
226 struct ctf_stream_declaration
*stream_class
;
228 stream_class
= g_ptr_array_index(tin
->streams
, i
);
231 /* for each file_stream */
232 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
233 struct ctf_stream_definition
*stream
;
234 struct ctf_file_stream
*cfs
;
235 struct ctf_stream_pos
*stream_pos
;
236 struct packet_index
*index
;
238 stream
= g_ptr_array_index(stream_class
->streams
, j
);
239 cfs
= container_of(stream
, struct ctf_file_stream
,
241 stream_pos
= &cfs
->pos
;
243 if (!stream_pos
->packet_index
)
246 if (stream_pos
->packet_index
->len
<= 0)
249 index
= &g_array_index(stream_pos
->packet_index
,
251 stream_pos
->packet_index
->len
- 1);
252 if (type
== BT_CLOCK_REAL
) {
253 if (index
->ts_real
.timestamp_end
> end
)
254 end
= index
->ts_real
.timestamp_end
;
255 } else if (type
== BT_CLOCK_CYCLES
) {
256 if (index
->ts_cycles
.timestamp_end
> end
)
257 end
= index
->ts_cycles
.timestamp_end
;
271 * Update stream current timestamp
274 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
275 struct definition_integer
*integer_definition
)
277 struct declaration_integer
*integer_declaration
=
278 integer_definition
->declaration
;
279 uint64_t oldval
, newval
, updateval
;
281 if (unlikely(integer_declaration
->len
== 64)) {
282 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
283 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
284 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
285 stream
->prev_cycles_timestamp
);
286 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
287 stream
->cycles_timestamp
);
291 oldval
= stream
->cycles_timestamp
;
292 oldval
&= (1ULL << integer_declaration
->len
) - 1;
293 newval
= integer_definition
->value
._unsigned
;
294 /* Test for overflow by comparing low bits */
296 newval
+= 1ULL << integer_declaration
->len
;
297 /* updateval contains old high bits, and new low bits (sum) */
298 updateval
= stream
->cycles_timestamp
;
299 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
301 stream
->prev_cycles_timestamp
= stream
->cycles_timestamp
;
302 stream
->cycles_timestamp
= updateval
;
304 /* convert to real timestamp */
305 stream
->prev_real_timestamp
= ctf_get_real_timestamp(stream
,
306 stream
->prev_cycles_timestamp
);
307 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
308 stream
->cycles_timestamp
);
312 * Print timestamp, rescaling clock frequency to nanoseconds and
313 * applying offsets as needed (unix time).
316 void ctf_print_timestamp_real(FILE *fp
,
317 struct ctf_stream_definition
*stream
,
320 uint64_t ts_sec
= 0, ts_nsec
;
324 /* Add command-line offset in ns*/
325 ts_nsec
+= opt_clock_offset_ns
;
327 /* Add command-line offset */
328 ts_sec
+= opt_clock_offset
;
330 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
331 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
333 if (!opt_clock_seconds
) {
335 time_t time_s
= (time_t) ts_sec
;
337 if (!opt_clock_gmt
) {
340 res
= localtime_r(&time_s
, &tm
);
342 fprintf(stderr
, "[warning] Unable to get localtime.\n");
348 res
= gmtime_r(&time_s
, &tm
);
350 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
354 if (opt_clock_date
) {
358 /* Print date and time */
359 res
= strftime(timestr
, sizeof(timestr
),
362 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
365 fprintf(fp
, "%s", timestr
);
367 /* Print time in HH:MM:SS.ns */
368 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
369 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
373 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
381 * Print timestamp, in cycles
384 void ctf_print_timestamp_cycles(FILE *fp
,
385 struct ctf_stream_definition
*stream
,
388 fprintf(fp
, "%020" PRIu64
, timestamp
);
391 void ctf_print_timestamp(FILE *fp
,
392 struct ctf_stream_definition
*stream
,
395 if (opt_clock_cycles
) {
396 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
398 ctf_print_timestamp_real(fp
, stream
, timestamp
);
403 void print_uuid(FILE *fp
, unsigned char *uuid
)
407 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
408 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
411 void ctf_print_discarded(FILE *fp
, struct ctf_stream_definition
*stream
,
414 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events %sbetween [",
415 stream
->events_discarded
,
416 end_stream
? "at end of stream " : "");
417 if (opt_clock_cycles
) {
418 ctf_print_timestamp(fp
, stream
,
419 stream
->prev_cycles_timestamp
);
420 fprintf(fp
, "] and [");
421 ctf_print_timestamp(fp
, stream
,
422 stream
->prev_cycles_timestamp_end
);
424 ctf_print_timestamp(fp
, stream
,
425 stream
->prev_real_timestamp
);
426 fprintf(fp
, "] and [");
427 ctf_print_timestamp(fp
, stream
,
428 stream
->prev_real_timestamp_end
);
430 fprintf(fp
, "] in trace UUID ");
431 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
432 if (stream
->stream_class
->trace
->parent
.path
[0])
433 fprintf(fp
, ", at path: \"%s\"",
434 stream
->stream_class
->trace
->parent
.path
);
436 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
438 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
440 fprintf(fp
, "You should consider recording a new trace with larger "
441 "buffers or with fewer events enabled.\n");
446 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
448 struct ctf_stream_pos
*pos
=
449 container_of(ppos
, struct ctf_stream_pos
, parent
);
450 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
451 struct ctf_event_definition
*event
;
455 /* We need to check for EOF here for empty files. */
456 if (unlikely(pos
->offset
== EOF
))
459 ctf_pos_get_event(pos
);
461 /* save the current position as a restore point */
462 pos
->last_offset
= pos
->offset
;
465 * This is the EOF check after we've advanced the position in
468 if (unlikely(pos
->offset
== EOF
))
471 /* Stream is inactive for now (live reading). */
472 if (unlikely(pos
->content_size
== 0))
476 * Packet seeked to by ctf_pos_get_event() only contains
477 * headers, no event. Consider stream as inactive (live
480 if (unlikely(pos
->data_offset
== pos
->content_size
))
483 assert(pos
->offset
< pos
->content_size
);
485 /* Read event header */
486 if (likely(stream
->stream_event_header
)) {
487 struct definition_integer
*integer_definition
;
488 struct bt_definition
*variant
;
490 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
493 /* lookup event id */
494 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
495 if (integer_definition
) {
496 id
= integer_definition
->value
._unsigned
;
498 struct definition_enum
*enum_definition
;
500 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
501 if (enum_definition
) {
502 id
= enum_definition
->integer
->value
._unsigned
;
506 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
508 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
509 if (integer_definition
) {
510 id
= integer_definition
->value
._unsigned
;
513 stream
->event_id
= id
;
515 /* lookup timestamp */
516 stream
->has_timestamp
= 0;
517 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
518 if (integer_definition
) {
519 ctf_update_timestamp(stream
, integer_definition
);
520 stream
->has_timestamp
= 1;
523 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
524 if (integer_definition
) {
525 ctf_update_timestamp(stream
, integer_definition
);
526 stream
->has_timestamp
= 1;
532 /* Read stream-declared event context */
533 if (stream
->stream_event_context
) {
534 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
539 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
540 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
543 event
= g_ptr_array_index(stream
->events_by_id
, id
);
544 if (unlikely(!event
)) {
545 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
549 /* Read event-declared event context */
550 if (event
->event_context
) {
551 ret
= generic_rw(ppos
, &event
->event_context
->p
);
556 /* Read event payload */
557 if (likely(event
->event_fields
)) {
558 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
563 if (pos
->last_offset
== pos
->offset
) {
564 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
571 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
576 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
578 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
579 struct ctf_event_definition
*event
;
583 id
= stream
->event_id
;
585 /* print event header */
586 if (likely(stream
->stream_event_header
)) {
587 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
592 /* print stream-declared event context */
593 if (stream
->stream_event_context
) {
594 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
599 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
600 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
603 event
= g_ptr_array_index(stream
->events_by_id
, id
);
604 if (unlikely(!event
)) {
605 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
609 /* print event-declared event context */
610 if (event
->event_context
) {
611 ret
= generic_rw(pos
, &event
->event_context
->p
);
616 /* Read and print event payload */
617 if (likely(event
->event_fields
)) {
618 ret
= generic_rw(pos
, &event
->event_fields
->p
);
626 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
631 * One side-effect of this function is to unmap pos mmap base if one is
635 int find_data_offset(struct ctf_stream_pos
*pos
,
636 struct ctf_file_stream
*file_stream
,
637 struct packet_index
*packet_index
)
639 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
640 struct stat filestats
;
644 pos
= &file_stream
->pos
;
646 ret
= fstat(pos
->fd
, &filestats
);
649 filesize
= filestats
.st_size
;
651 /* Deal with empty files */
657 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
658 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
663 ret
= munmap_align(pos
->base_mma
);
665 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
669 pos
->base_mma
= NULL
;
671 /* map new base. Need mapping length from header. */
672 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
673 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
674 assert(pos
->base_mma
!= MAP_FAILED
);
676 pos
->content_size
= packet_map_len
;
677 pos
->packet_size
= packet_map_len
;
678 pos
->offset
= 0; /* Position of the packet header */
680 /* update trace_packet_header and stream_packet_context */
681 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
682 /* Read packet header */
683 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
689 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
690 /* Read packet context */
691 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
697 packet_index
->data_offset
= pos
->offset
;
700 ret
= munmap_align(pos
->base_mma
);
702 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
706 pos
->base_mma
= NULL
;
710 /* Retry with larger mapping */
712 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
714 * Reached EOF, but still expecting header/context data.
716 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
719 /* Double the mapping len, and retry */
720 tmp_map_len
= packet_map_len
<< 1;
721 if (tmp_map_len
>> 1 != packet_map_len
) {
723 fprintf(stderr
, "[error] Packet mapping length overflow\n");
726 packet_map_len
= tmp_map_len
;
731 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
732 int fd
, int open_flags
)
736 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
737 sizeof(struct packet_index
));
739 pos
->packet_index
= NULL
;
741 switch (open_flags
& O_ACCMODE
) {
743 pos
->prot
= PROT_READ
;
744 pos
->flags
= MAP_PRIVATE
;
745 pos
->parent
.rw_table
= read_dispatch_table
;
746 pos
->parent
.event_cb
= ctf_read_event
;
747 pos
->parent
.trace
= trace
;
750 pos
->prot
= PROT_WRITE
; /* Write has priority */
751 pos
->flags
= MAP_SHARED
;
752 pos
->parent
.rw_table
= write_dispatch_table
;
753 pos
->parent
.event_cb
= ctf_write_event
;
754 pos
->parent
.trace
= trace
;
756 ctf_packet_seek(&pos
->parent
, 0, SEEK_SET
); /* position for write */
764 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
766 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
767 *pos
->content_size_loc
= pos
->offset
;
772 ret
= munmap_align(pos
->base_mma
);
774 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
779 if (pos
->packet_index
)
780 (void) g_array_free(pos
->packet_index
, TRUE
);
784 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
785 struct packet_index
*prev_index
,
786 struct packet_index
*cur_index
)
788 uint64_t events_discarded_diff
;
790 /* Update packet index time information */
791 stream
->prev_cycles_timestamp_end
=
792 cur_index
->ts_cycles
.timestamp_end
;
793 stream
->prev_cycles_timestamp
=
794 cur_index
->ts_cycles
.timestamp_begin
;
795 stream
->prev_real_timestamp_end
=
796 cur_index
->ts_real
.timestamp_end
;
797 stream
->prev_real_timestamp
=
798 cur_index
->ts_real
.timestamp_begin
;
800 stream
->prev_real_timestamp
=
801 stream
->real_timestamp
;
802 stream
->prev_cycles_timestamp
=
803 stream
->cycles_timestamp
;
805 /* Update packet index discarded event information */
806 events_discarded_diff
= cur_index
->events_discarded
;
808 events_discarded_diff
-= prev_index
->events_discarded
;
810 * Deal with 32-bit wrap-around if the tracer provided a
813 if (prev_index
->events_discarded_len
== 32) {
814 events_discarded_diff
= (uint32_t) events_discarded_diff
;
817 stream
->events_discarded
= events_discarded_diff
;
821 * for SEEK_CUR: go to next packet.
822 * for SEEK_SET: go to packet numer (index).
824 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
826 struct ctf_stream_pos
*pos
=
827 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
828 struct ctf_file_stream
*file_stream
=
829 container_of(pos
, struct ctf_file_stream
, pos
);
832 struct packet_index
*packet_index
;
836 case SEEK_SET
: /* Fall-through */
842 if (pos
->prot
== PROT_WRITE
&& pos
->content_size_loc
)
843 *pos
->content_size_loc
= pos
->offset
;
847 ret
= munmap_align(pos
->base_mma
);
849 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
853 pos
->base_mma
= NULL
;
857 * The caller should never ask for ctf_move_pos across packets,
858 * except to get exactly at the beginning of the next packet.
860 if (pos
->prot
== PROT_WRITE
) {
863 /* The writer will add padding */
864 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
867 assert(index
== 0); /* only seek supported for now */
873 pos
->content_size
= -1U; /* Unknown at this point */
874 pos
->packet_size
= WRITE_PACKET_LEN
;
875 off
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
876 pos
->packet_size
/ CHAR_BIT
);
884 struct packet_index
*prev_index
= NULL
;
886 if (pos
->offset
== EOF
) {
889 assert(pos
->cur_index
< pos
->packet_index
->len
);
890 packet_index
= &g_array_index(pos
->packet_index
,
891 struct packet_index
, pos
->cur_index
);
892 if (pos
->cur_index
> 0) {
893 prev_index
= &g_array_index(pos
->packet_index
,
897 ctf_update_current_packet_index(&file_stream
->parent
,
898 prev_index
, packet_index
);
900 /* The reader will expect us to skip padding */
905 if (index
>= pos
->packet_index
->len
) {
909 packet_index
= &g_array_index(pos
->packet_index
,
910 struct packet_index
, index
);
911 pos
->last_events_discarded
= packet_index
->events_discarded
;
912 pos
->cur_index
= index
;
913 file_stream
->parent
.prev_real_timestamp
= 0;
914 file_stream
->parent
.prev_real_timestamp_end
= 0;
915 file_stream
->parent
.prev_cycles_timestamp
= 0;
916 file_stream
->parent
.prev_cycles_timestamp_end
= 0;
921 if (pos
->cur_index
>= pos
->packet_index
->len
) {
923 * We need to check if we are in trace read or
924 * called from packet indexing. In this last
925 * case, the collection is not there, so we
926 * cannot print the timestamps.
928 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
930 * When a stream reaches the end of the
931 * file, we need to show the number of
932 * events discarded ourselves, because
933 * there is no next event scheduled to
934 * be printed in the output.
936 if (file_stream
->parent
.events_discarded
) {
938 ctf_print_discarded(stderr
,
939 &file_stream
->parent
,
941 file_stream
->parent
.events_discarded
= 0;
947 packet_index
= &g_array_index(pos
->packet_index
,
950 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
952 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
954 /* Lookup context/packet size in index */
955 if (packet_index
->data_offset
== -1) {
956 ret
= find_data_offset(pos
, file_stream
, packet_index
);
961 pos
->content_size
= packet_index
->content_size
;
962 pos
->packet_size
= packet_index
->packet_size
;
963 pos
->mmap_offset
= packet_index
->offset
;
964 pos
->data_offset
= packet_index
->data_offset
;
965 if (pos
->data_offset
< packet_index
->content_size
) {
966 pos
->offset
= 0; /* will read headers */
967 } else if (pos
->data_offset
== packet_index
->content_size
) {
969 pos
->offset
= packet_index
->data_offset
;
971 goto read_next_packet
;
977 /* map new base. Need mapping length from header. */
978 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
979 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
980 if (pos
->base_mma
== MAP_FAILED
) {
981 fprintf(stderr
, "[error] mmap error %s.\n",
986 /* update trace_packet_header and stream_packet_context */
987 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.trace_packet_header
) {
988 /* Read packet header */
989 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
992 if (pos
->prot
!= PROT_WRITE
&& file_stream
->parent
.stream_packet_context
) {
993 /* Read packet context */
994 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1000 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1006 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1010 if (magic
== TSDL_MAGIC
) {
1012 td
->byte_order
= BYTE_ORDER
;
1013 CTF_TRACE_SET_FIELD(td
, byte_order
);
1014 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1016 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1017 LITTLE_ENDIAN
: BIG_ENDIAN
;
1018 CTF_TRACE_SET_FIELD(td
, byte_order
);
1026 * Returns 0 on success, -1 on error.
1029 int check_version(unsigned int major
, unsigned int minor
)
1044 /* eventually return an error instead of warning */
1046 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1052 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1055 struct metadata_packet_header header
;
1056 size_t readlen
, writelen
, toread
;
1057 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1060 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1064 if (td
->byte_order
!= BYTE_ORDER
) {
1065 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1066 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1067 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1068 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1070 if (header
.checksum
)
1071 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1072 if (header
.compression_scheme
) {
1073 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1074 header
.compression_scheme
);
1077 if (header
.encryption_scheme
) {
1078 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1079 header
.encryption_scheme
);
1082 if (header
.checksum_scheme
) {
1083 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1084 header
.checksum_scheme
);
1087 if (check_version(header
.major
, header
.minor
) < 0)
1089 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1090 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1091 CTF_TRACE_SET_FIELD(td
, uuid
);
1093 if (babeltrace_uuid_compare(header
.uuid
, td
->uuid
))
1097 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1100 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1103 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1108 if (babeltrace_debug
) {
1109 buf
[readlen
] = '\0';
1110 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1114 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1115 if (writelen
< readlen
) {
1125 ret
= 0; /* continue reading next packet */
1132 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1133 ret
= fseek(in
, toread
, SEEK_CUR
);
1135 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1142 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1146 size_t size
, buflen
;
1151 * Using strlen on *buf instead of size of open_memstream
1152 * because its size includes garbage at the end (after final
1153 * \0). This is the allocated size, not the actual string size.
1155 out
= babeltrace_open_memstream(buf
, &size
);
1157 perror("Metadata open_memstream");
1161 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1170 /* close to flush the buffer */
1171 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1175 perror("babeltrace_flush_memstream");
1177 closeret
= fclose(in
);
1179 perror("Error in fclose");
1185 perror("Error in fclose");
1187 /* open for reading */
1188 buflen
= strlen(*buf
);
1193 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1195 perror("Metadata fmemopen");
1202 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1203 struct ctf_scanner
*scanner
, int append
)
1205 struct ctf_file_stream
*metadata_stream
;
1208 int ret
= 0, closeret
;
1210 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1211 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1215 metadata_stream
->pos
.fd
= -1;
1217 td
->metadata
= &metadata_stream
->parent
;
1218 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1219 if (metadata_stream
->pos
.fd
< 0) {
1220 fprintf(stderr
, "Unable to open metadata.\n");
1225 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1227 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1228 perror("Metadata stream open");
1232 /* fd now belongs to fp */
1233 metadata_stream
->pos
.fd
= -1;
1235 if (babeltrace_debug
)
1238 if (packet_metadata(td
, fp
)) {
1239 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1241 /* Warn about empty metadata */
1242 fprintf(stderr
, "[warning] Empty metadata.\n");
1245 td
->metadata_string
= buf
;
1246 td
->metadata_packetized
= 1;
1249 unsigned int major
, minor
;
1252 td
->byte_order
= BYTE_ORDER
;
1254 /* Check text-only metadata header and version */
1255 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1257 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1258 if (check_version(major
, minor
) < 0) {
1266 ret
= ctf_scanner_append_ast(scanner
, fp
);
1268 fprintf(stderr
, "[error] Error creating AST\n");
1272 if (babeltrace_debug
) {
1273 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1275 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1280 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1282 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1285 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1286 td
, td
->byte_order
);
1288 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1293 closeret
= fclose(fp
);
1295 perror("Error on fclose");
1299 if (metadata_stream
->pos
.fd
>= 0) {
1300 closeret
= close(metadata_stream
->pos
.fd
);
1302 perror("Error on metadata stream fd close");
1307 g_free(metadata_stream
);
1312 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1313 struct ctf_stream_definition
*stream
,
1314 struct ctf_event_declaration
*event
)
1316 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1318 if (event
->context_decl
) {
1319 struct bt_definition
*definition
=
1320 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1321 stream
->parent_def_scope
, 0, 0, "event.context");
1325 stream_event
->event_context
= container_of(definition
,
1326 struct definition_struct
, p
);
1327 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1329 if (event
->fields_decl
) {
1330 struct bt_definition
*definition
=
1331 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1332 stream
->parent_def_scope
, 0, 0, "event.fields");
1336 stream_event
->event_fields
= container_of(definition
,
1337 struct definition_struct
, p
);
1338 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1340 stream_event
->stream
= stream
;
1341 return stream_event
;
1344 if (stream_event
->event_fields
)
1345 bt_definition_unref(&stream_event
->event_fields
->p
);
1346 if (stream_event
->event_context
)
1347 bt_definition_unref(&stream_event
->event_context
->p
);
1348 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1349 g_quark_to_string(event
->name
));
1354 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1355 struct ctf_stream_declaration
*stream_class
,
1356 struct ctf_stream_definition
*stream
)
1358 size_t def_size
, class_size
, i
;
1361 def_size
= stream
->events_by_id
->len
;
1362 class_size
= stream_class
->events_by_id
->len
;
1364 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1365 for (i
= def_size
; i
< class_size
; i
++) {
1366 struct ctf_event_declaration
*event
=
1367 g_ptr_array_index(stream_class
->events_by_id
, i
);
1368 struct ctf_event_definition
*stream_event
;
1372 stream_event
= create_event_definitions(td
, stream
, event
);
1373 if (!stream_event
) {
1377 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1384 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1386 struct ctf_stream_declaration
*stream_class
;
1390 if (stream
->stream_definitions_created
)
1393 stream_class
= stream
->stream_class
;
1395 if (stream_class
->packet_context_decl
) {
1396 struct bt_definition
*definition
=
1397 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1398 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1403 stream
->stream_packet_context
= container_of(definition
,
1404 struct definition_struct
, p
);
1405 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1407 if (stream_class
->event_header_decl
) {
1408 struct bt_definition
*definition
=
1409 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1410 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1415 stream
->stream_event_header
=
1416 container_of(definition
, struct definition_struct
, p
);
1417 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1419 if (stream_class
->event_context_decl
) {
1420 struct bt_definition
*definition
=
1421 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1422 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1427 stream
->stream_event_context
=
1428 container_of(definition
, struct definition_struct
, p
);
1429 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1431 stream
->events_by_id
= g_ptr_array_new();
1432 ret
= copy_event_declarations_stream_class_to_stream(td
,
1433 stream_class
, stream
);
1439 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1440 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1442 g_free(stream_event
);
1444 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1446 if (stream
->stream_event_context
)
1447 bt_definition_unref(&stream
->stream_event_context
->p
);
1448 if (stream
->stream_event_header
)
1449 bt_definition_unref(&stream
->stream_event_header
->p
);
1450 if (stream
->stream_packet_context
)
1451 bt_definition_unref(&stream
->stream_packet_context
->p
);
1452 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1453 stream_class
->stream_id
, strerror(-ret
));
1458 int stream_assign_class(struct ctf_trace
*td
,
1459 struct ctf_file_stream
*file_stream
,
1462 struct ctf_stream_declaration
*stream
;
1465 file_stream
->parent
.stream_id
= stream_id
;
1466 if (stream_id
>= td
->streams
->len
) {
1467 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1470 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1472 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1475 file_stream
->parent
.stream_class
= stream
;
1476 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1483 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1484 struct ctf_trace
*td
,
1485 struct ctf_file_stream
*file_stream
,
1488 struct packet_index packet_index
;
1489 uint64_t stream_id
= 0;
1490 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1491 int first_packet
= 0;
1496 if (!pos
->mmap_offset
) {
1500 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1501 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1504 if (pos
->base_mma
) {
1505 /* unmap old base */
1506 ret
= munmap_align(pos
->base_mma
);
1508 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1512 pos
->base_mma
= NULL
;
1514 /* map new base. Need mapping length from header. */
1515 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1516 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1517 assert(pos
->base_mma
!= MAP_FAILED
);
1519 * Use current mapping size as temporary content and packet
1522 pos
->content_size
= packet_map_len
;
1523 pos
->packet_size
= packet_map_len
;
1524 pos
->offset
= 0; /* Position of the packet header */
1526 packet_index
.offset
= pos
->mmap_offset
;
1527 packet_index
.content_size
= 0;
1528 packet_index
.packet_size
= 0;
1529 packet_index
.ts_real
.timestamp_begin
= 0;
1530 packet_index
.ts_real
.timestamp_end
= 0;
1531 packet_index
.ts_cycles
.timestamp_begin
= 0;
1532 packet_index
.ts_cycles
.timestamp_end
= 0;
1533 packet_index
.events_discarded
= 0;
1534 packet_index
.events_discarded_len
= 0;
1536 /* read and check header, set stream id (and check) */
1537 if (file_stream
->parent
.trace_packet_header
) {
1538 /* Read packet header */
1539 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1543 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1546 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1547 if (len_index
>= 0) {
1548 struct bt_definition
*field
;
1551 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1552 magic
= bt_get_unsigned_int(field
);
1553 if (magic
!= CTF_MAGIC
) {
1554 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1556 file_stream
->pos
.packet_index
->len
,
1557 (ssize_t
) pos
->mmap_offset
);
1563 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1564 if (len_index
>= 0) {
1565 struct definition_array
*defarray
;
1566 struct bt_definition
*field
;
1568 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1570 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1571 assert(field
->declaration
->id
== CTF_TYPE_ARRAY
);
1572 defarray
= container_of(field
, struct definition_array
, p
);
1573 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1575 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1576 struct bt_definition
*elem
;
1578 elem
= bt_array_index(defarray
, i
);
1579 uuidval
[i
] = bt_get_unsigned_int(elem
);
1581 ret
= babeltrace_uuid_compare(td
->uuid
, uuidval
);
1583 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1588 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1589 if (len_index
>= 0) {
1590 struct bt_definition
*field
;
1592 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1593 stream_id
= bt_get_unsigned_int(field
);
1597 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1598 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1600 file_stream
->parent
.stream_id
);
1604 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1609 if (file_stream
->parent
.stream_packet_context
) {
1610 /* Read packet context */
1611 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1615 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1618 /* read packet size from header */
1619 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1620 if (len_index
>= 0) {
1621 struct bt_definition
*field
;
1623 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1624 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1626 /* Use file size for packet size */
1627 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1630 /* read content size from header */
1631 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1632 if (len_index
>= 0) {
1633 struct bt_definition
*field
;
1635 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1636 packet_index
.content_size
= bt_get_unsigned_int(field
);
1638 /* Use packet size if non-zero, else file size */
1639 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1642 /* read timestamp begin from header */
1643 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1644 if (len_index
>= 0) {
1645 struct bt_definition
*field
;
1647 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1648 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1649 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1650 packet_index
.ts_real
.timestamp_begin
=
1651 ctf_get_real_timestamp(
1652 &file_stream
->parent
,
1653 packet_index
.ts_cycles
.timestamp_begin
);
1657 /* read timestamp end from header */
1658 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1659 if (len_index
>= 0) {
1660 struct bt_definition
*field
;
1662 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1663 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1664 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1665 packet_index
.ts_real
.timestamp_end
=
1666 ctf_get_real_timestamp(
1667 &file_stream
->parent
,
1668 packet_index
.ts_cycles
.timestamp_end
);
1672 /* read events discarded from header */
1673 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1674 if (len_index
>= 0) {
1675 struct bt_definition
*field
;
1677 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1678 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1679 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1682 /* Use file size for packet size */
1683 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1684 /* Use packet size if non-zero, else file size */
1685 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1688 /* Validate content size and packet size values */
1689 if (packet_index
.content_size
> packet_index
.packet_size
) {
1690 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1691 packet_index
.content_size
, packet_index
.packet_size
);
1695 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1696 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1697 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1701 if (packet_index
.content_size
< pos
->offset
) {
1702 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1706 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1707 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1711 /* Save position after header and context */
1712 packet_index
.data_offset
= pos
->offset
;
1714 /* add index to packet array */
1715 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1717 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1721 /* Retry with larger mapping */
1723 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1725 * Reached EOF, but still expecting header/context data.
1727 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1730 /* Double the mapping len, and retry */
1731 tmp_map_len
= packet_map_len
<< 1;
1732 if (tmp_map_len
>> 1 != packet_map_len
) {
1734 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1737 packet_map_len
= tmp_map_len
;
1742 int create_stream_packet_index(struct ctf_trace
*td
,
1743 struct ctf_file_stream
*file_stream
)
1745 struct ctf_stream_pos
*pos
;
1746 struct stat filestats
;
1749 pos
= &file_stream
->pos
;
1751 ret
= fstat(pos
->fd
, &filestats
);
1755 /* Deal with empty files */
1756 if (!filestats
.st_size
) {
1757 if (file_stream
->parent
.trace_packet_header
1758 || file_stream
->parent
.stream_packet_context
) {
1760 * We expect a trace packet header and/or stream packet
1761 * context. Since a trace needs to have at least one
1762 * packet, empty files are therefore not accepted.
1764 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1768 * Without trace packet header nor stream packet
1769 * context, a one-packet trace can indeed be empty. This
1770 * is only valid if there is only one stream class: 0.
1772 ret
= stream_assign_class(td
, file_stream
, 0);
1779 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1780 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1789 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1793 if (td
->packet_header_decl
) {
1794 struct bt_definition
*definition
=
1795 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1796 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1801 stream
->trace_packet_header
=
1802 container_of(definition
, struct definition_struct
, p
);
1803 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
1809 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
1814 int import_stream_packet_index(struct ctf_trace
*td
,
1815 struct ctf_file_stream
*file_stream
)
1817 struct ctf_stream_pos
*pos
;
1818 struct ctf_packet_index
*ctf_index
= NULL
;
1819 struct ctf_packet_index_file_hdr index_hdr
;
1820 struct packet_index index
;
1822 int first_packet
= 1;
1825 pos
= &file_stream
->pos
;
1827 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
1829 perror("read index file header");
1833 /* Check the index header */
1834 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
1835 fprintf(stderr
, "[error] wrong index magic\n");
1839 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
1840 fprintf(stderr
, "[error] Incompatible index file %" PRIu64
1841 ".%" PRIu64
", supported %d.%d\n",
1842 be64toh(index_hdr
.index_major
),
1843 be64toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
1848 if (index_hdr
.packet_index_len
== 0) {
1849 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
1854 * Allocate the index length found in header, not internal
1857 ctf_index
= g_malloc0(index_hdr
.packet_index_len
);
1858 while (fread(ctf_index
, index_hdr
.packet_index_len
, 1,
1859 pos
->index_fp
) == 1) {
1861 struct ctf_stream_declaration
*stream
= NULL
;
1863 memset(&index
, 0, sizeof(index
));
1864 index
.offset
= be64toh(ctf_index
->offset
);
1865 index
.packet_size
= be64toh(ctf_index
->packet_size
);
1866 index
.content_size
= be64toh(ctf_index
->content_size
);
1867 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
1868 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
1869 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
1870 index
.events_discarded_len
= 64;
1871 index
.data_offset
= -1;
1872 stream_id
= be64toh(ctf_index
->stream_id
);
1874 if (!first_packet
) {
1875 /* add index to packet array */
1876 g_array_append_val(file_stream
->pos
.packet_index
, index
);
1880 file_stream
->parent
.stream_id
= stream_id
;
1881 if (stream_id
< td
->streams
->len
) {
1882 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1885 fprintf(stderr
, "[error] Stream %" PRIu64
1886 " is not declared in metadata.\n",
1891 file_stream
->parent
.stream_class
= stream
;
1892 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1896 /* add index to packet array */
1897 g_array_append_val(file_stream
->pos
.packet_index
, index
);
1900 /* Index containing only the header. */
1901 if (!file_stream
->parent
.stream_class
) {
1914 * Note: many file streams can inherit from the same stream class
1915 * description (metadata).
1918 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
1919 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
1922 int ret
, fd
, closeret
;
1923 struct ctf_file_stream
*file_stream
;
1924 struct stat statbuf
;
1927 fd
= openat(td
->dirfd
, path
, flags
);
1929 perror("File stream openat()");
1934 /* Don't try to mmap subdirectories. Skip them, return success. */
1935 ret
= fstat(fd
, &statbuf
);
1937 perror("File stream fstat()");
1940 if (S_ISDIR(statbuf
.st_mode
)) {
1941 if (strncmp(path
, "index", 5) != 0) {
1942 fprintf(stderr
, "[warning] Skipping directory '%s' "
1943 "found in trace\n", path
);
1949 file_stream
= g_new0(struct ctf_file_stream
, 1);
1950 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1951 file_stream
->pos
.fd
= -1;
1952 file_stream
->pos
.index_fp
= NULL
;
1954 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
1955 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
1958 file_stream
->pos
.packet_seek
= packet_seek
;
1960 fprintf(stderr
, "[error] packet_seek function undefined.\n");
1965 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
1968 ret
= create_trace_definitions(td
, &file_stream
->parent
);
1972 * For now, only a single clock per trace is supported.
1974 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
1977 * Allocate the index name for this stream and try to open it.
1979 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
1981 fprintf(stderr
, "[error] Cannot allocate index filename\n");
1984 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
1987 if (faccessat(td
->dirfd
, index_name
, O_RDONLY
, flags
) < 0) {
1988 ret
= create_stream_packet_index(td
, file_stream
);
1990 fprintf(stderr
, "[error] Stream index creation error.\n");
1994 ret
= openat(td
->dirfd
, index_name
, flags
);
1996 perror("Index file openat()");
2000 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2001 if (!file_stream
->pos
.index_fp
) {
2002 perror("fdopen() error");
2005 ret
= import_stream_packet_index(td
, file_stream
);
2010 ret
= fclose(file_stream
->pos
.index_fp
);
2012 perror("close index");
2018 /* Add stream file to stream class */
2019 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2020 &file_stream
->parent
);
2024 if (file_stream
->pos
.index_fp
) {
2025 ret
= fclose(file_stream
->pos
.index_fp
);
2027 perror("close index");
2030 if (file_stream
->parent
.trace_packet_header
)
2031 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2035 closeret
= ctf_fini_pos(&file_stream
->pos
);
2037 fprintf(stderr
, "Error on ctf_fini_pos\n");
2039 g_free(file_stream
);
2042 closeret
= close(fd
);
2044 perror("Error on fd close");
2051 int ctf_open_trace_read(struct ctf_trace
*td
,
2052 const char *path
, int flags
,
2053 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2054 int whence
), FILE *metadata_fp
)
2056 struct ctf_scanner
*scanner
;
2058 struct dirent
*dirent
;
2059 struct dirent
*diriter
;
2065 /* Open trace directory */
2066 td
->dir
= opendir(path
);
2068 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2073 td
->dirfd
= open(path
, 0);
2074 if (td
->dirfd
< 0) {
2075 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2076 perror("Trace directory open");
2080 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2081 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2084 * Keep the metadata file separate.
2085 * Keep scanner object local to the open. We don't support
2086 * incremental metadata append for on-disk traces.
2088 scanner
= ctf_scanner_alloc();
2090 fprintf(stderr
, "[error] Error allocating scanner\n");
2092 goto error_metadata
;
2094 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2095 ctf_scanner_free(scanner
);
2097 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2098 goto error_metadata
;
2102 * Open each stream: for each file, try to open, check magic
2103 * number, and get the stream ID to add to the right location in
2107 dirent_len
= offsetof(struct dirent
, d_name
) +
2108 fpathconf(td
->dirfd
, _PC_NAME_MAX
) + 1;
2110 dirent
= malloc(dirent_len
);
2113 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2115 fprintf(stderr
, "[error] Readdir error.\n");
2120 /* Ignore hidden files, ., .. and metadata. */
2121 if (!strncmp(diriter
->d_name
, ".", 1)
2122 || !strcmp(diriter
->d_name
, "..")
2123 || !strcmp(diriter
->d_name
, "metadata"))
2126 /* Ignore index files : *.idx */
2127 ext
= strrchr(diriter
->d_name
, '.');
2128 if (ext
&& (!strcmp(ext
, ".idx"))) {
2132 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2133 flags
, packet_seek
);
2135 fprintf(stderr
, "[error] Open file stream error.\n");
2146 closeret
= close(td
->dirfd
);
2148 perror("Error on fd close");
2151 closeret
= closedir(td
->dir
);
2153 perror("Error on closedir");
2160 * ctf_open_trace: Open a CTF trace and index it.
2161 * Note that the user must seek the trace after the open (using the iterator)
2162 * since the index creation read it entirely.
2165 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2166 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2167 int whence
), FILE *metadata_fp
)
2169 struct ctf_trace
*td
;
2173 * If packet_seek is NULL, we provide our default version.
2176 packet_seek
= ctf_packet_seek
;
2178 td
= g_new0(struct ctf_trace
, 1);
2180 switch (flags
& O_ACCMODE
) {
2183 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2186 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2191 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2194 fprintf(stderr
, "[error] Incorrect open flags.\n");
2205 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2206 struct bt_mmap_stream
*mmap_info
)
2208 pos
->mmap_offset
= 0;
2209 pos
->packet_size
= 0;
2210 pos
->content_size
= 0;
2211 pos
->content_size_loc
= NULL
;
2212 pos
->fd
= mmap_info
->fd
;
2213 pos
->base_mma
= NULL
;
2217 pos
->prot
= PROT_READ
;
2218 pos
->flags
= MAP_PRIVATE
;
2219 pos
->parent
.rw_table
= read_dispatch_table
;
2220 pos
->parent
.event_cb
= ctf_read_event
;
2221 pos
->priv
= mmap_info
->priv
;
2222 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2223 sizeof(struct packet_index
));
2227 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2228 struct ctf_file_stream
*file_stream
)
2230 struct ctf_stream_declaration
*stream
;
2231 uint64_t stream_id
= 0;
2234 file_stream
->parent
.stream_id
= stream_id
;
2235 if (stream_id
>= td
->streams
->len
) {
2236 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2237 "in metadata.\n", stream_id
);
2241 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2243 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2244 "in metadata.\n", stream_id
);
2248 file_stream
->parent
.stream_class
= stream
;
2249 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2255 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2256 struct bt_mmap_stream
*mmap_info
,
2257 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2261 struct ctf_file_stream
*file_stream
;
2263 file_stream
= g_new0(struct ctf_file_stream
, 1);
2264 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2265 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2267 file_stream
->pos
.packet_seek
= packet_seek
;
2269 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2274 ret
= prepare_mmap_stream_definition(td
, file_stream
);
2279 * For now, only a single clock per trace is supported.
2281 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2283 /* Add stream file to stream class */
2284 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2285 &file_stream
->parent
);
2289 if (file_stream
->parent
.trace_packet_header
)
2290 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2292 g_free(file_stream
);
2297 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2298 struct bt_mmap_stream_list
*mmap_list
,
2299 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2304 struct bt_mmap_stream
*mmap_info
;
2306 td
->scanner
= ctf_scanner_alloc();
2308 fprintf(stderr
, "[error] Error allocating scanner\n");
2312 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2318 * for each stream, try to open, check magic number, and get the
2319 * stream ID to add to the right location in the stream array.
2321 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2322 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2324 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2331 ctf_scanner_free(td
->scanner
);
2336 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2337 struct bt_mmap_stream_list
*mmap_list
,
2338 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2342 struct ctf_trace
*td
;
2346 fprintf(stderr
, "[error] No metadata file pointer associated, "
2347 "required for mmap parsing\n");
2351 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2354 td
= g_new0(struct ctf_trace
, 1);
2356 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2368 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2371 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2377 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2380 /* for each stream_class */
2381 for (i
= 0; i
< td
->streams
->len
; i
++) {
2382 struct ctf_stream_declaration
*stream_class
;
2384 stream_class
= g_ptr_array_index(td
->streams
, i
);
2387 /* for each stream */
2388 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2389 struct ctf_stream_definition
*stream
;
2391 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2394 ret
= copy_event_declarations_stream_class_to_stream(td
,
2395 stream_class
, stream
);
2404 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2407 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2409 /* for each stream_class */
2410 for (i
= 0; i
< td
->streams
->len
; i
++) {
2411 struct ctf_stream_declaration
*stream_class
;
2413 stream_class
= g_ptr_array_index(td
->streams
, i
);
2416 /* for each file_stream */
2417 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2418 struct ctf_stream_definition
*stream
;
2419 struct ctf_stream_pos
*stream_pos
;
2420 struct ctf_file_stream
*cfs
;
2422 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2425 cfs
= container_of(stream
, struct ctf_file_stream
,
2427 stream_pos
= &cfs
->pos
;
2428 if (!stream_pos
->packet_index
)
2431 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2432 struct packet_index
*index
;
2434 index
= &g_array_index(stream_pos
->packet_index
,
2435 struct packet_index
, k
);
2436 index
->ts_real
.timestamp_begin
=
2437 ctf_get_real_timestamp(stream
,
2438 index
->ts_cycles
.timestamp_begin
);
2439 index
->ts_real
.timestamp_end
=
2440 ctf_get_real_timestamp(stream
,
2441 index
->ts_cycles
.timestamp_end
);
2449 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2453 ret
= ctf_fini_pos(&file_stream
->pos
);
2455 fprintf(stderr
, "Error on ctf_fini_pos\n");
2458 if (file_stream
->pos
.fd
>= 0) {
2459 ret
= close(file_stream
->pos
.fd
);
2461 perror("Error closing file fd");
2469 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2471 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2477 for (i
= 0; i
< td
->streams
->len
; i
++) {
2478 struct ctf_stream_declaration
*stream
;
2481 stream
= g_ptr_array_index(td
->streams
, i
);
2484 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2485 struct ctf_file_stream
*file_stream
;
2486 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2487 struct ctf_file_stream
, parent
);
2488 ret
= ctf_close_file_stream(file_stream
);
2494 ctf_destroy_metadata(td
);
2495 ctf_scanner_free(td
->scanner
);
2496 if (td
->dirfd
>= 0) {
2497 ret
= close(td
->dirfd
);
2499 perror("Error closing dirfd");
2504 ret
= closedir(td
->dir
);
2506 perror("Error closedir");
2510 free(td
->metadata_string
);
2516 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2517 struct bt_context
*ctx
)
2519 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2522 td
->parent
.ctx
= ctx
;
2526 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2527 struct bt_trace_handle
*handle
)
2529 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2532 td
->parent
.handle
= handle
;
2536 void __attribute__((constructor
)) ctf_init(void)
2540 ctf_format
.name
= g_quark_from_static_string("ctf");
2541 ret
= bt_register_format(&ctf_format
);
2546 void __attribute__((destructor
)) ctf_exit(void)
2548 bt_unregister_format(&ctf_format
);