2 * BabelTrace - Common Trace Format (CTF)
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <babeltrace/trace-handle-internal.h>
35 #include <babeltrace/context-internal.h>
36 #include <babeltrace/compat/uuid.h>
37 #include <babeltrace/endian.h>
38 #include <babeltrace/trace-debuginfo.h>
39 #include <babeltrace/ctf/ctf-index.h>
44 #include <sys/types.h>
52 #include "metadata/ctf-scanner.h"
53 #include "metadata/ctf-parser.h"
54 #include "metadata/ctf-ast.h"
55 #include "events-private.h"
56 #include <babeltrace/compat/memstream.h>
57 #include <babeltrace/compat/fcntl.h>
59 #define LOG2_CHAR_BIT 3
62 * Length of first attempt at mapping a packet header, in bits.
64 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
67 * Lenght of packet to write, in bits.
69 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
72 #define min(a, b) (((a) < (b)) ? (a) : (b))
75 #define NSEC_PER_SEC 1000000000LL
77 #define INDEX_PATH "./index/%s.idx"
84 int64_t opt_clock_offset
;
85 int64_t opt_clock_offset_ns
;
88 char *opt_debug_info_dir
;
89 char *opt_debug_info_target_prefix
;
92 * TODO: babeltrace_ctf_console_output ensures that we only print
93 * discarded events when ctf-text plugin is used. Should be cleaned up
94 * with the plugin system redesign.
96 int babeltrace_ctf_console_output
;
99 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
100 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
104 struct bt_trace_descriptor
*ctf_open_mmap_trace(
105 struct bt_mmap_stream_list
*mmap_list
,
106 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
110 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
111 struct bt_context
*ctx
);
113 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
114 struct bt_trace_handle
*handle
);
117 int ctf_close_trace(struct bt_trace_descriptor
*descriptor
);
119 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
120 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
123 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
124 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
127 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
);
130 rw_dispatch read_dispatch_table
[] = {
131 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_read
,
132 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_read
,
133 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_read
,
134 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_read
,
135 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
136 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
137 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_read
,
138 [ BT_CTF_TYPE_ID_SEQUENCE
] = ctf_sequence_read
,
142 rw_dispatch write_dispatch_table
[] = {
143 [ BT_CTF_TYPE_ID_INTEGER
] = ctf_integer_write
,
144 [ BT_CTF_TYPE_ID_FLOAT
] = ctf_float_write
,
145 [ BT_CTF_TYPE_ID_ENUM
] = ctf_enum_write
,
146 [ BT_CTF_TYPE_ID_STRING
] = ctf_string_write
,
147 [ BT_CTF_TYPE_ID_STRUCT
] = ctf_struct_rw
,
148 [ BT_CTF_TYPE_ID_VARIANT
] = ctf_variant_rw
,
149 [ BT_CTF_TYPE_ID_ARRAY
] = ctf_array_write
,
150 [ BT_CTF_TYPE_ID_SEQUENCE
] = ctf_sequence_write
,
154 struct bt_format ctf_format
= {
155 .open_trace
= ctf_open_trace
,
156 .open_mmap_trace
= ctf_open_mmap_trace
,
157 .close_trace
= ctf_close_trace
,
158 .set_context
= ctf_set_context
,
159 .set_handle
= ctf_set_handle
,
160 .timestamp_begin
= ctf_timestamp_begin
,
161 .timestamp_end
= ctf_timestamp_end
,
162 .convert_index_timestamp
= ctf_convert_index_timestamp
,
166 int ctf_timestamp_begin(struct bt_trace_descriptor
*descriptor
,
167 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
170 struct ctf_trace
*tin
;
171 int64_t begin
= LLONG_MAX
;
174 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
176 if (!tin
|| !timestamp
) {
181 /* for each stream_class */
182 for (i
= 0; i
< tin
->streams
->len
; i
++) {
183 struct ctf_stream_declaration
*stream_class
;
185 stream_class
= g_ptr_array_index(tin
->streams
, i
);
188 /* for each file_stream */
189 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
190 struct ctf_stream_definition
*stream
;
191 struct ctf_file_stream
*cfs
;
192 struct ctf_stream_pos
*stream_pos
;
193 struct packet_index
*index
;
195 stream
= g_ptr_array_index(stream_class
->streams
, j
);
196 cfs
= container_of(stream
, struct ctf_file_stream
,
198 stream_pos
= &cfs
->pos
;
200 if (!stream_pos
->packet_index
) {
205 if (stream_pos
->packet_index
->len
<= 0)
208 index
= &g_array_index(stream_pos
->packet_index
,
210 stream_pos
->packet_index
->len
- 1);
211 if (type
== BT_CLOCK_REAL
) {
212 if (index
->ts_real
.timestamp_begin
< begin
)
213 begin
= index
->ts_real
.timestamp_begin
;
214 } else if (type
== BT_CLOCK_CYCLES
) {
215 if (index
->ts_cycles
.timestamp_begin
< begin
)
216 begin
= index
->ts_cycles
.timestamp_begin
;
223 if (begin
== LLONG_MAX
) {
235 int ctf_timestamp_end(struct bt_trace_descriptor
*descriptor
,
236 struct bt_trace_handle
*handle
, enum bt_clock_type type
,
239 struct ctf_trace
*tin
;
240 int64_t end
= LLONG_MIN
;
243 tin
= container_of(descriptor
, struct ctf_trace
, parent
);
245 if (!tin
|| !timestamp
) {
250 /* for each stream_class */
251 for (i
= 0; i
< tin
->streams
->len
; i
++) {
252 struct ctf_stream_declaration
*stream_class
;
254 stream_class
= g_ptr_array_index(tin
->streams
, i
);
257 /* for each file_stream */
258 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
259 struct ctf_stream_definition
*stream
;
260 struct ctf_file_stream
*cfs
;
261 struct ctf_stream_pos
*stream_pos
;
262 struct packet_index
*index
;
264 stream
= g_ptr_array_index(stream_class
->streams
, j
);
265 cfs
= container_of(stream
, struct ctf_file_stream
,
267 stream_pos
= &cfs
->pos
;
269 if (!stream_pos
->packet_index
) {
274 if (stream_pos
->packet_index
->len
<= 0)
277 index
= &g_array_index(stream_pos
->packet_index
,
279 stream_pos
->packet_index
->len
- 1);
280 if (type
== BT_CLOCK_REAL
) {
281 if (index
->ts_real
.timestamp_end
> end
)
282 end
= index
->ts_real
.timestamp_end
;
283 } else if (type
== BT_CLOCK_CYCLES
) {
284 if (index
->ts_cycles
.timestamp_end
> end
)
285 end
= index
->ts_cycles
.timestamp_end
;
292 if (end
== LLONG_MIN
) {
304 * Update stream current timestamp
307 void ctf_update_timestamp(struct ctf_stream_definition
*stream
,
308 struct definition_integer
*integer_definition
)
310 struct declaration_integer
*integer_declaration
=
311 integer_definition
->declaration
;
312 uint64_t oldval
, newval
, updateval
;
314 if (unlikely(integer_declaration
->len
== 64)) {
315 stream
->cycles_timestamp
= integer_definition
->value
._unsigned
;
316 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
317 stream
->cycles_timestamp
);
321 oldval
= stream
->cycles_timestamp
;
322 oldval
&= (1ULL << integer_declaration
->len
) - 1;
323 newval
= integer_definition
->value
._unsigned
;
324 /* Test for overflow by comparing low bits */
326 newval
+= 1ULL << integer_declaration
->len
;
327 /* updateval contains old high bits, and new low bits (sum) */
328 updateval
= stream
->cycles_timestamp
;
329 updateval
&= ~((1ULL << integer_declaration
->len
) - 1);
331 stream
->cycles_timestamp
= updateval
;
333 /* convert to real timestamp */
334 stream
->real_timestamp
= ctf_get_real_timestamp(stream
,
335 stream
->cycles_timestamp
);
339 * Print timestamp, rescaling clock frequency to nanoseconds and
340 * applying offsets as needed (unix time).
343 void ctf_print_timestamp_real(FILE *fp
,
344 struct ctf_stream_definition
*stream
,
347 int64_t ts_sec
= 0, ts_nsec
;
348 uint64_t ts_sec_abs
, ts_nsec_abs
;
353 /* Add command-line offset in ns */
354 ts_nsec
+= opt_clock_offset_ns
;
356 /* Add command-line offset */
357 ts_sec
+= opt_clock_offset
;
359 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
360 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
361 if (ts_sec
>= 0 && ts_nsec
>= 0) {
364 ts_nsec_abs
= ts_nsec
;
365 } else if (ts_sec
> 0 && ts_nsec
< 0) {
367 ts_sec_abs
= ts_sec
- 1;
368 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
369 } else if (ts_sec
== 0 && ts_nsec
< 0) {
372 ts_nsec_abs
= -ts_nsec
;
373 } else if (ts_sec
< 0 && ts_nsec
> 0) {
375 ts_sec_abs
= -(ts_sec
+ 1);
376 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
377 } else if (ts_sec
< 0 && ts_nsec
== 0) {
379 ts_sec_abs
= -ts_sec
;
380 ts_nsec_abs
= ts_nsec
;
381 } else { /* (ts_sec < 0 && ts_nsec < 0) */
383 ts_sec_abs
= -ts_sec
;
384 ts_nsec_abs
= -ts_nsec
;
387 if (!opt_clock_seconds
) {
389 time_t time_s
= (time_t) ts_sec_abs
;
392 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
396 if (!opt_clock_gmt
) {
399 res
= localtime_r(&time_s
, &tm
);
401 fprintf(stderr
, "[warning] Unable to get localtime.\n");
407 res
= gmtime_r(&time_s
, &tm
);
409 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
413 if (opt_clock_date
) {
417 /* Print date and time */
418 res
= strftime(timestr
, sizeof(timestr
),
421 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
424 fprintf(fp
, "%s", timestr
);
426 /* Print time in HH:MM:SS.ns */
427 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
428 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec_abs
);
432 fprintf(fp
, "%s%" PRId64
".%09" PRIu64
,
433 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
440 * Print timestamp, in cycles
443 void ctf_print_timestamp_cycles(FILE *fp
,
444 struct ctf_stream_definition
*stream
,
447 fprintf(fp
, "%020" PRIu64
, timestamp
);
450 void ctf_print_timestamp(FILE *fp
,
451 struct ctf_stream_definition
*stream
,
454 if (opt_clock_cycles
) {
455 ctf_print_timestamp_cycles(fp
, stream
, timestamp
);
457 ctf_print_timestamp_real(fp
, stream
, timestamp
);
462 void print_uuid(FILE *fp
, unsigned char *uuid
)
466 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++)
467 fprintf(fp
, "%x", (unsigned int) uuid
[i
]);
471 * Discarded events can be either:
472 * - discarded after end of previous buffer due to buffer full:
473 * happened within range: [ prev_timestamp_end, timestamp_begin ]
474 * - discarded within current buffer due to either event too large or
475 * nested wrap-around:
476 * happened within range: [ timestamp_begin, timestamp_end ]
478 * Given we have discarded counters of those two types merged into the
479 * events_discarded counter, we need to use the union of those ranges:
480 * [ prev_timestamp_end, timestamp_end ]
482 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
483 * consumer had time to extract them. We keep track of those gaps with the
484 * packet sequence number in each packet.
487 void ctf_print_discarded_lost(FILE *fp
, struct ctf_stream_definition
*stream
)
489 if ((!stream
->events_discarded
&& !stream
->packets_lost
) ||
490 !babeltrace_ctf_console_output
) {
494 if (stream
->events_discarded
) {
495 fprintf(fp
, "[warning] Tracer discarded %" PRIu64
" events between [",
496 stream
->events_discarded
);
497 } else if (stream
->packets_lost
) {
498 fprintf(fp
, "[warning] Tracer lost %" PRIu64
" trace packets between [",
499 stream
->packets_lost
);
501 if (opt_clock_cycles
) {
502 ctf_print_timestamp(fp
, stream
,
503 stream
->prev
.cycles
.end
);
504 fprintf(fp
, "] and [");
505 ctf_print_timestamp(fp
, stream
,
506 stream
->current
.cycles
.end
);
508 ctf_print_timestamp(fp
, stream
,
509 stream
->prev
.real
.end
);
510 fprintf(fp
, "] and [");
511 ctf_print_timestamp(fp
, stream
,
512 stream
->current
.real
.end
);
514 fprintf(fp
, "] in trace UUID ");
515 print_uuid(fp
, stream
->stream_class
->trace
->uuid
);
516 if (stream
->stream_class
->trace
->parent
.path
[0])
517 fprintf(fp
, ", at path: \"%s\"",
518 stream
->stream_class
->trace
->parent
.path
);
520 fprintf(fp
, ", within stream id %" PRIu64
, stream
->stream_id
);
522 fprintf(fp
, ", at relative path: \"%s\"", stream
->path
);
524 fprintf(fp
, "You should consider recording a new trace with larger "
525 "buffers or with fewer events enabled.\n");
530 int ctf_read_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
532 struct ctf_stream_pos
*pos
=
533 container_of(ppos
, struct ctf_stream_pos
, parent
);
534 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
535 struct ctf_event_definition
*event
;
539 /* We need to check for EOF here for empty files. */
540 if (unlikely(pos
->offset
== EOF
))
543 ctf_pos_get_event(pos
);
545 /* save the current position as a restore point */
546 pos
->last_offset
= pos
->offset
;
549 * This is the EOF check after we've advanced the position in
552 if (unlikely(pos
->offset
== EOF
))
555 /* Stream is inactive for now (live reading). */
556 if (unlikely(pos
->content_size
== 0))
560 * Packet seeked to by ctf_pos_get_event() only contains
561 * headers, no event. Consider stream as inactive (live
564 if (unlikely(pos
->data_offset
== pos
->content_size
))
567 assert(pos
->offset
< pos
->content_size
);
569 /* Read event header */
570 if (likely(stream
->stream_event_header
)) {
571 struct definition_integer
*integer_definition
;
572 struct bt_definition
*variant
;
574 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
577 /* lookup event id */
578 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
579 if (integer_definition
) {
580 id
= integer_definition
->value
._unsigned
;
582 struct definition_enum
*enum_definition
;
584 enum_definition
= bt_lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
585 if (enum_definition
) {
586 id
= enum_definition
->integer
->value
._unsigned
;
590 variant
= bt_lookup_variant(&stream
->stream_event_header
->p
, "v");
592 integer_definition
= bt_lookup_integer(variant
, "id", FALSE
);
593 if (integer_definition
) {
594 id
= integer_definition
->value
._unsigned
;
597 stream
->event_id
= id
;
599 /* lookup timestamp */
600 stream
->has_timestamp
= 0;
601 integer_definition
= bt_lookup_integer(&stream
->stream_event_header
->p
, "timestamp", FALSE
);
602 if (integer_definition
) {
603 ctf_update_timestamp(stream
, integer_definition
);
604 stream
->has_timestamp
= 1;
607 integer_definition
= bt_lookup_integer(variant
, "timestamp", FALSE
);
608 if (integer_definition
) {
609 ctf_update_timestamp(stream
, integer_definition
);
610 stream
->has_timestamp
= 1;
616 /* Read stream-declared event context */
617 if (stream
->stream_event_context
) {
618 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
623 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
624 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
627 event
= g_ptr_array_index(stream
->events_by_id
, id
);
628 if (unlikely(!event
)) {
629 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
633 /* Read event-declared event context */
634 if (event
->event_context
) {
635 ret
= generic_rw(ppos
, &event
->event_context
->p
);
640 /* Read event payload */
641 if (likely(event
->event_fields
)) {
642 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
647 if (pos
->last_offset
== pos
->offset
) {
648 fprintf(stderr
, "[error] Invalid 0 byte event encountered.\n");
655 fprintf(stderr
, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
660 int ctf_write_event(struct bt_stream_pos
*pos
, struct ctf_stream_definition
*stream
)
662 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
663 struct ctf_event_definition
*event
;
667 id
= stream
->event_id
;
669 /* print event header */
670 if (likely(stream
->stream_event_header
)) {
671 ret
= generic_rw(pos
, &stream
->stream_event_header
->p
);
676 /* print stream-declared event context */
677 if (stream
->stream_event_context
) {
678 ret
= generic_rw(pos
, &stream
->stream_event_context
->p
);
683 if (unlikely(id
>= stream_class
->events_by_id
->len
)) {
684 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
687 event
= g_ptr_array_index(stream
->events_by_id
, id
);
688 if (unlikely(!event
)) {
689 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
693 /* print event-declared event context */
694 if (event
->event_context
) {
695 ret
= generic_rw(pos
, &event
->event_context
->p
);
700 /* Read and print event payload */
701 if (likely(event
->event_fields
)) {
702 ret
= generic_rw(pos
, &event
->event_fields
->p
);
710 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
715 * One side-effect of this function is to unmap pos mmap base if one is
719 int find_data_offset(struct ctf_stream_pos
*pos
,
720 struct ctf_file_stream
*file_stream
,
721 struct packet_index
*packet_index
)
723 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
724 struct stat filestats
;
728 pos
= &file_stream
->pos
;
730 ret
= fstat(pos
->fd
, &filestats
);
733 filesize
= filestats
.st_size
;
735 /* Deal with empty files */
741 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
742 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
747 ret
= munmap_align(pos
->base_mma
);
749 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
753 pos
->base_mma
= NULL
;
755 /* map new base. Need mapping length from header. */
756 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
757 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
758 assert(pos
->base_mma
!= MAP_FAILED
);
760 pos
->content_size
= packet_map_len
;
761 pos
->packet_size
= packet_map_len
;
762 pos
->offset
= 0; /* Position of the packet header */
764 /* update trace_packet_header and stream_packet_context */
765 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.trace_packet_header
) {
766 /* Read packet header */
767 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
773 if (pos
->prot
== PROT_READ
&& file_stream
->parent
.stream_packet_context
) {
774 /* Read packet context */
775 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
781 packet_index
->data_offset
= pos
->offset
;
784 ret
= munmap_align(pos
->base_mma
);
786 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
790 pos
->base_mma
= NULL
;
794 /* Retry with larger mapping */
796 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
798 * Reached EOF, but still expecting header/context data.
800 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
803 /* Double the mapping len, and retry */
804 tmp_map_len
= packet_map_len
<< 1;
805 if (tmp_map_len
>> 1 != packet_map_len
) {
807 fprintf(stderr
, "[error] Packet mapping length overflow\n");
810 packet_map_len
= tmp_map_len
;
815 int ctf_init_pos(struct ctf_stream_pos
*pos
, struct bt_trace_descriptor
*trace
,
816 int fd
, int open_flags
)
820 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
821 sizeof(struct packet_index
));
823 pos
->packet_index
= NULL
;
825 switch (open_flags
& O_ACCMODE
) {
827 pos
->prot
= PROT_READ
;
828 pos
->flags
= MAP_PRIVATE
;
829 pos
->parent
.rw_table
= read_dispatch_table
;
830 pos
->parent
.event_cb
= ctf_read_event
;
831 pos
->parent
.trace
= trace
;
834 pos
->prot
= PROT_READ
| PROT_WRITE
;
835 pos
->flags
= MAP_SHARED
;
836 pos
->parent
.rw_table
= write_dispatch_table
;
837 pos
->parent
.event_cb
= ctf_write_event
;
838 pos
->parent
.trace
= trace
;
846 int ctf_fini_pos(struct ctf_stream_pos
*pos
)
848 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
849 *pos
->content_size_loc
= pos
->offset
;
854 ret
= munmap_align(pos
->base_mma
);
856 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
861 if (pos
->packet_index
)
862 (void) g_array_free(pos
->packet_index
, TRUE
);
866 void ctf_update_current_packet_index(struct ctf_stream_definition
*stream
,
867 struct packet_index
*prev_index
,
868 struct packet_index
*cur_index
)
870 uint64_t events_discarded_diff
;
871 uint64_t packets_lost_diff
= 0;
873 /* Update packet index time information */
875 /* Current packet begin/end */
876 stream
->current
.real
.begin
=
877 cur_index
->ts_real
.timestamp_begin
;
878 stream
->current
.cycles
.begin
=
879 cur_index
->ts_cycles
.timestamp_begin
;
880 stream
->current
.real
.end
=
881 cur_index
->ts_real
.timestamp_end
;
882 stream
->current
.cycles
.end
=
883 cur_index
->ts_cycles
.timestamp_end
;
885 /* Update packet index discarded event information */
886 events_discarded_diff
= cur_index
->events_discarded
;
888 /* Previous packet begin/end */
889 stream
->prev
.cycles
.begin
=
890 prev_index
->ts_cycles
.timestamp_begin
;
891 stream
->prev
.real
.begin
=
892 prev_index
->ts_real
.timestamp_begin
;
893 stream
->prev
.cycles
.end
=
894 prev_index
->ts_cycles
.timestamp_end
;
895 stream
->prev
.real
.end
=
896 prev_index
->ts_real
.timestamp_end
;
898 events_discarded_diff
-= prev_index
->events_discarded
;
899 /* packet_seq_num stays at 0 if not produced by the tracer */
900 if (cur_index
->packet_seq_num
) {
901 packets_lost_diff
= cur_index
->packet_seq_num
-
902 prev_index
->packet_seq_num
- 1;
905 * Deal with 32-bit wrap-around if the tracer provided a
908 if (prev_index
->events_discarded_len
== 32) {
909 events_discarded_diff
= (uint32_t) events_discarded_diff
;
913 * First packet: use current packet info as limits for
916 stream
->prev
.cycles
.begin
=
917 stream
->prev
.cycles
.end
=
918 stream
->current
.cycles
.begin
;
919 stream
->prev
.real
.begin
=
920 stream
->prev
.real
.end
=
921 stream
->current
.real
.begin
;
923 stream
->events_discarded
= events_discarded_diff
;
924 stream
->packets_lost
= packets_lost_diff
;
928 * Find the timerange where all the streams in the trace are active
931 * Return 0 and update begin/end if necessary on success, return 1 for
932 * empty streams and return a negative value on error.
935 int ctf_intersect_trace(struct bt_trace_descriptor
*td_read
,
936 uint64_t *begin
, uint64_t *end
)
938 struct ctf_trace
*tin
;
939 int stream_id
, ret
= 0;
941 tin
= container_of(td_read
, struct ctf_trace
, parent
);
943 for (stream_id
= 0; stream_id
< tin
->streams
->len
;
946 struct ctf_stream_declaration
*stream_class
;
948 stream_class
= g_ptr_array_index(tin
->streams
, stream_id
);
952 for (filenr
= 0; filenr
< stream_class
->streams
->len
; filenr
++) {
953 struct ctf_file_stream
*file_stream
;
954 struct ctf_stream_pos
*stream_pos
;
955 struct packet_index
*index
;
957 file_stream
= g_ptr_array_index(stream_class
->streams
,
962 stream_pos
= &file_stream
->pos
;
963 if (!stream_pos
->packet_index
||
964 stream_pos
->packet_index
->len
<= 0) {
968 index
= &g_array_index(stream_pos
->packet_index
,
969 struct packet_index
, 0);
970 if (index
->ts_real
.timestamp_begin
> *begin
) {
971 *begin
= index
->ts_real
.timestamp_begin
;
973 index
= &g_array_index(stream_pos
->packet_index
,
975 stream_pos
->packet_index
->len
- 1);
976 if (index
->ts_real
.timestamp_end
< *end
) {
977 *end
= index
->ts_real
.timestamp_end
;
987 * Find the timerange where all streams in the trace collection are active
990 * Return 0 on success.
991 * Return 1 if no intersections are found.
992 * Return a negative value on error.
994 int ctf_find_packets_intersection(struct bt_context
*ctx
,
995 uint64_t *ts_begin
, uint64_t *ts_end
)
999 if (!ctx
|| !ctx
->tc
|| !ctx
->tc
->array
) {
1004 for (i
= 0; i
< ctx
->tc
->array
->len
; i
++) {
1005 struct bt_trace_descriptor
*td_read
;
1007 td_read
= g_ptr_array_index(ctx
->tc
->array
, i
);
1011 ret
= ctf_intersect_trace(td_read
, ts_begin
, ts_end
);
1016 if (*ts_end
< *ts_begin
) {
1026 * for SEEK_CUR: go to next packet.
1027 * for SEEK_SET: go to packet numer (index).
1029 void ctf_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
, int whence
)
1031 struct ctf_stream_pos
*pos
=
1032 container_of(stream_pos
, struct ctf_stream_pos
, parent
);
1033 struct ctf_file_stream
*file_stream
=
1034 container_of(pos
, struct ctf_file_stream
, pos
);
1036 struct packet_index
*packet_index
, *prev_index
;
1040 case SEEK_SET
: /* Fall-through */
1046 if ((pos
->prot
& PROT_WRITE
) && pos
->content_size_loc
)
1047 *pos
->content_size_loc
= pos
->offset
;
1049 if (pos
->base_mma
) {
1050 /* unmap old base */
1051 ret
= munmap_align(pos
->base_mma
);
1053 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1057 pos
->base_mma
= NULL
;
1061 * The caller should never ask for ctf_move_pos across packets,
1062 * except to get exactly at the beginning of the next packet.
1064 if (pos
->prot
& PROT_WRITE
) {
1067 /* The writer will add padding */
1068 pos
->mmap_offset
+= pos
->packet_size
/ CHAR_BIT
;
1071 assert(index
== 0); /* only seek supported for now */
1077 pos
->content_size
= -1U; /* Unknown at this point */
1078 pos
->packet_size
= WRITE_PACKET_LEN
;
1080 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1081 pos
->packet_size
/ CHAR_BIT
);
1082 } while (ret
== EINTR
);
1090 if (pos
->offset
== EOF
) {
1093 assert(pos
->cur_index
< pos
->packet_index
->len
);
1094 /* The reader will expect us to skip padding */
1099 if (index
>= pos
->packet_index
->len
) {
1103 pos
->cur_index
= index
;
1109 if (pos
->cur_index
>= pos
->packet_index
->len
) {
1114 packet_index
= &g_array_index(pos
->packet_index
,
1115 struct packet_index
, pos
->cur_index
);
1116 if (pos
->cur_index
> 0) {
1117 prev_index
= &g_array_index(pos
->packet_index
,
1118 struct packet_index
,
1119 pos
->cur_index
- 1);
1123 ctf_update_current_packet_index(&file_stream
->parent
,
1124 prev_index
, packet_index
);
1127 * We need to check if we are in trace read or called
1128 * from packet indexing. In this last case, the
1129 * collection is not there, so we cannot print the
1132 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1133 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1136 packet_index
= &g_array_index(pos
->packet_index
,
1137 struct packet_index
,
1139 file_stream
->parent
.cycles_timestamp
= packet_index
->ts_cycles
.timestamp_begin
;
1141 file_stream
->parent
.real_timestamp
= packet_index
->ts_real
.timestamp_begin
;
1143 /* Lookup context/packet size in index */
1144 if (packet_index
->data_offset
== -1) {
1145 ret
= find_data_offset(pos
, file_stream
, packet_index
);
1150 pos
->content_size
= packet_index
->content_size
;
1151 pos
->packet_size
= packet_index
->packet_size
;
1152 pos
->mmap_offset
= packet_index
->offset
;
1153 pos
->data_offset
= packet_index
->data_offset
;
1154 if (pos
->data_offset
< packet_index
->content_size
) {
1155 pos
->offset
= 0; /* will read headers */
1156 } else if (pos
->data_offset
== packet_index
->content_size
) {
1158 pos
->offset
= packet_index
->data_offset
;
1160 goto read_next_packet
;
1166 /* map new base. Need mapping length from header. */
1167 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1168 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1169 if (pos
->base_mma
== MAP_FAILED
) {
1170 fprintf(stderr
, "[error] mmap error %s.\n",
1175 /* update trace_packet_header and stream_packet_context */
1176 if (!(pos
->prot
& PROT_WRITE
) &&
1177 file_stream
->parent
.trace_packet_header
) {
1178 /* Read packet header */
1179 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1182 if (!(pos
->prot
& PROT_WRITE
) &&
1183 file_stream
->parent
.stream_packet_context
) {
1184 /* Read packet context */
1185 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1191 int packet_metadata(struct ctf_trace
*td
, FILE *fp
)
1197 len
= fread(&magic
, sizeof(magic
), 1, fp
);
1201 if (magic
== TSDL_MAGIC
) {
1203 td
->byte_order
= BYTE_ORDER
;
1204 CTF_TRACE_SET_FIELD(td
, byte_order
);
1205 } else if (magic
== GUINT32_SWAP_LE_BE(TSDL_MAGIC
)) {
1207 td
->byte_order
= (BYTE_ORDER
== BIG_ENDIAN
) ?
1208 LITTLE_ENDIAN
: BIG_ENDIAN
;
1209 CTF_TRACE_SET_FIELD(td
, byte_order
);
1217 * Returns 0 on success, -1 on error.
1220 int check_version(unsigned int major
, unsigned int minor
)
1235 /* eventually return an error instead of warning */
1237 fprintf(stderr
, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1243 int ctf_trace_metadata_packet_read(struct ctf_trace
*td
, FILE *in
,
1246 struct metadata_packet_header header
;
1247 size_t readlen
, writelen
, toread
;
1248 char buf
[4096 + 1]; /* + 1 for debug-mode \0 */
1251 readlen
= fread(&header
, header_sizeof(header
), 1, in
);
1255 if (td
->byte_order
!= BYTE_ORDER
) {
1256 header
.magic
= GUINT32_SWAP_LE_BE(header
.magic
);
1257 header
.checksum
= GUINT32_SWAP_LE_BE(header
.checksum
);
1258 header
.content_size
= GUINT32_SWAP_LE_BE(header
.content_size
);
1259 header
.packet_size
= GUINT32_SWAP_LE_BE(header
.packet_size
);
1261 if (header
.checksum
)
1262 fprintf(stderr
, "[warning] checksum verification not supported yet.\n");
1263 if (header
.compression_scheme
) {
1264 fprintf(stderr
, "[error] compression (%u) not supported yet.\n",
1265 header
.compression_scheme
);
1268 if (header
.encryption_scheme
) {
1269 fprintf(stderr
, "[error] encryption (%u) not supported yet.\n",
1270 header
.encryption_scheme
);
1273 if (header
.checksum_scheme
) {
1274 fprintf(stderr
, "[error] checksum (%u) not supported yet.\n",
1275 header
.checksum_scheme
);
1278 if (check_version(header
.major
, header
.minor
) < 0)
1280 if (!CTF_TRACE_FIELD_IS_SET(td
, uuid
)) {
1281 memcpy(td
->uuid
, header
.uuid
, sizeof(header
.uuid
));
1282 CTF_TRACE_SET_FIELD(td
, uuid
);
1284 if (bt_uuid_compare(header
.uuid
, td
->uuid
))
1288 if ((header
.content_size
/ CHAR_BIT
) < header_sizeof(header
))
1291 toread
= (header
.content_size
/ CHAR_BIT
) - header_sizeof(header
);
1294 readlen
= fread(buf
, sizeof(char), min(sizeof(buf
) - 1, toread
), in
);
1299 if (babeltrace_debug
) {
1300 buf
[readlen
] = '\0';
1301 fprintf(stderr
, "[debug] metadata packet read: %s\n",
1305 writelen
= fwrite(buf
, sizeof(char), readlen
, out
);
1306 if (writelen
< readlen
) {
1316 ret
= 0; /* continue reading next packet */
1323 toread
= (header
.packet_size
- header
.content_size
) / CHAR_BIT
;
1324 ret
= fseek(in
, toread
, SEEK_CUR
);
1326 fprintf(stderr
, "[warning] Missing padding at end of file\n");
1333 int ctf_trace_metadata_stream_read(struct ctf_trace
*td
, FILE **fp
,
1337 size_t size
, buflen
;
1342 * Using strlen on *buf instead of size of open_memstream
1343 * because its size includes garbage at the end (after final
1344 * \0). This is the allocated size, not the actual string size.
1346 out
= babeltrace_open_memstream(buf
, &size
);
1348 perror("Metadata open_memstream");
1352 ret
= ctf_trace_metadata_packet_read(td
, in
, out
);
1361 /* close to flush the buffer */
1362 ret
= babeltrace_close_memstream(buf
, &size
, out
);
1366 perror("babeltrace_flush_memstream");
1368 closeret
= fclose(in
);
1370 perror("Error in fclose");
1376 perror("Error in fclose");
1378 /* open for reading */
1379 buflen
= strlen(*buf
);
1384 *fp
= babeltrace_fmemopen(*buf
, buflen
, "rb");
1386 perror("Metadata fmemopen");
1393 int ctf_trace_metadata_read(struct ctf_trace
*td
, FILE *metadata_fp
,
1394 struct ctf_scanner
*scanner
, int append
)
1396 struct ctf_file_stream
*metadata_stream
;
1399 int ret
= 0, closeret
;
1401 metadata_stream
= g_new0(struct ctf_file_stream
, 1);
1402 metadata_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
1406 metadata_stream
->pos
.fd
= -1;
1408 td
->metadata
= &metadata_stream
->parent
;
1409 metadata_stream
->pos
.fd
= openat(td
->dirfd
, "metadata", O_RDONLY
);
1410 if (metadata_stream
->pos
.fd
< 0) {
1411 fprintf(stderr
, "Unable to open metadata.\n");
1416 fp
= fdopen(metadata_stream
->pos
.fd
, "r");
1418 fprintf(stderr
, "[error] Unable to open metadata stream.\n");
1419 perror("Metadata stream open");
1423 /* fd now belongs to fp */
1424 metadata_stream
->pos
.fd
= -1;
1426 if (babeltrace_debug
)
1429 if (packet_metadata(td
, fp
)) {
1430 ret
= ctf_trace_metadata_stream_read(td
, &fp
, &buf
);
1434 td
->metadata_string
= buf
;
1435 td
->metadata_packetized
= 1;
1438 unsigned int major
, minor
;
1441 td
->byte_order
= BYTE_ORDER
;
1443 /* Check text-only metadata header and version */
1444 nr_items
= fscanf(fp
, "/* CTF %10u.%10u", &major
, &minor
);
1446 fprintf(stderr
, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1447 if (check_version(major
, minor
) < 0) {
1455 ret
= ctf_scanner_append_ast(scanner
, fp
);
1457 fprintf(stderr
, "[error] Error creating AST\n");
1461 if (babeltrace_debug
) {
1462 ret
= ctf_visitor_print_xml(stderr
, 0, &scanner
->ast
->root
);
1464 fprintf(stderr
, "[error] Error visiting AST for XML output\n");
1469 ret
= ctf_visitor_semantic_check(stderr
, 0, &scanner
->ast
->root
);
1471 fprintf(stderr
, "[error] Error in CTF semantic validation %d\n", ret
);
1474 ret
= ctf_visitor_construct_metadata(stderr
, 0, &scanner
->ast
->root
,
1475 td
, td
->byte_order
);
1477 fprintf(stderr
, "[error] Error in CTF metadata constructor %d\n", ret
);
1482 closeret
= fclose(fp
);
1484 perror("Error on fclose");
1488 if (metadata_stream
->pos
.fd
>= 0) {
1489 closeret
= close(metadata_stream
->pos
.fd
);
1491 perror("Error on metadata stream fd close");
1496 g_free(metadata_stream
);
1501 struct ctf_event_definition
*create_event_definitions(struct ctf_trace
*td
,
1502 struct ctf_stream_definition
*stream
,
1503 struct ctf_event_declaration
*event
)
1505 struct ctf_event_definition
*stream_event
= g_new0(struct ctf_event_definition
, 1);
1507 if (event
->context_decl
) {
1508 struct bt_definition
*definition
=
1509 event
->context_decl
->p
.definition_new(&event
->context_decl
->p
,
1510 stream
->parent_def_scope
, 0, 0, "event.context");
1514 stream_event
->event_context
= container_of(definition
,
1515 struct definition_struct
, p
);
1516 stream
->parent_def_scope
= stream_event
->event_context
->p
.scope
;
1518 if (event
->fields_decl
) {
1519 struct bt_definition
*definition
=
1520 event
->fields_decl
->p
.definition_new(&event
->fields_decl
->p
,
1521 stream
->parent_def_scope
, 0, 0, "event.fields");
1525 stream_event
->event_fields
= container_of(definition
,
1526 struct definition_struct
, p
);
1527 stream
->parent_def_scope
= stream_event
->event_fields
->p
.scope
;
1529 stream_event
->stream
= stream
;
1530 return stream_event
;
1533 if (stream_event
->event_fields
)
1534 bt_definition_unref(&stream_event
->event_fields
->p
);
1535 if (stream_event
->event_context
)
1536 bt_definition_unref(&stream_event
->event_context
->p
);
1537 fprintf(stderr
, "[error] Unable to create event definition for event \"%s\".\n",
1538 g_quark_to_string(event
->name
));
1543 int copy_event_declarations_stream_class_to_stream(struct ctf_trace
*td
,
1544 struct ctf_stream_declaration
*stream_class
,
1545 struct ctf_stream_definition
*stream
)
1547 size_t def_size
, class_size
, i
;
1550 def_size
= stream
->events_by_id
->len
;
1551 class_size
= stream_class
->events_by_id
->len
;
1553 g_ptr_array_set_size(stream
->events_by_id
, class_size
);
1554 for (i
= def_size
; i
< class_size
; i
++) {
1555 struct ctf_event_declaration
*event
=
1556 g_ptr_array_index(stream_class
->events_by_id
, i
);
1557 struct ctf_event_definition
*stream_event
;
1561 stream_event
= create_event_definitions(td
, stream
, event
);
1562 if (!stream_event
) {
1566 g_ptr_array_index(stream
->events_by_id
, i
) = stream_event
;
1573 int create_stream_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1575 struct ctf_stream_declaration
*stream_class
;
1579 if (stream
->stream_definitions_created
)
1582 stream_class
= stream
->stream_class
;
1584 if (stream_class
->packet_context_decl
) {
1585 struct bt_definition
*definition
=
1586 stream_class
->packet_context_decl
->p
.definition_new(&stream_class
->packet_context_decl
->p
,
1587 stream
->parent_def_scope
, 0, 0, "stream.packet.context");
1592 stream
->stream_packet_context
= container_of(definition
,
1593 struct definition_struct
, p
);
1594 stream
->parent_def_scope
= stream
->stream_packet_context
->p
.scope
;
1596 if (stream_class
->event_header_decl
) {
1597 struct bt_definition
*definition
=
1598 stream_class
->event_header_decl
->p
.definition_new(&stream_class
->event_header_decl
->p
,
1599 stream
->parent_def_scope
, 0, 0, "stream.event.header");
1604 stream
->stream_event_header
=
1605 container_of(definition
, struct definition_struct
, p
);
1606 stream
->parent_def_scope
= stream
->stream_event_header
->p
.scope
;
1608 if (stream_class
->event_context_decl
) {
1609 struct bt_definition
*definition
=
1610 stream_class
->event_context_decl
->p
.definition_new(&stream_class
->event_context_decl
->p
,
1611 stream
->parent_def_scope
, 0, 0, "stream.event.context");
1616 stream
->stream_event_context
=
1617 container_of(definition
, struct definition_struct
, p
);
1618 stream
->parent_def_scope
= stream
->stream_event_context
->p
.scope
;
1620 stream
->events_by_id
= g_ptr_array_new();
1621 ret
= copy_event_declarations_stream_class_to_stream(td
,
1622 stream_class
, stream
);
1628 for (i
= 0; i
< stream
->events_by_id
->len
; i
++) {
1629 struct ctf_event_definition
*stream_event
= g_ptr_array_index(stream
->events_by_id
, i
);
1631 g_free(stream_event
);
1633 g_ptr_array_free(stream
->events_by_id
, TRUE
);
1635 if (stream
->stream_event_context
)
1636 bt_definition_unref(&stream
->stream_event_context
->p
);
1637 if (stream
->stream_event_header
)
1638 bt_definition_unref(&stream
->stream_event_header
->p
);
1639 if (stream
->stream_packet_context
)
1640 bt_definition_unref(&stream
->stream_packet_context
->p
);
1641 fprintf(stderr
, "[error] Unable to create stream (%" PRIu64
") definitions: %s\n",
1642 stream_class
->stream_id
, strerror(-ret
));
1647 int stream_assign_class(struct ctf_trace
*td
,
1648 struct ctf_file_stream
*file_stream
,
1651 struct ctf_stream_declaration
*stream
;
1654 file_stream
->parent
.stream_id
= stream_id
;
1655 if (stream_id
>= td
->streams
->len
) {
1656 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1659 stream
= g_ptr_array_index(td
->streams
, stream_id
);
1661 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared in metadata.\n", stream_id
);
1664 file_stream
->parent
.stream_class
= stream
;
1665 ret
= create_stream_definitions(td
, &file_stream
->parent
);
1672 int create_stream_one_packet_index(struct ctf_stream_pos
*pos
,
1673 struct ctf_trace
*td
,
1674 struct ctf_file_stream
*file_stream
,
1677 struct packet_index packet_index
;
1678 uint64_t stream_id
= 0;
1679 uint64_t packet_map_len
= DEFAULT_HEADER_LEN
, tmp_map_len
;
1680 int first_packet
= 0;
1685 memset(&packet_index
, 0, sizeof(packet_index
));
1686 if (!pos
->mmap_offset
) {
1690 if (filesize
- pos
->mmap_offset
< (packet_map_len
>> LOG2_CHAR_BIT
)) {
1691 packet_map_len
= (filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
;
1694 if (pos
->base_mma
) {
1695 /* unmap old base */
1696 ret
= munmap_align(pos
->base_mma
);
1698 fprintf(stderr
, "[error] Unable to unmap old base: %s.\n",
1702 pos
->base_mma
= NULL
;
1704 /* map new base. Need mapping length from header. */
1705 pos
->base_mma
= mmap_align(packet_map_len
>> LOG2_CHAR_BIT
, PROT_READ
,
1706 MAP_PRIVATE
, pos
->fd
, pos
->mmap_offset
);
1707 assert(pos
->base_mma
!= MAP_FAILED
);
1709 * Use current mapping size as temporary content and packet
1712 pos
->content_size
= packet_map_len
;
1713 pos
->packet_size
= packet_map_len
;
1714 pos
->offset
= 0; /* Position of the packet header */
1716 packet_index
.offset
= pos
->mmap_offset
;
1718 /* read and check header, set stream id (and check) */
1719 if (file_stream
->parent
.trace_packet_header
) {
1720 /* Read packet header */
1721 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.trace_packet_header
->p
);
1725 fprintf(stderr
, "[error] Unable to read packet header: %s\n", strerror(-ret
));
1728 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("magic"));
1729 if (len_index
>= 0) {
1730 struct bt_definition
*field
;
1733 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1734 magic
= bt_get_unsigned_int(field
);
1735 if (magic
!= CTF_MAGIC
) {
1736 fprintf(stderr
, "[error] Invalid magic number 0x%" PRIX64
" at packet %u (file offset %zd).\n",
1738 file_stream
->pos
.packet_index
->len
,
1739 (ssize_t
) pos
->mmap_offset
);
1745 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("uuid"));
1746 if (len_index
>= 0) {
1747 struct definition_array
*defarray
;
1748 struct bt_definition
*field
;
1750 uint8_t uuidval
[BABELTRACE_UUID_LEN
];
1752 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1753 assert(field
->declaration
->id
== BT_CTF_TYPE_ID_ARRAY
);
1754 defarray
= container_of(field
, struct definition_array
, p
);
1755 assert(bt_array_len(defarray
) == BABELTRACE_UUID_LEN
);
1757 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
1758 struct bt_definition
*elem
;
1760 elem
= bt_array_index(defarray
, i
);
1761 uuidval
[i
] = bt_get_unsigned_int(elem
);
1763 ret
= bt_uuid_compare(td
->uuid
, uuidval
);
1765 fprintf(stderr
, "[error] Unique Universal Identifiers do not match.\n");
1770 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.trace_packet_header
->declaration
, g_quark_from_static_string("stream_id"));
1771 if (len_index
>= 0) {
1772 struct bt_definition
*field
;
1774 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.trace_packet_header
, len_index
);
1775 stream_id
= bt_get_unsigned_int(field
);
1779 if (!first_packet
&& file_stream
->parent
.stream_id
!= stream_id
) {
1780 fprintf(stderr
, "[error] Stream ID is changing within a stream: expecting %" PRIu64
", but packet has %" PRIu64
"\n",
1782 file_stream
->parent
.stream_id
);
1786 ret
= stream_assign_class(td
, file_stream
, stream_id
);
1791 if (file_stream
->parent
.stream_packet_context
) {
1792 /* Read packet context */
1793 ret
= generic_rw(&pos
->parent
, &file_stream
->parent
.stream_packet_context
->p
);
1797 fprintf(stderr
, "[error] Unable to read packet context: %s\n", strerror(-ret
));
1800 /* read packet size from header */
1801 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("packet_size"));
1802 if (len_index
>= 0) {
1803 struct bt_definition
*field
;
1805 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1806 packet_index
.packet_size
= bt_get_unsigned_int(field
);
1808 /* Use file size for packet size */
1809 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1812 /* read content size from header */
1813 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("content_size"));
1814 if (len_index
>= 0) {
1815 struct bt_definition
*field
;
1817 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1818 packet_index
.content_size
= bt_get_unsigned_int(field
);
1820 /* Use packet size if non-zero, else file size */
1821 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1824 /* read timestamp begin from header */
1825 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_begin"));
1826 if (len_index
>= 0) {
1827 struct bt_definition
*field
;
1829 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1830 packet_index
.ts_cycles
.timestamp_begin
= bt_get_unsigned_int(field
);
1831 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1832 packet_index
.ts_real
.timestamp_begin
=
1833 ctf_get_real_timestamp(
1834 &file_stream
->parent
,
1835 packet_index
.ts_cycles
.timestamp_begin
);
1839 /* read timestamp end from header */
1840 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("timestamp_end"));
1841 if (len_index
>= 0) {
1842 struct bt_definition
*field
;
1844 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1845 packet_index
.ts_cycles
.timestamp_end
= bt_get_unsigned_int(field
);
1846 if (file_stream
->parent
.stream_class
->trace
->parent
.collection
) {
1847 packet_index
.ts_real
.timestamp_end
=
1848 ctf_get_real_timestamp(
1849 &file_stream
->parent
,
1850 packet_index
.ts_cycles
.timestamp_end
);
1854 /* read events discarded from header */
1855 len_index
= bt_struct_declaration_lookup_field_index(file_stream
->parent
.stream_packet_context
->declaration
, g_quark_from_static_string("events_discarded"));
1856 if (len_index
>= 0) {
1857 struct bt_definition
*field
;
1859 field
= bt_struct_definition_get_field_from_index(file_stream
->parent
.stream_packet_context
, len_index
);
1860 packet_index
.events_discarded
= bt_get_unsigned_int(field
);
1861 packet_index
.events_discarded_len
= bt_get_int_len(field
);
1864 /* read packet_seq_num from header */
1865 len_index
= bt_struct_declaration_lookup_field_index(
1866 file_stream
->parent
.stream_packet_context
->declaration
,
1867 g_quark_from_static_string("packet_seq_num"));
1868 if (len_index
>= 0) {
1869 struct bt_definition
*field
;
1871 field
= bt_struct_definition_get_field_from_index(
1872 file_stream
->parent
.stream_packet_context
,
1874 packet_index
.packet_seq_num
= bt_get_unsigned_int(field
);
1877 /* Use file size for packet size */
1878 packet_index
.packet_size
= filesize
* CHAR_BIT
;
1879 /* Use packet size if non-zero, else file size */
1880 packet_index
.content_size
= packet_index
.packet_size
? : filesize
* CHAR_BIT
;
1883 /* Validate content size and packet size values */
1884 if (packet_index
.content_size
> packet_index
.packet_size
) {
1885 fprintf(stderr
, "[error] Content size (%" PRIu64
" bits) is larger than packet size (%" PRIu64
" bits).\n",
1886 packet_index
.content_size
, packet_index
.packet_size
);
1890 if (packet_index
.packet_size
> ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
) {
1891 fprintf(stderr
, "[error] Packet size (%" PRIu64
" bits) is larger than remaining file size (%" PRIu64
" bits).\n",
1892 packet_index
.packet_size
, ((uint64_t) filesize
- packet_index
.offset
) * CHAR_BIT
);
1896 if (packet_index
.content_size
< pos
->offset
) {
1897 fprintf(stderr
, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1901 if ((packet_index
.packet_size
>> LOG2_CHAR_BIT
) == 0) {
1902 fprintf(stderr
, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1906 /* Save position after header and context */
1907 packet_index
.data_offset
= pos
->offset
;
1909 /* add index to packet array */
1910 g_array_append_val(file_stream
->pos
.packet_index
, packet_index
);
1912 pos
->mmap_offset
+= packet_index
.packet_size
>> LOG2_CHAR_BIT
;
1916 /* Retry with larger mapping */
1918 if (packet_map_len
== ((filesize
- pos
->mmap_offset
) << LOG2_CHAR_BIT
)) {
1920 * Reached EOF, but still expecting header/context data.
1922 fprintf(stderr
, "[error] Reached end of file, but still expecting header or context fields.\n");
1925 /* Double the mapping len, and retry */
1926 tmp_map_len
= packet_map_len
<< 1;
1927 if (tmp_map_len
>> 1 != packet_map_len
) {
1929 fprintf(stderr
, "[error] Packet mapping length overflow\n");
1932 packet_map_len
= tmp_map_len
;
1937 int create_stream_packet_index(struct ctf_trace
*td
,
1938 struct ctf_file_stream
*file_stream
)
1940 struct ctf_stream_pos
*pos
;
1941 struct stat filestats
;
1944 pos
= &file_stream
->pos
;
1946 ret
= fstat(pos
->fd
, &filestats
);
1950 /* Deal with empty files */
1951 if (!filestats
.st_size
) {
1952 if (file_stream
->parent
.trace_packet_header
1953 || file_stream
->parent
.stream_packet_context
) {
1955 * We expect a trace packet header and/or stream packet
1956 * context. Since a trace needs to have at least one
1957 * packet, empty files are therefore not accepted.
1959 fprintf(stderr
, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1963 * Without trace packet header nor stream packet
1964 * context, a one-packet trace can indeed be empty. This
1965 * is only valid if there is only one stream class: 0.
1967 ret
= stream_assign_class(td
, file_stream
, 0);
1974 for (pos
->mmap_offset
= 0; pos
->mmap_offset
< filestats
.st_size
; ) {
1975 ret
= create_stream_one_packet_index(pos
, td
, file_stream
,
1984 int create_trace_definitions(struct ctf_trace
*td
, struct ctf_stream_definition
*stream
)
1988 if (td
->packet_header_decl
) {
1989 struct bt_definition
*definition
=
1990 td
->packet_header_decl
->p
.definition_new(&td
->packet_header_decl
->p
,
1991 stream
->parent_def_scope
, 0, 0, "trace.packet.header");
1996 stream
->trace_packet_header
=
1997 container_of(definition
, struct definition_struct
, p
);
1998 stream
->parent_def_scope
= stream
->trace_packet_header
->p
.scope
;
2004 fprintf(stderr
, "[error] Unable to create trace definitions: %s\n", strerror(-ret
));
2009 int import_stream_packet_index(struct ctf_trace
*td
,
2010 struct ctf_file_stream
*file_stream
)
2012 struct ctf_stream_pos
*pos
;
2013 struct ctf_packet_index
*ctf_index
= NULL
;
2014 struct ctf_packet_index_file_hdr index_hdr
;
2015 struct packet_index index
;
2016 uint32_t packet_index_len
, index_minor
;
2018 int first_packet
= 1;
2021 pos
= &file_stream
->pos
;
2023 len
= fread(&index_hdr
, sizeof(index_hdr
), 1, pos
->index_fp
);
2025 perror("read index file header");
2029 /* Check the index header */
2030 if (be32toh(index_hdr
.magic
) != CTF_INDEX_MAGIC
) {
2031 fprintf(stderr
, "[error] wrong index magic\n");
2035 if (be32toh(index_hdr
.index_major
) != CTF_INDEX_MAJOR
) {
2036 fprintf(stderr
, "[error] Incompatible index file %" PRIu32
2037 ".%" PRIu32
", supported %d.%d\n",
2038 be32toh(index_hdr
.index_major
),
2039 be32toh(index_hdr
.index_minor
), CTF_INDEX_MAJOR
,
2044 index_minor
= be32toh(index_hdr
.index_minor
);
2046 packet_index_len
= be32toh(index_hdr
.packet_index_len
);
2047 if (packet_index_len
== 0) {
2048 fprintf(stderr
, "[error] Packet index length cannot be 0.\n");
2053 * Allocate the index length found in header, not internal
2056 ctf_index
= g_malloc0(packet_index_len
);
2057 while (fread(ctf_index
, packet_index_len
, 1,
2058 pos
->index_fp
) == 1) {
2060 struct ctf_stream_declaration
*stream
= NULL
;
2062 memset(&index
, 0, sizeof(index
));
2063 index
.offset
= be64toh(ctf_index
->offset
);
2064 index
.packet_size
= be64toh(ctf_index
->packet_size
);
2065 index
.content_size
= be64toh(ctf_index
->content_size
);
2066 index
.ts_cycles
.timestamp_begin
= be64toh(ctf_index
->timestamp_begin
);
2067 index
.ts_cycles
.timestamp_end
= be64toh(ctf_index
->timestamp_end
);
2068 index
.events_discarded
= be64toh(ctf_index
->events_discarded
);
2069 index
.events_discarded_len
= 64;
2070 index
.data_offset
= -1;
2071 stream_id
= be64toh(ctf_index
->stream_id
);
2072 if (index_minor
>= 1) {
2073 index
.stream_instance_id
= be64toh(ctf_index
->stream_instance_id
);
2074 index
.packet_seq_num
= be64toh(ctf_index
->packet_seq_num
);
2077 if (!first_packet
) {
2078 /* add index to packet array */
2079 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2083 file_stream
->parent
.stream_id
= stream_id
;
2084 if (stream_id
< td
->streams
->len
) {
2085 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2088 fprintf(stderr
, "[error] Stream %" PRIu64
2089 " is not declared in metadata.\n",
2094 file_stream
->parent
.stream_class
= stream
;
2095 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2099 /* add index to packet array */
2100 g_array_append_val(file_stream
->pos
.packet_index
, index
);
2103 /* Index containing only the header. */
2104 if (!file_stream
->parent
.stream_class
) {
2117 * Note: many file streams can inherit from the same stream class
2118 * description (metadata).
2121 int ctf_open_file_stream_read(struct ctf_trace
*td
, const char *path
, int flags
,
2122 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2125 int ret
, fd
, closeret
;
2126 struct ctf_file_stream
*file_stream
;
2127 struct stat statbuf
;
2130 fd
= openat(td
->dirfd
, path
, flags
);
2132 perror("File stream openat()");
2137 /* Don't try to mmap subdirectories. Skip them, return success. */
2138 ret
= fstat(fd
, &statbuf
);
2140 perror("File stream fstat()");
2143 if (S_ISDIR(statbuf
.st_mode
)) {
2144 if (strncmp(path
, "index", 5) != 0) {
2145 fprintf(stderr
, "[warning] Skipping directory '%s' "
2146 "found in trace\n", path
);
2151 if (!statbuf
.st_size
) {
2152 /** Skip empty files. */
2154 goto fd_is_empty_file
;
2157 file_stream
= g_new0(struct ctf_file_stream
, 1);
2158 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2159 file_stream
->pos
.fd
= -1;
2160 file_stream
->pos
.index_fp
= NULL
;
2162 strncpy(file_stream
->parent
.path
, path
, PATH_MAX
);
2163 file_stream
->parent
.path
[PATH_MAX
- 1] = '\0';
2166 file_stream
->pos
.packet_seek
= packet_seek
;
2168 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2173 ret
= ctf_init_pos(&file_stream
->pos
, &td
->parent
, fd
, flags
);
2176 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2180 * For now, only a single clock per trace is supported.
2182 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2185 * Allocate the index name for this stream and try to open it.
2187 index_name
= malloc((strlen(path
) + sizeof(INDEX_PATH
)) * sizeof(char));
2189 fprintf(stderr
, "[error] Cannot allocate index filename\n");
2193 snprintf(index_name
, strlen(path
) + sizeof(INDEX_PATH
),
2196 if (bt_faccessat(td
->dirfd
, td
->parent
.path
, index_name
, O_RDONLY
, 0) < 0) {
2197 ret
= create_stream_packet_index(td
, file_stream
);
2199 fprintf(stderr
, "[error] Stream index creation error.\n");
2203 ret
= openat(td
->dirfd
, index_name
, flags
);
2205 perror("Index file openat()");
2209 file_stream
->pos
.index_fp
= fdopen(ret
, "r");
2210 if (!file_stream
->pos
.index_fp
) {
2211 perror("fdopen() error");
2214 ret
= import_stream_packet_index(td
, file_stream
);
2219 ret
= fclose(file_stream
->pos
.index_fp
);
2221 perror("close index");
2227 /* Add stream file to stream class */
2228 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2229 &file_stream
->parent
);
2233 if (file_stream
->pos
.index_fp
) {
2234 ret
= fclose(file_stream
->pos
.index_fp
);
2236 perror("close index");
2239 if (file_stream
->parent
.trace_packet_header
)
2240 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2244 closeret
= ctf_fini_pos(&file_stream
->pos
);
2246 fprintf(stderr
, "Error on ctf_fini_pos\n");
2248 g_free(file_stream
);
2252 closeret
= close(fd
);
2254 perror("Error on fd close");
2261 int ctf_open_trace_read(struct ctf_trace
*td
,
2262 const char *path
, int flags
,
2263 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2264 int whence
), FILE *metadata_fp
)
2266 struct ctf_scanner
*scanner
;
2268 struct dirent
*dirent
;
2269 struct dirent
*diriter
;
2276 /* Open trace directory */
2277 td
->dir
= opendir(path
);
2279 fprintf(stderr
, "[error] Unable to open trace directory \"%s\".\n", path
);
2284 td
->dirfd
= open(path
, 0);
2285 if (td
->dirfd
< 0) {
2286 fprintf(stderr
, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path
);
2287 perror("Trace directory open");
2291 strncpy(td
->parent
.path
, path
, sizeof(td
->parent
.path
));
2292 td
->parent
.path
[sizeof(td
->parent
.path
) - 1] = '\0';
2295 * Keep the metadata file separate.
2296 * Keep scanner object local to the open. We don't support
2297 * incremental metadata append for on-disk traces.
2299 scanner
= ctf_scanner_alloc();
2301 fprintf(stderr
, "[error] Error allocating scanner\n");
2303 goto error_metadata
;
2305 ret
= ctf_trace_metadata_read(td
, metadata_fp
, scanner
, 0);
2306 ctf_scanner_free(scanner
);
2308 if (ret
== -ENOENT
) {
2309 fprintf(stderr
, "[warning] Empty metadata.\n");
2311 fprintf(stderr
, "[warning] Unable to open trace metadata for path \"%s\".\n", path
);
2312 goto error_metadata
;
2316 * Open each stream: for each file, try to open, check magic
2317 * number, and get the stream ID to add to the right location in
2321 pc_name_max
= fpathconf(td
->dirfd
, _PC_NAME_MAX
);
2322 if (pc_name_max
< 0) {
2323 perror("Error on fpathconf");
2324 fprintf(stderr
, "[error] Failed to get _PC_NAME_MAX for path \"%s\".\n", path
);
2326 goto error_metadata
;
2329 dirent_len
= offsetof(struct dirent
, d_name
) + pc_name_max
+ 1;
2331 dirent
= malloc(dirent_len
);
2334 ret
= readdir_r(td
->dir
, dirent
, &diriter
);
2336 fprintf(stderr
, "[error] Readdir error.\n");
2341 /* Ignore hidden files, ., .. and metadata. */
2342 if (!strncmp(diriter
->d_name
, ".", 1)
2343 || !strcmp(diriter
->d_name
, "..")
2344 || !strcmp(diriter
->d_name
, "metadata"))
2347 /* Ignore index files : *.idx */
2348 ext
= strrchr(diriter
->d_name
, '.');
2349 if (ext
&& (!strcmp(ext
, ".idx"))) {
2353 ret
= ctf_open_file_stream_read(td
, diriter
->d_name
,
2354 flags
, packet_seek
);
2356 fprintf(stderr
, "[error] Open file stream error.\n");
2367 closeret
= close(td
->dirfd
);
2369 perror("Error on fd close");
2372 closeret
= closedir(td
->dir
);
2374 perror("Error on closedir");
2381 * ctf_open_trace: Open a CTF trace and index it.
2382 * Note that the user must seek the trace after the open (using the iterator)
2383 * since the index creation read it entirely.
2386 struct bt_trace_descriptor
*ctf_open_trace(const char *path
, int flags
,
2387 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2388 int whence
), FILE *metadata_fp
)
2390 struct ctf_trace
*td
;
2394 * If packet_seek is NULL, we provide our default version.
2397 packet_seek
= ctf_packet_seek
;
2399 td
= g_new0(struct ctf_trace
, 1);
2401 switch (flags
& O_ACCMODE
) {
2404 fprintf(stderr
, "[error] Path missing for input CTF trace.\n");
2407 ret
= ctf_open_trace_read(td
, path
, flags
, packet_seek
, metadata_fp
);
2412 fprintf(stderr
, "[error] Opening CTF traces for output is not supported yet.\n");
2415 fprintf(stderr
, "[error] Incorrect open flags.\n");
2419 ret
= trace_debug_info_create(td
);
2426 trace_debug_info_destroy(td
);
2432 void ctf_init_mmap_pos(struct ctf_stream_pos
*pos
,
2433 struct bt_mmap_stream
*mmap_info
)
2435 pos
->mmap_offset
= 0;
2436 pos
->packet_size
= 0;
2437 pos
->content_size
= 0;
2438 pos
->content_size_loc
= NULL
;
2439 pos
->fd
= mmap_info
->fd
;
2440 pos
->base_mma
= NULL
;
2444 pos
->prot
= PROT_READ
;
2445 pos
->flags
= MAP_PRIVATE
;
2446 pos
->parent
.rw_table
= read_dispatch_table
;
2447 pos
->parent
.event_cb
= ctf_read_event
;
2448 pos
->priv
= mmap_info
->priv
;
2449 pos
->packet_index
= g_array_new(FALSE
, TRUE
,
2450 sizeof(struct packet_index
));
2454 int prepare_mmap_stream_definition(struct ctf_trace
*td
,
2455 struct ctf_file_stream
*file_stream
,
2456 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2459 struct ctf_stream_declaration
*stream
;
2463 /* Ask for the first packet to get the stream_id. */
2464 packet_seek(&file_stream
->pos
.parent
, 0, SEEK_SET
);
2465 stream_id
= file_stream
->parent
.stream_id
;
2466 if (stream_id
>= td
->streams
->len
) {
2467 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2468 "in metadata.\n", stream_id
);
2472 stream
= g_ptr_array_index(td
->streams
, stream_id
);
2474 fprintf(stderr
, "[error] Stream %" PRIu64
" is not declared "
2475 "in metadata.\n", stream_id
);
2479 file_stream
->parent
.stream_class
= stream
;
2480 ret
= create_stream_definitions(td
, &file_stream
->parent
);
2486 int ctf_open_mmap_stream_read(struct ctf_trace
*td
,
2487 struct bt_mmap_stream
*mmap_info
,
2488 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2492 struct ctf_file_stream
*file_stream
;
2494 file_stream
= g_new0(struct ctf_file_stream
, 1);
2495 file_stream
->parent
.stream_id
= -1ULL;
2496 file_stream
->pos
.last_offset
= LAST_OFFSET_POISON
;
2497 ctf_init_mmap_pos(&file_stream
->pos
, mmap_info
);
2499 file_stream
->pos
.packet_seek
= packet_seek
;
2501 ret
= create_trace_definitions(td
, &file_stream
->parent
);
2506 ret
= prepare_mmap_stream_definition(td
, file_stream
, packet_seek
);
2511 * For now, only a single clock per trace is supported.
2513 file_stream
->parent
.current_clock
= td
->parent
.single_clock
;
2515 /* Add stream file to stream class */
2516 g_ptr_array_add(file_stream
->parent
.stream_class
->streams
,
2517 &file_stream
->parent
);
2521 if (file_stream
->parent
.trace_packet_header
)
2522 bt_definition_unref(&file_stream
->parent
.trace_packet_header
->p
);
2524 g_free(file_stream
);
2529 int ctf_open_mmap_trace_read(struct ctf_trace
*td
,
2530 struct bt_mmap_stream_list
*mmap_list
,
2531 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2536 struct bt_mmap_stream
*mmap_info
;
2538 td
->scanner
= ctf_scanner_alloc();
2540 fprintf(stderr
, "[error] Error allocating scanner\n");
2544 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 0);
2546 if (ret
== -ENOENT
) {
2547 fprintf(stderr
, "[warning] Empty metadata.\n");
2553 * for each stream, try to open, check magic number, and get the
2554 * stream ID to add to the right location in the stream array.
2556 bt_list_for_each_entry(mmap_info
, &mmap_list
->head
, list
) {
2557 ret
= ctf_open_mmap_stream_read(td
, mmap_info
, packet_seek
);
2559 fprintf(stderr
, "[error] Open file mmap stream error.\n");
2566 ctf_scanner_free(td
->scanner
);
2571 struct bt_trace_descriptor
*ctf_open_mmap_trace(
2572 struct bt_mmap_stream_list
*mmap_list
,
2573 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
2577 struct ctf_trace
*td
;
2581 fprintf(stderr
, "[error] No metadata file pointer associated, "
2582 "required for mmap parsing\n");
2586 fprintf(stderr
, "[error] packet_seek function undefined.\n");
2589 td
= g_new0(struct ctf_trace
, 1);
2591 ret
= ctf_open_mmap_trace_read(td
, mmap_list
, packet_seek
, metadata_fp
);
2595 ret
= trace_debug_info_create(td
);
2608 int ctf_append_trace_metadata(struct bt_trace_descriptor
*tdp
,
2611 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2617 ret
= ctf_trace_metadata_read(td
, metadata_fp
, td
->scanner
, 1);
2620 /* for each stream_class */
2621 for (i
= 0; i
< td
->streams
->len
; i
++) {
2622 struct ctf_stream_declaration
*stream_class
;
2624 stream_class
= g_ptr_array_index(td
->streams
, i
);
2627 /* for each stream */
2628 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2629 struct ctf_stream_definition
*stream
;
2631 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2634 ret
= copy_event_declarations_stream_class_to_stream(td
,
2635 stream_class
, stream
);
2644 int ctf_convert_index_timestamp(struct bt_trace_descriptor
*tdp
)
2647 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2649 /* for each stream_class */
2650 for (i
= 0; i
< td
->streams
->len
; i
++) {
2651 struct ctf_stream_declaration
*stream_class
;
2653 stream_class
= g_ptr_array_index(td
->streams
, i
);
2656 /* for each file_stream */
2657 for (j
= 0; j
< stream_class
->streams
->len
; j
++) {
2658 struct ctf_stream_definition
*stream
;
2659 struct ctf_stream_pos
*stream_pos
;
2660 struct ctf_file_stream
*cfs
;
2662 stream
= g_ptr_array_index(stream_class
->streams
, j
);
2665 cfs
= container_of(stream
, struct ctf_file_stream
,
2667 stream_pos
= &cfs
->pos
;
2668 if (!stream_pos
->packet_index
)
2671 for (k
= 0; k
< stream_pos
->packet_index
->len
; k
++) {
2672 struct packet_index
*index
;
2674 index
= &g_array_index(stream_pos
->packet_index
,
2675 struct packet_index
, k
);
2676 index
->ts_real
.timestamp_begin
=
2677 ctf_get_real_timestamp(stream
,
2678 index
->ts_cycles
.timestamp_begin
);
2679 index
->ts_real
.timestamp_end
=
2680 ctf_get_real_timestamp(stream
,
2681 index
->ts_cycles
.timestamp_end
);
2689 int ctf_close_file_stream(struct ctf_file_stream
*file_stream
)
2693 ret
= ctf_fini_pos(&file_stream
->pos
);
2695 fprintf(stderr
, "Error on ctf_fini_pos\n");
2698 if (file_stream
->pos
.fd
>= 0) {
2699 ret
= close(file_stream
->pos
.fd
);
2701 perror("Error closing file fd");
2709 int ctf_close_trace(struct bt_trace_descriptor
*tdp
)
2711 struct ctf_trace
*td
= container_of(tdp
, struct ctf_trace
, parent
);
2717 for (i
= 0; i
< td
->streams
->len
; i
++) {
2718 struct ctf_stream_declaration
*stream
;
2721 stream
= g_ptr_array_index(td
->streams
, i
);
2724 for (j
= 0; j
< stream
->streams
->len
; j
++) {
2725 struct ctf_file_stream
*file_stream
;
2726 file_stream
= container_of(g_ptr_array_index(stream
->streams
, j
),
2727 struct ctf_file_stream
, parent
);
2728 ret
= ctf_close_file_stream(file_stream
);
2734 ctf_destroy_metadata(td
);
2735 ctf_scanner_free(td
->scanner
);
2736 if (td
->dirfd
>= 0) {
2737 ret
= close(td
->dirfd
);
2739 perror("Error closing dirfd");
2744 ret
= closedir(td
->dir
);
2746 perror("Error closedir");
2750 free(td
->metadata_string
);
2751 trace_debug_info_destroy(td
);
2757 void ctf_set_context(struct bt_trace_descriptor
*descriptor
,
2758 struct bt_context
*ctx
)
2760 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2763 td
->parent
.ctx
= ctx
;
2767 void ctf_set_handle(struct bt_trace_descriptor
*descriptor
,
2768 struct bt_trace_handle
*handle
)
2770 struct ctf_trace
*td
= container_of(descriptor
, struct ctf_trace
,
2773 td
->parent
.handle
= handle
;
2777 void __attribute__((constructor
)) ctf_init(void)
2781 ctf_format
.name
= g_quark_from_static_string("ctf");
2782 ret
= bt_register_format(&ctf_format
);
2787 void __attribute__((destructor
)) ctf_exit(void)
2789 bt_unregister_format(&ctf_format
);