ebc6d0e8d2023010e4807b8e2833148f47f7c14f
[babeltrace.git] / formats / ctf / ctf.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
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>
40 #include <inttypes.h>
41 #include <stdio.h>
42 #include <sys/mman.h>
43 #include <errno.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <dirent.h>
48 #include <glib.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51
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>
58
59 #define LOG2_CHAR_BIT 3
60
61 /*
62 * Length of first attempt at mapping a packet header, in bits.
63 */
64 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
65
66 /*
67 * Lenght of packet to write, in bits.
68 */
69 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
70
71 #ifndef min
72 #define min(a, b) (((a) < (b)) ? (a) : (b))
73 #endif
74
75 #define NSEC_PER_SEC 1000000000LL
76
77 #define INDEX_PATH "./index/%s.idx"
78
79 int opt_clock_cycles,
80 opt_clock_seconds,
81 opt_clock_date,
82 opt_clock_gmt;
83
84 int64_t opt_clock_offset;
85 int64_t opt_clock_offset_ns;
86
87 extern int yydebug;
88 char *opt_debug_info_dir;
89
90 /*
91 * TODO: babeltrace_ctf_console_output ensures that we only print
92 * discarded events when ctf-text plugin is used. Should be cleaned up
93 * with the plugin system redesign.
94 */
95 int babeltrace_ctf_console_output;
96
97 static
98 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
99 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
100 int whence),
101 FILE *metadata_fp);
102 static
103 struct bt_trace_descriptor *ctf_open_mmap_trace(
104 struct bt_mmap_stream_list *mmap_list,
105 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
106 int whence),
107 FILE *metadata_fp);
108 static
109 void ctf_set_context(struct bt_trace_descriptor *descriptor,
110 struct bt_context *ctx);
111 static
112 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
113 struct bt_trace_handle *handle);
114
115 static
116 int ctf_close_trace(struct bt_trace_descriptor *descriptor);
117 static
118 int ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
119 struct bt_trace_handle *handle, enum bt_clock_type type,
120 int64_t *timestamp);
121 static
122 int ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
123 struct bt_trace_handle *handle, enum bt_clock_type type,
124 int64_t *timestamp);
125 static
126 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
127
128 static
129 rw_dispatch read_dispatch_table[] = {
130 [ BT_CTF_TYPE_ID_INTEGER ] = ctf_integer_read,
131 [ BT_CTF_TYPE_ID_FLOAT ] = ctf_float_read,
132 [ BT_CTF_TYPE_ID_ENUM ] = ctf_enum_read,
133 [ BT_CTF_TYPE_ID_STRING ] = ctf_string_read,
134 [ BT_CTF_TYPE_ID_STRUCT ] = ctf_struct_rw,
135 [ BT_CTF_TYPE_ID_VARIANT ] = ctf_variant_rw,
136 [ BT_CTF_TYPE_ID_ARRAY ] = ctf_array_read,
137 [ BT_CTF_TYPE_ID_SEQUENCE ] = ctf_sequence_read,
138 };
139
140 static
141 rw_dispatch write_dispatch_table[] = {
142 [ BT_CTF_TYPE_ID_INTEGER ] = ctf_integer_write,
143 [ BT_CTF_TYPE_ID_FLOAT ] = ctf_float_write,
144 [ BT_CTF_TYPE_ID_ENUM ] = ctf_enum_write,
145 [ BT_CTF_TYPE_ID_STRING ] = ctf_string_write,
146 [ BT_CTF_TYPE_ID_STRUCT ] = ctf_struct_rw,
147 [ BT_CTF_TYPE_ID_VARIANT ] = ctf_variant_rw,
148 [ BT_CTF_TYPE_ID_ARRAY ] = ctf_array_write,
149 [ BT_CTF_TYPE_ID_SEQUENCE ] = ctf_sequence_write,
150 };
151
152 static
153 struct bt_format ctf_format = {
154 .open_trace = ctf_open_trace,
155 .open_mmap_trace = ctf_open_mmap_trace,
156 .close_trace = ctf_close_trace,
157 .set_context = ctf_set_context,
158 .set_handle = ctf_set_handle,
159 .timestamp_begin = ctf_timestamp_begin,
160 .timestamp_end = ctf_timestamp_end,
161 .convert_index_timestamp = ctf_convert_index_timestamp,
162 };
163
164 static
165 int ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
166 struct bt_trace_handle *handle, enum bt_clock_type type,
167 int64_t *timestamp)
168 {
169 struct ctf_trace *tin;
170 int64_t begin = LLONG_MAX;
171 int i, j, ret;
172
173 tin = container_of(descriptor, struct ctf_trace, parent);
174
175 if (!tin || !timestamp) {
176 ret = -EINVAL;
177 goto error;
178 }
179
180 /* for each stream_class */
181 for (i = 0; i < tin->streams->len; i++) {
182 struct ctf_stream_declaration *stream_class;
183
184 stream_class = g_ptr_array_index(tin->streams, i);
185 if (!stream_class)
186 continue;
187 /* for each file_stream */
188 for (j = 0; j < stream_class->streams->len; j++) {
189 struct ctf_stream_definition *stream;
190 struct ctf_file_stream *cfs;
191 struct ctf_stream_pos *stream_pos;
192 struct packet_index *index;
193
194 stream = g_ptr_array_index(stream_class->streams, j);
195 cfs = container_of(stream, struct ctf_file_stream,
196 parent);
197 stream_pos = &cfs->pos;
198
199 if (!stream_pos->packet_index) {
200 ret = -EINVAL;
201 goto error;
202 }
203
204 if (stream_pos->packet_index->len <= 0)
205 continue;
206
207 index = &g_array_index(stream_pos->packet_index,
208 struct packet_index,
209 stream_pos->packet_index->len - 1);
210 if (type == BT_CLOCK_REAL) {
211 if (index->ts_real.timestamp_begin < begin)
212 begin = index->ts_real.timestamp_begin;
213 } else if (type == BT_CLOCK_CYCLES) {
214 if (index->ts_cycles.timestamp_begin < begin)
215 begin = index->ts_cycles.timestamp_begin;
216 } else {
217 ret = -EINVAL;
218 goto error;
219 }
220 }
221 }
222 if (begin == LLONG_MAX) {
223 ret = -ENOENT;
224 goto error;
225 }
226 *timestamp = begin;
227 return 0;
228
229 error:
230 return ret;
231 }
232
233 static
234 int ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
235 struct bt_trace_handle *handle, enum bt_clock_type type,
236 int64_t *timestamp)
237 {
238 struct ctf_trace *tin;
239 int64_t end = LLONG_MIN;
240 int i, j, ret;
241
242 tin = container_of(descriptor, struct ctf_trace, parent);
243
244 if (!tin || !timestamp) {
245 ret = -EINVAL;
246 goto error;
247 }
248
249 /* for each stream_class */
250 for (i = 0; i < tin->streams->len; i++) {
251 struct ctf_stream_declaration *stream_class;
252
253 stream_class = g_ptr_array_index(tin->streams, i);
254 if (!stream_class)
255 continue;
256 /* for each file_stream */
257 for (j = 0; j < stream_class->streams->len; j++) {
258 struct ctf_stream_definition *stream;
259 struct ctf_file_stream *cfs;
260 struct ctf_stream_pos *stream_pos;
261 struct packet_index *index;
262
263 stream = g_ptr_array_index(stream_class->streams, j);
264 cfs = container_of(stream, struct ctf_file_stream,
265 parent);
266 stream_pos = &cfs->pos;
267
268 if (!stream_pos->packet_index) {
269 ret = -EINVAL;
270 goto error;
271 }
272
273 if (stream_pos->packet_index->len <= 0)
274 continue;
275
276 index = &g_array_index(stream_pos->packet_index,
277 struct packet_index,
278 stream_pos->packet_index->len - 1);
279 if (type == BT_CLOCK_REAL) {
280 if (index->ts_real.timestamp_end > end)
281 end = index->ts_real.timestamp_end;
282 } else if (type == BT_CLOCK_CYCLES) {
283 if (index->ts_cycles.timestamp_end > end)
284 end = index->ts_cycles.timestamp_end;
285 } else {
286 ret = -EINVAL;
287 goto error;
288 }
289 }
290 }
291 if (end == LLONG_MIN) {
292 ret = -ENOENT;
293 goto error;
294 }
295 *timestamp = end;
296 return 0;
297
298 error:
299 return ret;
300 }
301
302 /*
303 * Update stream current timestamp
304 */
305 static
306 void ctf_update_timestamp(struct ctf_stream_definition *stream,
307 struct definition_integer *integer_definition)
308 {
309 struct declaration_integer *integer_declaration =
310 integer_definition->declaration;
311 uint64_t oldval, newval, updateval;
312
313 if (unlikely(integer_declaration->len == 64)) {
314 stream->cycles_timestamp = integer_definition->value._unsigned;
315 stream->real_timestamp = ctf_get_real_timestamp(stream,
316 stream->cycles_timestamp);
317 return;
318 }
319 /* keep low bits */
320 oldval = stream->cycles_timestamp;
321 oldval &= (1ULL << integer_declaration->len) - 1;
322 newval = integer_definition->value._unsigned;
323 /* Test for overflow by comparing low bits */
324 if (newval < oldval)
325 newval += 1ULL << integer_declaration->len;
326 /* updateval contains old high bits, and new low bits (sum) */
327 updateval = stream->cycles_timestamp;
328 updateval &= ~((1ULL << integer_declaration->len) - 1);
329 updateval += newval;
330 stream->cycles_timestamp = updateval;
331
332 /* convert to real timestamp */
333 stream->real_timestamp = ctf_get_real_timestamp(stream,
334 stream->cycles_timestamp);
335 }
336
337 /*
338 * Print timestamp, rescaling clock frequency to nanoseconds and
339 * applying offsets as needed (unix time).
340 */
341 static
342 void ctf_print_timestamp_real(FILE *fp,
343 struct ctf_stream_definition *stream,
344 int64_t timestamp)
345 {
346 int64_t ts_sec = 0, ts_nsec;
347 uint64_t ts_sec_abs, ts_nsec_abs;
348 bool is_negative;
349
350 ts_nsec = timestamp;
351
352 /* Add command-line offset in ns */
353 ts_nsec += opt_clock_offset_ns;
354
355 /* Add command-line offset */
356 ts_sec += opt_clock_offset;
357
358 ts_sec += ts_nsec / NSEC_PER_SEC;
359 ts_nsec = ts_nsec % NSEC_PER_SEC;
360 if (ts_sec >= 0 && ts_nsec >= 0) {
361 is_negative = false;
362 ts_sec_abs = ts_sec;
363 ts_nsec_abs = ts_nsec;
364 } else if (ts_sec > 0 && ts_nsec < 0) {
365 is_negative = false;
366 ts_sec_abs = ts_sec - 1;
367 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
368 } else if (ts_sec == 0 && ts_nsec < 0) {
369 is_negative = true;
370 ts_sec_abs = ts_sec;
371 ts_nsec_abs = -ts_nsec;
372 } else if (ts_sec < 0 && ts_nsec > 0) {
373 is_negative = true;
374 ts_sec_abs = -(ts_sec + 1);
375 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
376 } else if (ts_sec < 0 && ts_nsec == 0) {
377 is_negative = true;
378 ts_sec_abs = -ts_sec;
379 ts_nsec_abs = ts_nsec;
380 } else { /* (ts_sec < 0 && ts_nsec < 0) */
381 is_negative = true;
382 ts_sec_abs = -ts_sec;
383 ts_nsec_abs = -ts_nsec;
384 }
385
386 if (!opt_clock_seconds) {
387 struct tm tm;
388 time_t time_s = (time_t) ts_sec_abs;
389
390 if (is_negative) {
391 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
392 goto seconds;
393 }
394
395 if (!opt_clock_gmt) {
396 struct tm *res;
397
398 res = localtime_r(&time_s, &tm);
399 if (!res) {
400 fprintf(stderr, "[warning] Unable to get localtime.\n");
401 goto seconds;
402 }
403 } else {
404 struct tm *res;
405
406 res = gmtime_r(&time_s, &tm);
407 if (!res) {
408 fprintf(stderr, "[warning] Unable to get gmtime.\n");
409 goto seconds;
410 }
411 }
412 if (opt_clock_date) {
413 char timestr[26];
414 size_t res;
415
416 /* Print date and time */
417 res = strftime(timestr, sizeof(timestr),
418 "%F ", &tm);
419 if (!res) {
420 fprintf(stderr, "[warning] Unable to print ascii time.\n");
421 goto seconds;
422 }
423 fprintf(fp, "%s", timestr);
424 }
425 /* Print time in HH:MM:SS.ns */
426 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
427 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
428 goto end;
429 }
430 seconds:
431 fprintf(fp, "%s%" PRId64 ".%09" PRIu64,
432 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
433
434 end:
435 return;
436 }
437
438 /*
439 * Print timestamp, in cycles
440 */
441 static
442 void ctf_print_timestamp_cycles(FILE *fp,
443 struct ctf_stream_definition *stream,
444 uint64_t timestamp)
445 {
446 fprintf(fp, "%020" PRIu64, timestamp);
447 }
448
449 void ctf_print_timestamp(FILE *fp,
450 struct ctf_stream_definition *stream,
451 int64_t timestamp)
452 {
453 if (opt_clock_cycles) {
454 ctf_print_timestamp_cycles(fp, stream, timestamp);
455 } else {
456 ctf_print_timestamp_real(fp, stream, timestamp);
457 }
458 }
459
460 static
461 void print_uuid(FILE *fp, unsigned char *uuid)
462 {
463 int i;
464
465 for (i = 0; i < BABELTRACE_UUID_LEN; i++)
466 fprintf(fp, "%x", (unsigned int) uuid[i]);
467 }
468
469 /*
470 * Discarded events can be either:
471 * - discarded after end of previous buffer due to buffer full:
472 * happened within range: [ prev_timestamp_end, timestamp_begin ]
473 * - discarded within current buffer due to either event too large or
474 * nested wrap-around:
475 * happened within range: [ timestamp_begin, timestamp_end ]
476 *
477 * Given we have discarded counters of those two types merged into the
478 * events_discarded counter, we need to use the union of those ranges:
479 * [ prev_timestamp_end, timestamp_end ]
480 *
481 * Lost packets occur if the tracer overwrote some subbuffer(s) before the
482 * consumer had time to extract them. We keep track of those gaps with the
483 * packet sequence number in each packet.
484 */
485 static
486 void ctf_print_discarded_lost(FILE *fp, struct ctf_stream_definition *stream)
487 {
488 if ((!stream->events_discarded && !stream->packets_lost) ||
489 !babeltrace_ctf_console_output) {
490 return;
491 }
492 fflush(stdout);
493 if (stream->events_discarded) {
494 fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events between [",
495 stream->events_discarded);
496 } else if (stream->packets_lost) {
497 fprintf(fp, "[warning] Tracer lost %" PRIu64 " trace packets between [",
498 stream->packets_lost);
499 }
500 if (opt_clock_cycles) {
501 ctf_print_timestamp(fp, stream,
502 stream->prev.cycles.end);
503 fprintf(fp, "] and [");
504 ctf_print_timestamp(fp, stream,
505 stream->current.cycles.end);
506 } else {
507 ctf_print_timestamp(fp, stream,
508 stream->prev.real.end);
509 fprintf(fp, "] and [");
510 ctf_print_timestamp(fp, stream,
511 stream->current.real.end);
512 }
513 fprintf(fp, "] in trace UUID ");
514 print_uuid(fp, stream->stream_class->trace->uuid);
515 if (stream->stream_class->trace->parent.path[0])
516 fprintf(fp, ", at path: \"%s\"",
517 stream->stream_class->trace->parent.path);
518
519 fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
520 if (stream->path[0])
521 fprintf(fp, ", at relative path: \"%s\"", stream->path);
522 fprintf(fp, ". ");
523 fprintf(fp, "You should consider recording a new trace with larger "
524 "buffers or with fewer events enabled.\n");
525 fflush(fp);
526 }
527
528 static
529 int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
530 {
531 struct ctf_stream_pos *pos =
532 container_of(ppos, struct ctf_stream_pos, parent);
533 struct ctf_stream_declaration *stream_class = stream->stream_class;
534 struct ctf_event_definition *event;
535 uint64_t id = 0;
536 int ret;
537
538 /* We need to check for EOF here for empty files. */
539 if (unlikely(pos->offset == EOF))
540 return EOF;
541
542 ctf_pos_get_event(pos);
543
544 /* save the current position as a restore point */
545 pos->last_offset = pos->offset;
546
547 /*
548 * This is the EOF check after we've advanced the position in
549 * ctf_pos_get_event.
550 */
551 if (unlikely(pos->offset == EOF))
552 return EOF;
553
554 /* Stream is inactive for now (live reading). */
555 if (unlikely(pos->content_size == 0))
556 return EAGAIN;
557
558 /*
559 * Packet seeked to by ctf_pos_get_event() only contains
560 * headers, no event. Consider stream as inactive (live
561 * reading).
562 */
563 if (unlikely(pos->data_offset == pos->content_size))
564 return EAGAIN;
565
566 assert(pos->offset < pos->content_size);
567
568 /* Read event header */
569 if (likely(stream->stream_event_header)) {
570 struct definition_integer *integer_definition;
571 struct bt_definition *variant;
572
573 ret = generic_rw(ppos, &stream->stream_event_header->p);
574 if (unlikely(ret))
575 goto error;
576 /* lookup event id */
577 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
578 if (integer_definition) {
579 id = integer_definition->value._unsigned;
580 } else {
581 struct definition_enum *enum_definition;
582
583 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
584 if (enum_definition) {
585 id = enum_definition->integer->value._unsigned;
586 }
587 }
588
589 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
590 if (variant) {
591 integer_definition = bt_lookup_integer(variant, "id", FALSE);
592 if (integer_definition) {
593 id = integer_definition->value._unsigned;
594 }
595 }
596 stream->event_id = id;
597
598 /* lookup timestamp */
599 stream->has_timestamp = 0;
600 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
601 if (integer_definition) {
602 ctf_update_timestamp(stream, integer_definition);
603 stream->has_timestamp = 1;
604 } else {
605 if (variant) {
606 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
607 if (integer_definition) {
608 ctf_update_timestamp(stream, integer_definition);
609 stream->has_timestamp = 1;
610 }
611 }
612 }
613 }
614
615 /* Read stream-declared event context */
616 if (stream->stream_event_context) {
617 ret = generic_rw(ppos, &stream->stream_event_context->p);
618 if (ret)
619 goto error;
620 }
621
622 if (unlikely(id >= stream_class->events_by_id->len)) {
623 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
624 return -EINVAL;
625 }
626 event = g_ptr_array_index(stream->events_by_id, id);
627 if (unlikely(!event)) {
628 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
629 return -EINVAL;
630 }
631
632 /* Read event-declared event context */
633 if (event->event_context) {
634 ret = generic_rw(ppos, &event->event_context->p);
635 if (ret)
636 goto error;
637 }
638
639 /* Read event payload */
640 if (likely(event->event_fields)) {
641 ret = generic_rw(ppos, &event->event_fields->p);
642 if (ret)
643 goto error;
644 }
645
646 if (pos->last_offset == pos->offset) {
647 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
648 return -EINVAL;
649 }
650
651 return 0;
652
653 error:
654 fprintf(stderr, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
655 return ret;
656 }
657
658 static
659 int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
660 {
661 struct ctf_stream_declaration *stream_class = stream->stream_class;
662 struct ctf_event_definition *event;
663 uint64_t id;
664 int ret;
665
666 id = stream->event_id;
667
668 /* print event header */
669 if (likely(stream->stream_event_header)) {
670 ret = generic_rw(pos, &stream->stream_event_header->p);
671 if (ret)
672 goto error;
673 }
674
675 /* print stream-declared event context */
676 if (stream->stream_event_context) {
677 ret = generic_rw(pos, &stream->stream_event_context->p);
678 if (ret)
679 goto error;
680 }
681
682 if (unlikely(id >= stream_class->events_by_id->len)) {
683 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
684 return -EINVAL;
685 }
686 event = g_ptr_array_index(stream->events_by_id, id);
687 if (unlikely(!event)) {
688 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
689 return -EINVAL;
690 }
691
692 /* print event-declared event context */
693 if (event->event_context) {
694 ret = generic_rw(pos, &event->event_context->p);
695 if (ret)
696 goto error;
697 }
698
699 /* Read and print event payload */
700 if (likely(event->event_fields)) {
701 ret = generic_rw(pos, &event->event_fields->p);
702 if (ret)
703 goto error;
704 }
705
706 return 0;
707
708 error:
709 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
710 return ret;
711 }
712
713 /*
714 * One side-effect of this function is to unmap pos mmap base if one is
715 * mapped.
716 */
717 static
718 int find_data_offset(struct ctf_stream_pos *pos,
719 struct ctf_file_stream *file_stream,
720 struct packet_index *packet_index)
721 {
722 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
723 struct stat filestats;
724 size_t filesize;
725 int ret;
726
727 pos = &file_stream->pos;
728
729 ret = fstat(pos->fd, &filestats);
730 if (ret < 0)
731 return ret;
732 filesize = filestats.st_size;
733
734 /* Deal with empty files */
735 if (!filesize) {
736 return 0;
737 }
738
739 begin:
740 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
741 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
742 }
743
744 if (pos->base_mma) {
745 /* unmap old base */
746 ret = munmap_align(pos->base_mma);
747 if (ret) {
748 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
749 strerror(errno));
750 return ret;
751 }
752 pos->base_mma = NULL;
753 }
754 /* map new base. Need mapping length from header. */
755 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
756 MAP_PRIVATE, pos->fd, pos->mmap_offset);
757 assert(pos->base_mma != MAP_FAILED);
758
759 pos->content_size = packet_map_len;
760 pos->packet_size = packet_map_len;
761 pos->offset = 0; /* Position of the packet header */
762
763 /* update trace_packet_header and stream_packet_context */
764 if (pos->prot == PROT_READ && file_stream->parent.trace_packet_header) {
765 /* Read packet header */
766 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
767 if (ret) {
768 if (ret == -EFAULT)
769 goto retry;
770 }
771 }
772 if (pos->prot == PROT_READ && file_stream->parent.stream_packet_context) {
773 /* Read packet context */
774 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
775 if (ret) {
776 if (ret == -EFAULT)
777 goto retry;
778 }
779 }
780 packet_index->data_offset = pos->offset;
781
782 /* unmap old base */
783 ret = munmap_align(pos->base_mma);
784 if (ret) {
785 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
786 strerror(errno));
787 return ret;
788 }
789 pos->base_mma = NULL;
790
791 return 0;
792
793 /* Retry with larger mapping */
794 retry:
795 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
796 /*
797 * Reached EOF, but still expecting header/context data.
798 */
799 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
800 return -EFAULT;
801 }
802 /* Double the mapping len, and retry */
803 tmp_map_len = packet_map_len << 1;
804 if (tmp_map_len >> 1 != packet_map_len) {
805 /* Overflow */
806 fprintf(stderr, "[error] Packet mapping length overflow\n");
807 return -EFAULT;
808 }
809 packet_map_len = tmp_map_len;
810 goto begin;
811 }
812
813
814 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
815 int fd, int open_flags)
816 {
817 pos->fd = fd;
818 if (fd >= 0) {
819 pos->packet_index = g_array_new(FALSE, TRUE,
820 sizeof(struct packet_index));
821 } else {
822 pos->packet_index = NULL;
823 }
824 switch (open_flags & O_ACCMODE) {
825 case O_RDONLY:
826 pos->prot = PROT_READ;
827 pos->flags = MAP_PRIVATE;
828 pos->parent.rw_table = read_dispatch_table;
829 pos->parent.event_cb = ctf_read_event;
830 pos->parent.trace = trace;
831 break;
832 case O_RDWR:
833 pos->prot = PROT_READ | PROT_WRITE;
834 pos->flags = MAP_SHARED;
835 pos->parent.rw_table = write_dispatch_table;
836 pos->parent.event_cb = ctf_write_event;
837 pos->parent.trace = trace;
838 break;
839 default:
840 assert(0);
841 }
842 return 0;
843 }
844
845 int ctf_fini_pos(struct ctf_stream_pos *pos)
846 {
847 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
848 *pos->content_size_loc = pos->offset;
849 if (pos->base_mma) {
850 int ret;
851
852 /* unmap old base */
853 ret = munmap_align(pos->base_mma);
854 if (ret) {
855 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
856 strerror(errno));
857 return -1;
858 }
859 }
860 if (pos->packet_index)
861 (void) g_array_free(pos->packet_index, TRUE);
862 return 0;
863 }
864
865 void ctf_update_current_packet_index(struct ctf_stream_definition *stream,
866 struct packet_index *prev_index,
867 struct packet_index *cur_index)
868 {
869 uint64_t events_discarded_diff;
870 uint64_t packets_lost_diff = 0;
871
872 /* Update packet index time information */
873
874 /* Current packet begin/end */
875 stream->current.real.begin =
876 cur_index->ts_real.timestamp_begin;
877 stream->current.cycles.begin =
878 cur_index->ts_cycles.timestamp_begin;
879 stream->current.real.end =
880 cur_index->ts_real.timestamp_end;
881 stream->current.cycles.end =
882 cur_index->ts_cycles.timestamp_end;
883
884 /* Update packet index discarded event information */
885 events_discarded_diff = cur_index->events_discarded;
886 if (prev_index) {
887 /* Previous packet begin/end */
888 stream->prev.cycles.begin =
889 prev_index->ts_cycles.timestamp_begin;
890 stream->prev.real.begin =
891 prev_index->ts_real.timestamp_begin;
892 stream->prev.cycles.end =
893 prev_index->ts_cycles.timestamp_end;
894 stream->prev.real.end =
895 prev_index->ts_real.timestamp_end;
896
897 events_discarded_diff -= prev_index->events_discarded;
898 /* packet_seq_num stays at 0 if not produced by the tracer */
899 if (cur_index->packet_seq_num) {
900 packets_lost_diff = cur_index->packet_seq_num -
901 prev_index->packet_seq_num - 1;
902 }
903 /*
904 * Deal with 32-bit wrap-around if the tracer provided a
905 * 32-bit field.
906 */
907 if (prev_index->events_discarded_len == 32) {
908 events_discarded_diff = (uint32_t) events_discarded_diff;
909 }
910 } else {
911 /*
912 * First packet: use current packet info as limits for
913 * previous packet.
914 */
915 stream->prev.cycles.begin =
916 stream->prev.cycles.end =
917 stream->current.cycles.begin;
918 stream->prev.real.begin =
919 stream->prev.real.end =
920 stream->current.real.begin;
921 }
922 stream->events_discarded = events_discarded_diff;
923 stream->packets_lost = packets_lost_diff;
924 }
925
926 /*
927 * Find the timerange where all the streams in the trace are active
928 * simultaneously.
929 *
930 * Return 0 and update begin/end if necessary on success, return 1 for
931 * empty streams and return a negative value on error.
932 */
933 static
934 int ctf_intersect_trace(struct bt_trace_descriptor *td_read,
935 uint64_t *begin, uint64_t *end)
936 {
937 struct ctf_trace *tin;
938 int stream_id, ret = 0;
939
940 tin = container_of(td_read, struct ctf_trace, parent);
941
942 for (stream_id = 0; stream_id < tin->streams->len;
943 stream_id++) {
944 int filenr;
945 struct ctf_stream_declaration *stream_class;
946
947 stream_class = g_ptr_array_index(tin->streams, stream_id);
948 if (!stream_class) {
949 continue;
950 }
951 for (filenr = 0; filenr < stream_class->streams->len; filenr++) {
952 struct ctf_file_stream *file_stream;
953 struct ctf_stream_pos *stream_pos;
954 struct packet_index *index;
955
956 file_stream = g_ptr_array_index(stream_class->streams,
957 filenr);
958 if (!file_stream) {
959 continue;
960 }
961 stream_pos = &file_stream->pos;
962 if (!stream_pos->packet_index ||
963 stream_pos->packet_index->len <= 0) {
964 ret = 1;
965 goto end;
966 }
967 index = &g_array_index(stream_pos->packet_index,
968 struct packet_index, 0);
969 if (index->ts_real.timestamp_begin > *begin) {
970 *begin = index->ts_real.timestamp_begin;
971 }
972 index = &g_array_index(stream_pos->packet_index,
973 struct packet_index,
974 stream_pos->packet_index->len - 1);
975 if (index->ts_real.timestamp_end < *end) {
976 *end = index->ts_real.timestamp_end;
977 }
978 }
979 }
980
981 end:
982 return ret;
983 }
984
985 /*
986 * Find the timerange where all streams in the trace collection are active
987 * simultaneously.
988 *
989 * Return 0 on success.
990 * Return 1 if no intersections are found.
991 * Return a negative value on error.
992 */
993 int ctf_find_packets_intersection(struct bt_context *ctx,
994 uint64_t *ts_begin, uint64_t *ts_end)
995 {
996 int ret, i;
997
998 if (!ctx || !ctx->tc || !ctx->tc->array) {
999 ret = -EINVAL;
1000 goto end;
1001 }
1002
1003 for (i = 0; i < ctx->tc->array->len; i++) {
1004 struct bt_trace_descriptor *td_read;
1005
1006 td_read = g_ptr_array_index(ctx->tc->array, i);
1007 if (!td_read) {
1008 continue;
1009 }
1010 ret = ctf_intersect_trace(td_read, ts_begin, ts_end);
1011 if (ret) {
1012 goto end;
1013 }
1014 }
1015 if (*ts_end < *ts_begin) {
1016 ret = 1;
1017 } else {
1018 ret = 0;
1019 }
1020 end:
1021 return ret;
1022 }
1023
1024 /*
1025 * for SEEK_CUR: go to next packet.
1026 * for SEEK_SET: go to packet numer (index).
1027 */
1028 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
1029 {
1030 struct ctf_stream_pos *pos =
1031 container_of(stream_pos, struct ctf_stream_pos, parent);
1032 struct ctf_file_stream *file_stream =
1033 container_of(pos, struct ctf_file_stream, pos);
1034 int ret;
1035 struct packet_index *packet_index, *prev_index;
1036
1037 switch (whence) {
1038 case SEEK_CUR:
1039 case SEEK_SET: /* Fall-through */
1040 break; /* OK */
1041 default:
1042 assert(0);
1043 }
1044
1045 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
1046 *pos->content_size_loc = pos->offset;
1047
1048 if (pos->base_mma) {
1049 /* unmap old base */
1050 ret = munmap_align(pos->base_mma);
1051 if (ret) {
1052 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1053 strerror(errno));
1054 assert(0);
1055 }
1056 pos->base_mma = NULL;
1057 }
1058
1059 /*
1060 * The caller should never ask for ctf_move_pos across packets,
1061 * except to get exactly at the beginning of the next packet.
1062 */
1063 if (pos->prot & PROT_WRITE) {
1064 switch (whence) {
1065 case SEEK_CUR:
1066 /* The writer will add padding */
1067 pos->mmap_offset += pos->packet_size / CHAR_BIT;
1068 break;
1069 case SEEK_SET:
1070 assert(index == 0); /* only seek supported for now */
1071 pos->cur_index = 0;
1072 break;
1073 default:
1074 assert(0);
1075 }
1076 pos->content_size = -1U; /* Unknown at this point */
1077 pos->packet_size = WRITE_PACKET_LEN;
1078 do {
1079 ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
1080 pos->packet_size / CHAR_BIT);
1081 } while (ret == EINTR);
1082 assert(ret == 0);
1083 pos->offset = 0;
1084 } else {
1085 read_next_packet:
1086 switch (whence) {
1087 case SEEK_CUR:
1088 {
1089 if (pos->offset == EOF) {
1090 return;
1091 }
1092 assert(pos->cur_index < pos->packet_index->len);
1093 /* The reader will expect us to skip padding */
1094 ++pos->cur_index;
1095 break;
1096 }
1097 case SEEK_SET:
1098 if (index >= pos->packet_index->len) {
1099 pos->offset = EOF;
1100 return;
1101 }
1102 pos->cur_index = index;
1103 break;
1104 default:
1105 assert(0);
1106 }
1107
1108 if (pos->cur_index >= pos->packet_index->len) {
1109 pos->offset = EOF;
1110 return;
1111 }
1112
1113 packet_index = &g_array_index(pos->packet_index,
1114 struct packet_index, pos->cur_index);
1115 if (pos->cur_index > 0) {
1116 prev_index = &g_array_index(pos->packet_index,
1117 struct packet_index,
1118 pos->cur_index - 1);
1119 } else {
1120 prev_index = NULL;
1121 }
1122 ctf_update_current_packet_index(&file_stream->parent,
1123 prev_index, packet_index);
1124
1125 /*
1126 * We need to check if we are in trace read or called
1127 * from packet indexing. In this last case, the
1128 * collection is not there, so we cannot print the
1129 * timestamps.
1130 */
1131 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
1132 ctf_print_discarded_lost(stderr, &file_stream->parent);
1133 }
1134
1135 packet_index = &g_array_index(pos->packet_index,
1136 struct packet_index,
1137 pos->cur_index);
1138 file_stream->parent.cycles_timestamp = packet_index->ts_cycles.timestamp_begin;
1139
1140 file_stream->parent.real_timestamp = packet_index->ts_real.timestamp_begin;
1141
1142 /* Lookup context/packet size in index */
1143 if (packet_index->data_offset == -1) {
1144 ret = find_data_offset(pos, file_stream, packet_index);
1145 if (ret < 0) {
1146 return;
1147 }
1148 }
1149 pos->content_size = packet_index->content_size;
1150 pos->packet_size = packet_index->packet_size;
1151 pos->mmap_offset = packet_index->offset;
1152 pos->data_offset = packet_index->data_offset;
1153 if (pos->data_offset < packet_index->content_size) {
1154 pos->offset = 0; /* will read headers */
1155 } else if (pos->data_offset == packet_index->content_size) {
1156 /* empty packet */
1157 pos->offset = packet_index->data_offset;
1158 whence = SEEK_CUR;
1159 goto read_next_packet;
1160 } else {
1161 pos->offset = EOF;
1162 return;
1163 }
1164 }
1165 /* map new base. Need mapping length from header. */
1166 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1167 pos->flags, pos->fd, pos->mmap_offset);
1168 if (pos->base_mma == MAP_FAILED) {
1169 fprintf(stderr, "[error] mmap error %s.\n",
1170 strerror(errno));
1171 assert(0);
1172 }
1173
1174 /* update trace_packet_header and stream_packet_context */
1175 if (!(pos->prot & PROT_WRITE) &&
1176 file_stream->parent.trace_packet_header) {
1177 /* Read packet header */
1178 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1179 assert(!ret);
1180 }
1181 if (!(pos->prot & PROT_WRITE) &&
1182 file_stream->parent.stream_packet_context) {
1183 /* Read packet context */
1184 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1185 assert(!ret);
1186 }
1187 }
1188
1189 static
1190 int packet_metadata(struct ctf_trace *td, FILE *fp)
1191 {
1192 uint32_t magic;
1193 size_t len;
1194 int ret = 0;
1195
1196 len = fread(&magic, sizeof(magic), 1, fp);
1197 if (len != 1) {
1198 goto end;
1199 }
1200 if (magic == TSDL_MAGIC) {
1201 ret = 1;
1202 td->byte_order = BYTE_ORDER;
1203 CTF_TRACE_SET_FIELD(td, byte_order);
1204 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1205 ret = 1;
1206 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1207 LITTLE_ENDIAN : BIG_ENDIAN;
1208 CTF_TRACE_SET_FIELD(td, byte_order);
1209 }
1210 end:
1211 rewind(fp);
1212 return ret;
1213 }
1214
1215 /*
1216 * Returns 0 on success, -1 on error.
1217 */
1218 static
1219 int check_version(unsigned int major, unsigned int minor)
1220 {
1221 switch (major) {
1222 case 1:
1223 switch (minor) {
1224 case 8:
1225 return 0;
1226 default:
1227 goto warning;
1228 }
1229 default:
1230 goto warning;
1231
1232 }
1233
1234 /* eventually return an error instead of warning */
1235 warning:
1236 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1237 major, minor);
1238 return 0;
1239 }
1240
1241 static
1242 int ctf_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
1243 FILE *out)
1244 {
1245 struct metadata_packet_header header;
1246 size_t readlen, writelen, toread;
1247 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
1248 int ret = 0;
1249
1250 readlen = fread(&header, header_sizeof(header), 1, in);
1251 if (readlen < 1)
1252 return -EINVAL;
1253
1254 if (td->byte_order != BYTE_ORDER) {
1255 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1256 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1257 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1258 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1259 }
1260 if (header.checksum)
1261 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
1262 if (header.compression_scheme) {
1263 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
1264 header.compression_scheme);
1265 return -EINVAL;
1266 }
1267 if (header.encryption_scheme) {
1268 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
1269 header.encryption_scheme);
1270 return -EINVAL;
1271 }
1272 if (header.checksum_scheme) {
1273 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
1274 header.checksum_scheme);
1275 return -EINVAL;
1276 }
1277 if (check_version(header.major, header.minor) < 0)
1278 return -EINVAL;
1279 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1280 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1281 CTF_TRACE_SET_FIELD(td, uuid);
1282 } else {
1283 if (bt_uuid_compare(header.uuid, td->uuid))
1284 return -EINVAL;
1285 }
1286
1287 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1288 return -EINVAL;
1289
1290 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
1291
1292 for (;;) {
1293 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
1294 if (ferror(in)) {
1295 ret = -EINVAL;
1296 break;
1297 }
1298 if (babeltrace_debug) {
1299 buf[readlen] = '\0';
1300 fprintf(stderr, "[debug] metadata packet read: %s\n",
1301 buf);
1302 }
1303
1304 writelen = fwrite(buf, sizeof(char), readlen, out);
1305 if (writelen < readlen) {
1306 ret = -EIO;
1307 break;
1308 }
1309 if (ferror(out)) {
1310 ret = -EINVAL;
1311 break;
1312 }
1313 toread -= readlen;
1314 if (!toread) {
1315 ret = 0; /* continue reading next packet */
1316 goto read_padding;
1317 }
1318 }
1319 return ret;
1320
1321 read_padding:
1322 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1323 ret = fseek(in, toread, SEEK_CUR);
1324 if (ret < 0) {
1325 fprintf(stderr, "[warning] Missing padding at end of file\n");
1326 ret = 0;
1327 }
1328 return ret;
1329 }
1330
1331 static
1332 int ctf_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1333 char **buf)
1334 {
1335 FILE *in, *out;
1336 size_t size, buflen;
1337 int ret;
1338
1339 in = *fp;
1340 /*
1341 * Using strlen on *buf instead of size of open_memstream
1342 * because its size includes garbage at the end (after final
1343 * \0). This is the allocated size, not the actual string size.
1344 */
1345 out = babeltrace_open_memstream(buf, &size);
1346 if (out == NULL) {
1347 perror("Metadata open_memstream");
1348 return -errno;
1349 }
1350 for (;;) {
1351 ret = ctf_trace_metadata_packet_read(td, in, out);
1352 if (ret) {
1353 break;
1354 }
1355 if (feof(in)) {
1356 ret = 0;
1357 break;
1358 }
1359 }
1360 /* close to flush the buffer */
1361 ret = babeltrace_close_memstream(buf, &size, out);
1362 if (ret < 0) {
1363 int closeret;
1364
1365 perror("babeltrace_flush_memstream");
1366 ret = -errno;
1367 closeret = fclose(in);
1368 if (closeret) {
1369 perror("Error in fclose");
1370 }
1371 return ret;
1372 }
1373 ret = fclose(in);
1374 if (ret) {
1375 perror("Error in fclose");
1376 }
1377 /* open for reading */
1378 buflen = strlen(*buf);
1379 if (!buflen) {
1380 *fp = NULL;
1381 return -ENOENT;
1382 }
1383 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1384 if (!*fp) {
1385 perror("Metadata fmemopen");
1386 return -errno;
1387 }
1388 return 0;
1389 }
1390
1391 static
1392 int ctf_trace_metadata_read(struct ctf_trace *td, FILE *metadata_fp,
1393 struct ctf_scanner *scanner, int append)
1394 {
1395 struct ctf_file_stream *metadata_stream;
1396 FILE *fp;
1397 char *buf = NULL;
1398 int ret = 0, closeret;
1399
1400 metadata_stream = g_new0(struct ctf_file_stream, 1);
1401 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1402
1403 if (metadata_fp) {
1404 fp = metadata_fp;
1405 metadata_stream->pos.fd = -1;
1406 } else {
1407 td->metadata = &metadata_stream->parent;
1408 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1409 if (metadata_stream->pos.fd < 0) {
1410 fprintf(stderr, "Unable to open metadata.\n");
1411 ret = -1;
1412 goto end_free;
1413 }
1414
1415 fp = fdopen(metadata_stream->pos.fd, "r");
1416 if (!fp) {
1417 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1418 perror("Metadata stream open");
1419 ret = -errno;
1420 goto end_stream;
1421 }
1422 /* fd now belongs to fp */
1423 metadata_stream->pos.fd = -1;
1424 }
1425 if (babeltrace_debug)
1426 yydebug = 1;
1427
1428 if (packet_metadata(td, fp)) {
1429 ret = ctf_trace_metadata_stream_read(td, &fp, &buf);
1430 if (ret) {
1431 goto end;
1432 }
1433 td->metadata_string = buf;
1434 td->metadata_packetized = 1;
1435 } else {
1436 if (!append) {
1437 unsigned int major, minor;
1438 ssize_t nr_items;
1439
1440 td->byte_order = BYTE_ORDER;
1441
1442 /* Check text-only metadata header and version */
1443 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
1444 if (nr_items < 2)
1445 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1446 if (check_version(major, minor) < 0) {
1447 ret = -EINVAL;
1448 goto end;
1449 }
1450 rewind(fp);
1451 }
1452 }
1453
1454 ret = ctf_scanner_append_ast(scanner, fp);
1455 if (ret) {
1456 fprintf(stderr, "[error] Error creating AST\n");
1457 goto end;
1458 }
1459
1460 if (babeltrace_debug) {
1461 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1462 if (ret) {
1463 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1464 goto end;
1465 }
1466 }
1467
1468 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1469 if (ret) {
1470 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1471 goto end;
1472 }
1473 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1474 td, td->byte_order);
1475 if (ret) {
1476 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1477 goto end;
1478 }
1479 end:
1480 if (fp) {
1481 closeret = fclose(fp);
1482 if (closeret) {
1483 perror("Error on fclose");
1484 }
1485 }
1486 end_stream:
1487 if (metadata_stream->pos.fd >= 0) {
1488 closeret = close(metadata_stream->pos.fd);
1489 if (closeret) {
1490 perror("Error on metadata stream fd close");
1491 }
1492 }
1493 end_free:
1494 if (ret)
1495 g_free(metadata_stream);
1496 return ret;
1497 }
1498
1499 static
1500 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1501 struct ctf_stream_definition *stream,
1502 struct ctf_event_declaration *event)
1503 {
1504 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1505
1506 if (event->context_decl) {
1507 struct bt_definition *definition =
1508 event->context_decl->p.definition_new(&event->context_decl->p,
1509 stream->parent_def_scope, 0, 0, "event.context");
1510 if (!definition) {
1511 goto error;
1512 }
1513 stream_event->event_context = container_of(definition,
1514 struct definition_struct, p);
1515 stream->parent_def_scope = stream_event->event_context->p.scope;
1516 }
1517 if (event->fields_decl) {
1518 struct bt_definition *definition =
1519 event->fields_decl->p.definition_new(&event->fields_decl->p,
1520 stream->parent_def_scope, 0, 0, "event.fields");
1521 if (!definition) {
1522 goto error;
1523 }
1524 stream_event->event_fields = container_of(definition,
1525 struct definition_struct, p);
1526 stream->parent_def_scope = stream_event->event_fields->p.scope;
1527 }
1528 stream_event->stream = stream;
1529 return stream_event;
1530
1531 error:
1532 if (stream_event->event_fields)
1533 bt_definition_unref(&stream_event->event_fields->p);
1534 if (stream_event->event_context)
1535 bt_definition_unref(&stream_event->event_context->p);
1536 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1537 g_quark_to_string(event->name));
1538 return NULL;
1539 }
1540
1541 static
1542 int copy_event_declarations_stream_class_to_stream(struct ctf_trace *td,
1543 struct ctf_stream_declaration *stream_class,
1544 struct ctf_stream_definition *stream)
1545 {
1546 size_t def_size, class_size, i;
1547 int ret = 0;
1548
1549 def_size = stream->events_by_id->len;
1550 class_size = stream_class->events_by_id->len;
1551
1552 g_ptr_array_set_size(stream->events_by_id, class_size);
1553 for (i = def_size; i < class_size; i++) {
1554 struct ctf_event_declaration *event =
1555 g_ptr_array_index(stream_class->events_by_id, i);
1556 struct ctf_event_definition *stream_event;
1557
1558 if (!event)
1559 continue;
1560 stream_event = create_event_definitions(td, stream, event);
1561 if (!stream_event) {
1562 ret = -EINVAL;
1563 goto error;
1564 }
1565 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1566 }
1567 error:
1568 return ret;
1569 }
1570
1571 static
1572 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1573 {
1574 struct ctf_stream_declaration *stream_class;
1575 int ret;
1576 int i;
1577
1578 if (stream->stream_definitions_created)
1579 return 0;
1580
1581 stream_class = stream->stream_class;
1582
1583 if (stream_class->packet_context_decl) {
1584 struct bt_definition *definition =
1585 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1586 stream->parent_def_scope, 0, 0, "stream.packet.context");
1587 if (!definition) {
1588 ret = -EINVAL;
1589 goto error;
1590 }
1591 stream->stream_packet_context = container_of(definition,
1592 struct definition_struct, p);
1593 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1594 }
1595 if (stream_class->event_header_decl) {
1596 struct bt_definition *definition =
1597 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1598 stream->parent_def_scope, 0, 0, "stream.event.header");
1599 if (!definition) {
1600 ret = -EINVAL;
1601 goto error;
1602 }
1603 stream->stream_event_header =
1604 container_of(definition, struct definition_struct, p);
1605 stream->parent_def_scope = stream->stream_event_header->p.scope;
1606 }
1607 if (stream_class->event_context_decl) {
1608 struct bt_definition *definition =
1609 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1610 stream->parent_def_scope, 0, 0, "stream.event.context");
1611 if (!definition) {
1612 ret = -EINVAL;
1613 goto error;
1614 }
1615 stream->stream_event_context =
1616 container_of(definition, struct definition_struct, p);
1617 stream->parent_def_scope = stream->stream_event_context->p.scope;
1618 }
1619 stream->events_by_id = g_ptr_array_new();
1620 ret = copy_event_declarations_stream_class_to_stream(td,
1621 stream_class, stream);
1622 if (ret)
1623 goto error_event;
1624 return 0;
1625
1626 error_event:
1627 for (i = 0; i < stream->events_by_id->len; i++) {
1628 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1629 if (stream_event)
1630 g_free(stream_event);
1631 }
1632 g_ptr_array_free(stream->events_by_id, TRUE);
1633 error:
1634 if (stream->stream_event_context)
1635 bt_definition_unref(&stream->stream_event_context->p);
1636 if (stream->stream_event_header)
1637 bt_definition_unref(&stream->stream_event_header->p);
1638 if (stream->stream_packet_context)
1639 bt_definition_unref(&stream->stream_packet_context->p);
1640 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1641 stream_class->stream_id, strerror(-ret));
1642 return ret;
1643 }
1644
1645 static
1646 int stream_assign_class(struct ctf_trace *td,
1647 struct ctf_file_stream *file_stream,
1648 uint64_t stream_id)
1649 {
1650 struct ctf_stream_declaration *stream;
1651 int ret;
1652
1653 file_stream->parent.stream_id = stream_id;
1654 if (stream_id >= td->streams->len) {
1655 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1656 return -EINVAL;
1657 }
1658 stream = g_ptr_array_index(td->streams, stream_id);
1659 if (!stream) {
1660 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1661 return -EINVAL;
1662 }
1663 file_stream->parent.stream_class = stream;
1664 ret = create_stream_definitions(td, &file_stream->parent);
1665 if (ret)
1666 return ret;
1667 return 0;
1668 }
1669
1670 static
1671 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1672 struct ctf_trace *td,
1673 struct ctf_file_stream *file_stream,
1674 size_t filesize)
1675 {
1676 struct packet_index packet_index;
1677 uint64_t stream_id = 0;
1678 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1679 int first_packet = 0;
1680 int len_index;
1681 int ret;
1682
1683 begin:
1684 memset(&packet_index, 0, sizeof(packet_index));
1685 if (!pos->mmap_offset) {
1686 first_packet = 1;
1687 }
1688
1689 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1690 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1691 }
1692
1693 if (pos->base_mma) {
1694 /* unmap old base */
1695 ret = munmap_align(pos->base_mma);
1696 if (ret) {
1697 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1698 strerror(errno));
1699 return ret;
1700 }
1701 pos->base_mma = NULL;
1702 }
1703 /* map new base. Need mapping length from header. */
1704 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1705 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1706 assert(pos->base_mma != MAP_FAILED);
1707 /*
1708 * Use current mapping size as temporary content and packet
1709 * size.
1710 */
1711 pos->content_size = packet_map_len;
1712 pos->packet_size = packet_map_len;
1713 pos->offset = 0; /* Position of the packet header */
1714
1715 packet_index.offset = pos->mmap_offset;
1716
1717 /* read and check header, set stream id (and check) */
1718 if (file_stream->parent.trace_packet_header) {
1719 /* Read packet header */
1720 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1721 if (ret) {
1722 if (ret == -EFAULT)
1723 goto retry;
1724 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1725 return ret;
1726 }
1727 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1728 if (len_index >= 0) {
1729 struct bt_definition *field;
1730 uint64_t magic;
1731
1732 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1733 magic = bt_get_unsigned_int(field);
1734 if (magic != CTF_MAGIC) {
1735 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1736 magic,
1737 file_stream->pos.packet_index->len,
1738 (ssize_t) pos->mmap_offset);
1739 return -EINVAL;
1740 }
1741 }
1742
1743 /* check uuid */
1744 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1745 if (len_index >= 0) {
1746 struct definition_array *defarray;
1747 struct bt_definition *field;
1748 uint64_t i;
1749 uint8_t uuidval[BABELTRACE_UUID_LEN];
1750
1751 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1752 assert(field->declaration->id == BT_CTF_TYPE_ID_ARRAY);
1753 defarray = container_of(field, struct definition_array, p);
1754 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1755
1756 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1757 struct bt_definition *elem;
1758
1759 elem = bt_array_index(defarray, i);
1760 uuidval[i] = bt_get_unsigned_int(elem);
1761 }
1762 ret = bt_uuid_compare(td->uuid, uuidval);
1763 if (ret) {
1764 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1765 return -EINVAL;
1766 }
1767 }
1768
1769 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1770 if (len_index >= 0) {
1771 struct bt_definition *field;
1772
1773 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1774 stream_id = bt_get_unsigned_int(field);
1775 }
1776 }
1777
1778 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1779 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1780 stream_id,
1781 file_stream->parent.stream_id);
1782 return -EINVAL;
1783 }
1784 if (first_packet) {
1785 ret = stream_assign_class(td, file_stream, stream_id);
1786 if (ret)
1787 return ret;
1788 }
1789
1790 if (file_stream->parent.stream_packet_context) {
1791 /* Read packet context */
1792 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1793 if (ret) {
1794 if (ret == -EFAULT)
1795 goto retry;
1796 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1797 return ret;
1798 }
1799 /* read packet size from header */
1800 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1801 if (len_index >= 0) {
1802 struct bt_definition *field;
1803
1804 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1805 packet_index.packet_size = bt_get_unsigned_int(field);
1806 } else {
1807 /* Use file size for packet size */
1808 packet_index.packet_size = filesize * CHAR_BIT;
1809 }
1810
1811 /* read content size from header */
1812 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1813 if (len_index >= 0) {
1814 struct bt_definition *field;
1815
1816 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1817 packet_index.content_size = bt_get_unsigned_int(field);
1818 } else {
1819 /* Use packet size if non-zero, else file size */
1820 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1821 }
1822
1823 /* read timestamp begin from header */
1824 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1825 if (len_index >= 0) {
1826 struct bt_definition *field;
1827
1828 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1829 packet_index.ts_cycles.timestamp_begin = bt_get_unsigned_int(field);
1830 if (file_stream->parent.stream_class->trace->parent.collection) {
1831 packet_index.ts_real.timestamp_begin =
1832 ctf_get_real_timestamp(
1833 &file_stream->parent,
1834 packet_index.ts_cycles.timestamp_begin);
1835 }
1836 }
1837
1838 /* read timestamp end from header */
1839 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1840 if (len_index >= 0) {
1841 struct bt_definition *field;
1842
1843 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1844 packet_index.ts_cycles.timestamp_end = bt_get_unsigned_int(field);
1845 if (file_stream->parent.stream_class->trace->parent.collection) {
1846 packet_index.ts_real.timestamp_end =
1847 ctf_get_real_timestamp(
1848 &file_stream->parent,
1849 packet_index.ts_cycles.timestamp_end);
1850 }
1851 }
1852
1853 /* read events discarded from header */
1854 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1855 if (len_index >= 0) {
1856 struct bt_definition *field;
1857
1858 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1859 packet_index.events_discarded = bt_get_unsigned_int(field);
1860 packet_index.events_discarded_len = bt_get_int_len(field);
1861 }
1862
1863 /* read packet_seq_num from header */
1864 len_index = bt_struct_declaration_lookup_field_index(
1865 file_stream->parent.stream_packet_context->declaration,
1866 g_quark_from_static_string("packet_seq_num"));
1867 if (len_index >= 0) {
1868 struct bt_definition *field;
1869
1870 field = bt_struct_definition_get_field_from_index(
1871 file_stream->parent.stream_packet_context,
1872 len_index);
1873 packet_index.packet_seq_num = bt_get_unsigned_int(field);
1874 }
1875 } else {
1876 /* Use file size for packet size */
1877 packet_index.packet_size = filesize * CHAR_BIT;
1878 /* Use packet size if non-zero, else file size */
1879 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1880 }
1881
1882 /* Validate content size and packet size values */
1883 if (packet_index.content_size > packet_index.packet_size) {
1884 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1885 packet_index.content_size, packet_index.packet_size);
1886 return -EINVAL;
1887 }
1888
1889 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1890 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1891 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1892 return -EINVAL;
1893 }
1894
1895 if (packet_index.content_size < pos->offset) {
1896 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1897 return -EINVAL;
1898 }
1899
1900 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1901 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1902 return -EINVAL;
1903 }
1904
1905 /* Save position after header and context */
1906 packet_index.data_offset = pos->offset;
1907
1908 /* add index to packet array */
1909 g_array_append_val(file_stream->pos.packet_index, packet_index);
1910
1911 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1912
1913 return 0;
1914
1915 /* Retry with larger mapping */
1916 retry:
1917 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1918 /*
1919 * Reached EOF, but still expecting header/context data.
1920 */
1921 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1922 return -EFAULT;
1923 }
1924 /* Double the mapping len, and retry */
1925 tmp_map_len = packet_map_len << 1;
1926 if (tmp_map_len >> 1 != packet_map_len) {
1927 /* Overflow */
1928 fprintf(stderr, "[error] Packet mapping length overflow\n");
1929 return -EFAULT;
1930 }
1931 packet_map_len = tmp_map_len;
1932 goto begin;
1933 }
1934
1935 static
1936 int create_stream_packet_index(struct ctf_trace *td,
1937 struct ctf_file_stream *file_stream)
1938 {
1939 struct ctf_stream_pos *pos;
1940 struct stat filestats;
1941 int ret;
1942
1943 pos = &file_stream->pos;
1944
1945 ret = fstat(pos->fd, &filestats);
1946 if (ret < 0)
1947 return ret;
1948
1949 /* Deal with empty files */
1950 if (!filestats.st_size) {
1951 if (file_stream->parent.trace_packet_header
1952 || file_stream->parent.stream_packet_context) {
1953 /*
1954 * We expect a trace packet header and/or stream packet
1955 * context. Since a trace needs to have at least one
1956 * packet, empty files are therefore not accepted.
1957 */
1958 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1959 return -EINVAL;
1960 } else {
1961 /*
1962 * Without trace packet header nor stream packet
1963 * context, a one-packet trace can indeed be empty. This
1964 * is only valid if there is only one stream class: 0.
1965 */
1966 ret = stream_assign_class(td, file_stream, 0);
1967 if (ret)
1968 return ret;
1969 return 0;
1970 }
1971 }
1972
1973 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1974 ret = create_stream_one_packet_index(pos, td, file_stream,
1975 filestats.st_size);
1976 if (ret)
1977 return ret;
1978 }
1979 return 0;
1980 }
1981
1982 static
1983 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1984 {
1985 int ret;
1986
1987 if (td->packet_header_decl) {
1988 struct bt_definition *definition =
1989 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1990 stream->parent_def_scope, 0, 0, "trace.packet.header");
1991 if (!definition) {
1992 ret = -EINVAL;
1993 goto error;
1994 }
1995 stream->trace_packet_header =
1996 container_of(definition, struct definition_struct, p);
1997 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1998 }
1999
2000 return 0;
2001
2002 error:
2003 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
2004 return ret;
2005 }
2006
2007 static
2008 int import_stream_packet_index(struct ctf_trace *td,
2009 struct ctf_file_stream *file_stream)
2010 {
2011 struct ctf_stream_pos *pos;
2012 struct ctf_packet_index *ctf_index = NULL;
2013 struct ctf_packet_index_file_hdr index_hdr;
2014 struct packet_index index;
2015 uint32_t packet_index_len, index_minor;
2016 int ret = 0;
2017 int first_packet = 1;
2018 size_t len;
2019
2020 pos = &file_stream->pos;
2021
2022 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
2023 if (len != 1) {
2024 perror("read index file header");
2025 goto error;
2026 }
2027
2028 /* Check the index header */
2029 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
2030 fprintf(stderr, "[error] wrong index magic\n");
2031 ret = -1;
2032 goto error;
2033 }
2034 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
2035 fprintf(stderr, "[error] Incompatible index file %" PRIu32
2036 ".%" PRIu32 ", supported %d.%d\n",
2037 be32toh(index_hdr.index_major),
2038 be32toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
2039 CTF_INDEX_MINOR);
2040 ret = -1;
2041 goto error;
2042 }
2043 index_minor = be32toh(index_hdr.index_minor);
2044
2045 packet_index_len = be32toh(index_hdr.packet_index_len);
2046 if (packet_index_len == 0) {
2047 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
2048 ret = -1;
2049 goto error;
2050 }
2051 /*
2052 * Allocate the index length found in header, not internal
2053 * representation.
2054 */
2055 ctf_index = g_malloc0(packet_index_len);
2056 while (fread(ctf_index, packet_index_len, 1,
2057 pos->index_fp) == 1) {
2058 uint64_t stream_id;
2059 struct ctf_stream_declaration *stream = NULL;
2060
2061 memset(&index, 0, sizeof(index));
2062 index.offset = be64toh(ctf_index->offset);
2063 index.packet_size = be64toh(ctf_index->packet_size);
2064 index.content_size = be64toh(ctf_index->content_size);
2065 index.ts_cycles.timestamp_begin = be64toh(ctf_index->timestamp_begin);
2066 index.ts_cycles.timestamp_end = be64toh(ctf_index->timestamp_end);
2067 index.events_discarded = be64toh(ctf_index->events_discarded);
2068 index.events_discarded_len = 64;
2069 index.data_offset = -1;
2070 stream_id = be64toh(ctf_index->stream_id);
2071 if (index_minor >= 1) {
2072 index.stream_instance_id = be64toh(ctf_index->stream_instance_id);
2073 index.packet_seq_num = be64toh(ctf_index->packet_seq_num);
2074 }
2075
2076 if (!first_packet) {
2077 /* add index to packet array */
2078 g_array_append_val(file_stream->pos.packet_index, index);
2079 continue;
2080 }
2081
2082 file_stream->parent.stream_id = stream_id;
2083 if (stream_id < td->streams->len) {
2084 stream = g_ptr_array_index(td->streams, stream_id);
2085 }
2086 if (!stream) {
2087 fprintf(stderr, "[error] Stream %" PRIu64
2088 " is not declared in metadata.\n",
2089 stream_id);
2090 ret = -EINVAL;
2091 goto error;
2092 }
2093 file_stream->parent.stream_class = stream;
2094 ret = create_stream_definitions(td, &file_stream->parent);
2095 if (ret)
2096 goto error;
2097 first_packet = 0;
2098 /* add index to packet array */
2099 g_array_append_val(file_stream->pos.packet_index, index);
2100 }
2101
2102 /* Index containing only the header. */
2103 if (!file_stream->parent.stream_class) {
2104 ret = -1;
2105 goto error;
2106 }
2107
2108 ret = 0;
2109
2110 error:
2111 g_free(ctf_index);
2112 return ret;
2113 }
2114
2115 /*
2116 * Note: many file streams can inherit from the same stream class
2117 * description (metadata).
2118 */
2119 static
2120 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
2121 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2122 int whence))
2123 {
2124 int ret, fd, closeret;
2125 struct ctf_file_stream *file_stream;
2126 struct stat statbuf;
2127 char *index_name;
2128
2129 fd = openat(td->dirfd, path, flags);
2130 if (fd < 0) {
2131 perror("File stream openat()");
2132 ret = fd;
2133 goto error;
2134 }
2135
2136 /* Don't try to mmap subdirectories. Skip them, return success. */
2137 ret = fstat(fd, &statbuf);
2138 if (ret) {
2139 perror("File stream fstat()");
2140 goto fstat_error;
2141 }
2142 if (S_ISDIR(statbuf.st_mode)) {
2143 if (strncmp(path, "index", 5) != 0) {
2144 fprintf(stderr, "[warning] Skipping directory '%s' "
2145 "found in trace\n", path);
2146 }
2147 ret = 0;
2148 goto fd_is_dir_ok;
2149 }
2150 if (!statbuf.st_size) {
2151 /** Skip empty files. */
2152 ret = 0;
2153 goto fd_is_empty_file;
2154 }
2155
2156 file_stream = g_new0(struct ctf_file_stream, 1);
2157 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2158 file_stream->pos.fd = -1;
2159 file_stream->pos.index_fp = NULL;
2160
2161 strncpy(file_stream->parent.path, path, PATH_MAX);
2162 file_stream->parent.path[PATH_MAX - 1] = '\0';
2163
2164 if (packet_seek) {
2165 file_stream->pos.packet_seek = packet_seek;
2166 } else {
2167 fprintf(stderr, "[error] packet_seek function undefined.\n");
2168 ret = -1;
2169 goto error_def;
2170 }
2171
2172 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
2173 if (ret)
2174 goto error_def;
2175 ret = create_trace_definitions(td, &file_stream->parent);
2176 if (ret)
2177 goto error_def;
2178 /*
2179 * For now, only a single clock per trace is supported.
2180 */
2181 file_stream->parent.current_clock = td->parent.single_clock;
2182
2183 /*
2184 * Allocate the index name for this stream and try to open it.
2185 */
2186 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
2187 if (!index_name) {
2188 fprintf(stderr, "[error] Cannot allocate index filename\n");
2189 ret = -ENOMEM;
2190 goto error_def;
2191 }
2192 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
2193 INDEX_PATH, path);
2194
2195 if (bt_faccessat(td->dirfd, td->parent.path, index_name, O_RDONLY, 0) < 0) {
2196 ret = create_stream_packet_index(td, file_stream);
2197 if (ret) {
2198 fprintf(stderr, "[error] Stream index creation error.\n");
2199 goto error_index;
2200 }
2201 } else {
2202 ret = openat(td->dirfd, index_name, flags);
2203 if (ret < 0) {
2204 perror("Index file openat()");
2205 ret = -1;
2206 goto error_free;
2207 }
2208 file_stream->pos.index_fp = fdopen(ret, "r");
2209 if (!file_stream->pos.index_fp) {
2210 perror("fdopen() error");
2211 goto error_free;
2212 }
2213 ret = import_stream_packet_index(td, file_stream);
2214 if (ret) {
2215 ret = -1;
2216 goto error_index;
2217 }
2218 ret = fclose(file_stream->pos.index_fp);
2219 if (ret < 0) {
2220 perror("close index");
2221 goto error_free;
2222 }
2223 }
2224 free(index_name);
2225
2226 /* Add stream file to stream class */
2227 g_ptr_array_add(file_stream->parent.stream_class->streams,
2228 &file_stream->parent);
2229 return 0;
2230
2231 error_index:
2232 if (file_stream->pos.index_fp) {
2233 ret = fclose(file_stream->pos.index_fp);
2234 if (ret < 0) {
2235 perror("close index");
2236 }
2237 }
2238 if (file_stream->parent.trace_packet_header)
2239 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2240 error_free:
2241 free(index_name);
2242 error_def:
2243 closeret = ctf_fini_pos(&file_stream->pos);
2244 if (closeret) {
2245 fprintf(stderr, "Error on ctf_fini_pos\n");
2246 }
2247 g_free(file_stream);
2248 fd_is_empty_file:
2249 fd_is_dir_ok:
2250 fstat_error:
2251 closeret = close(fd);
2252 if (closeret) {
2253 perror("Error on fd close");
2254 }
2255 error:
2256 return ret;
2257 }
2258
2259 static
2260 int ctf_open_trace_read(struct ctf_trace *td,
2261 const char *path, int flags,
2262 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2263 int whence), FILE *metadata_fp)
2264 {
2265 struct ctf_scanner *scanner;
2266 int ret, closeret;
2267 struct dirent *dirent;
2268 struct dirent *diriter;
2269 size_t dirent_len;
2270 int pc_name_max;
2271 char *ext;
2272
2273 td->flags = flags;
2274
2275 /* Open trace directory */
2276 td->dir = opendir(path);
2277 if (!td->dir) {
2278 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
2279 ret = -ENOENT;
2280 goto error;
2281 }
2282
2283 td->dirfd = open(path, 0);
2284 if (td->dirfd < 0) {
2285 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
2286 perror("Trace directory open");
2287 ret = -errno;
2288 goto error_dirfd;
2289 }
2290 strncpy(td->parent.path, path, sizeof(td->parent.path));
2291 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
2292
2293 /*
2294 * Keep the metadata file separate.
2295 * Keep scanner object local to the open. We don't support
2296 * incremental metadata append for on-disk traces.
2297 */
2298 scanner = ctf_scanner_alloc();
2299 if (!scanner) {
2300 fprintf(stderr, "[error] Error allocating scanner\n");
2301 ret = -ENOMEM;
2302 goto error_metadata;
2303 }
2304 ret = ctf_trace_metadata_read(td, metadata_fp, scanner, 0);
2305 ctf_scanner_free(scanner);
2306 if (ret) {
2307 if (ret == -ENOENT) {
2308 fprintf(stderr, "[warning] Empty metadata.\n");
2309 }
2310 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
2311 goto error_metadata;
2312 }
2313
2314 /*
2315 * Open each stream: for each file, try to open, check magic
2316 * number, and get the stream ID to add to the right location in
2317 * the stream array.
2318 */
2319
2320 pc_name_max = fpathconf(td->dirfd, _PC_NAME_MAX);
2321 if (pc_name_max < 0) {
2322 perror("Error on fpathconf");
2323 fprintf(stderr, "[error] Failed to get _PC_NAME_MAX for path \"%s\".\n", path);
2324 ret = -1;
2325 goto error_metadata;
2326 }
2327
2328 dirent_len = offsetof(struct dirent, d_name) + pc_name_max + 1;
2329
2330 dirent = malloc(dirent_len);
2331
2332 for (;;) {
2333 ret = readdir_r(td->dir, dirent, &diriter);
2334 if (ret) {
2335 fprintf(stderr, "[error] Readdir error.\n");
2336 goto readdir_error;
2337 }
2338 if (!diriter)
2339 break;
2340 /* Ignore hidden files, ., .. and metadata. */
2341 if (!strncmp(diriter->d_name, ".", 1)
2342 || !strcmp(diriter->d_name, "..")
2343 || !strcmp(diriter->d_name, "metadata"))
2344 continue;
2345
2346 /* Ignore index files : *.idx */
2347 ext = strrchr(diriter->d_name, '.');
2348 if (ext && (!strcmp(ext, ".idx"))) {
2349 continue;
2350 }
2351
2352 ret = ctf_open_file_stream_read(td, diriter->d_name,
2353 flags, packet_seek);
2354 if (ret) {
2355 fprintf(stderr, "[error] Open file stream error.\n");
2356 goto readdir_error;
2357 }
2358 }
2359
2360 free(dirent);
2361 return 0;
2362
2363 readdir_error:
2364 free(dirent);
2365 error_metadata:
2366 closeret = close(td->dirfd);
2367 if (closeret) {
2368 perror("Error on fd close");
2369 }
2370 error_dirfd:
2371 closeret = closedir(td->dir);
2372 if (closeret) {
2373 perror("Error on closedir");
2374 }
2375 error:
2376 return ret;
2377 }
2378
2379 /*
2380 * ctf_open_trace: Open a CTF trace and index it.
2381 * Note that the user must seek the trace after the open (using the iterator)
2382 * since the index creation read it entirely.
2383 */
2384 static
2385 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
2386 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2387 int whence), FILE *metadata_fp)
2388 {
2389 struct ctf_trace *td;
2390 int ret;
2391
2392 /*
2393 * If packet_seek is NULL, we provide our default version.
2394 */
2395 if (!packet_seek)
2396 packet_seek = ctf_packet_seek;
2397
2398 td = g_new0(struct ctf_trace, 1);
2399
2400 switch (flags & O_ACCMODE) {
2401 case O_RDONLY:
2402 if (!path) {
2403 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
2404 goto error;
2405 }
2406 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
2407 if (ret)
2408 goto error;
2409 break;
2410 case O_RDWR:
2411 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
2412 goto error;
2413 default:
2414 fprintf(stderr, "[error] Incorrect open flags.\n");
2415 goto error;
2416 }
2417
2418 ret = trace_debug_info_create(td);
2419 if (ret) {
2420 goto error;
2421 }
2422
2423 return &td->parent;
2424 error:
2425 trace_debug_info_destroy(td);
2426 g_free(td);
2427 return NULL;
2428 }
2429
2430 static
2431 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
2432 struct bt_mmap_stream *mmap_info)
2433 {
2434 pos->mmap_offset = 0;
2435 pos->packet_size = 0;
2436 pos->content_size = 0;
2437 pos->content_size_loc = NULL;
2438 pos->fd = mmap_info->fd;
2439 pos->base_mma = NULL;
2440 pos->offset = 0;
2441 pos->dummy = false;
2442 pos->cur_index = 0;
2443 pos->prot = PROT_READ;
2444 pos->flags = MAP_PRIVATE;
2445 pos->parent.rw_table = read_dispatch_table;
2446 pos->parent.event_cb = ctf_read_event;
2447 pos->priv = mmap_info->priv;
2448 pos->packet_index = g_array_new(FALSE, TRUE,
2449 sizeof(struct packet_index));
2450 }
2451
2452 static
2453 int prepare_mmap_stream_definition(struct ctf_trace *td,
2454 struct ctf_file_stream *file_stream,
2455 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2456 int whence))
2457 {
2458 struct ctf_stream_declaration *stream;
2459 uint64_t stream_id;
2460 int ret;
2461
2462 /* Ask for the first packet to get the stream_id. */
2463 packet_seek(&file_stream->pos.parent, 0, SEEK_SET);
2464 stream_id = file_stream->parent.stream_id;
2465 if (stream_id >= td->streams->len) {
2466 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2467 "in metadata.\n", stream_id);
2468 ret = -EINVAL;
2469 goto end;
2470 }
2471 stream = g_ptr_array_index(td->streams, stream_id);
2472 if (!stream) {
2473 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2474 "in metadata.\n", stream_id);
2475 ret = -EINVAL;
2476 goto end;
2477 }
2478 file_stream->parent.stream_class = stream;
2479 ret = create_stream_definitions(td, &file_stream->parent);
2480 end:
2481 return ret;
2482 }
2483
2484 static
2485 int ctf_open_mmap_stream_read(struct ctf_trace *td,
2486 struct bt_mmap_stream *mmap_info,
2487 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2488 int whence))
2489 {
2490 int ret;
2491 struct ctf_file_stream *file_stream;
2492
2493 file_stream = g_new0(struct ctf_file_stream, 1);
2494 file_stream->parent.stream_id = -1ULL;
2495 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2496 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2497
2498 file_stream->pos.packet_seek = packet_seek;
2499
2500 ret = create_trace_definitions(td, &file_stream->parent);
2501 if (ret) {
2502 goto error_def;
2503 }
2504
2505 ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
2506 if (ret)
2507 goto error_index;
2508
2509 /*
2510 * For now, only a single clock per trace is supported.
2511 */
2512 file_stream->parent.current_clock = td->parent.single_clock;
2513
2514 /* Add stream file to stream class */
2515 g_ptr_array_add(file_stream->parent.stream_class->streams,
2516 &file_stream->parent);
2517 return 0;
2518
2519 error_index:
2520 if (file_stream->parent.trace_packet_header)
2521 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2522 error_def:
2523 g_free(file_stream);
2524 return ret;
2525 }
2526
2527 static
2528 int ctf_open_mmap_trace_read(struct ctf_trace *td,
2529 struct bt_mmap_stream_list *mmap_list,
2530 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2531 int whence),
2532 FILE *metadata_fp)
2533 {
2534 int ret;
2535 struct bt_mmap_stream *mmap_info;
2536
2537 td->scanner = ctf_scanner_alloc();
2538 if (!td->scanner) {
2539 fprintf(stderr, "[error] Error allocating scanner\n");
2540 ret = -ENOMEM;
2541 goto error;
2542 }
2543 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 0);
2544 if (ret) {
2545 if (ret == -ENOENT) {
2546 fprintf(stderr, "[warning] Empty metadata.\n");
2547 }
2548 goto error;
2549 }
2550
2551 /*
2552 * for each stream, try to open, check magic number, and get the
2553 * stream ID to add to the right location in the stream array.
2554 */
2555 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2556 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2557 if (ret) {
2558 fprintf(stderr, "[error] Open file mmap stream error.\n");
2559 goto error;
2560 }
2561 }
2562 return 0;
2563
2564 error:
2565 ctf_scanner_free(td->scanner);
2566 return ret;
2567 }
2568
2569 static
2570 struct bt_trace_descriptor *ctf_open_mmap_trace(
2571 struct bt_mmap_stream_list *mmap_list,
2572 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2573 int whence),
2574 FILE *metadata_fp)
2575 {
2576 struct ctf_trace *td;
2577 int ret;
2578
2579 if (!metadata_fp) {
2580 fprintf(stderr, "[error] No metadata file pointer associated, "
2581 "required for mmap parsing\n");
2582 goto error;
2583 }
2584 if (!packet_seek) {
2585 fprintf(stderr, "[error] packet_seek function undefined.\n");
2586 goto error;
2587 }
2588 td = g_new0(struct ctf_trace, 1);
2589 td->dirfd = -1;
2590 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2591 if (ret)
2592 goto error_free;
2593
2594 ret = trace_debug_info_create(td);
2595 if (ret) {
2596 goto error_free;
2597 }
2598
2599 return &td->parent;
2600
2601 error_free:
2602 g_free(td);
2603 error:
2604 return NULL;
2605 }
2606
2607 int ctf_append_trace_metadata(struct bt_trace_descriptor *tdp,
2608 FILE *metadata_fp)
2609 {
2610 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2611 int i, j;
2612 int ret;
2613
2614 if (!td->scanner)
2615 return -EINVAL;
2616 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 1);
2617 if (ret)
2618 return ret;
2619 /* for each stream_class */
2620 for (i = 0; i < td->streams->len; i++) {
2621 struct ctf_stream_declaration *stream_class;
2622
2623 stream_class = g_ptr_array_index(td->streams, i);
2624 if (!stream_class)
2625 continue;
2626 /* for each stream */
2627 for (j = 0; j < stream_class->streams->len; j++) {
2628 struct ctf_stream_definition *stream;
2629
2630 stream = g_ptr_array_index(stream_class->streams, j);
2631 if (!stream)
2632 continue;
2633 ret = copy_event_declarations_stream_class_to_stream(td,
2634 stream_class, stream);
2635 if (ret)
2636 return ret;
2637 }
2638 }
2639 return 0;
2640 }
2641
2642 static
2643 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2644 {
2645 int i, j, k;
2646 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2647
2648 /* for each stream_class */
2649 for (i = 0; i < td->streams->len; i++) {
2650 struct ctf_stream_declaration *stream_class;
2651
2652 stream_class = g_ptr_array_index(td->streams, i);
2653 if (!stream_class)
2654 continue;
2655 /* for each file_stream */
2656 for (j = 0; j < stream_class->streams->len; j++) {
2657 struct ctf_stream_definition *stream;
2658 struct ctf_stream_pos *stream_pos;
2659 struct ctf_file_stream *cfs;
2660
2661 stream = g_ptr_array_index(stream_class->streams, j);
2662 if (!stream)
2663 continue;
2664 cfs = container_of(stream, struct ctf_file_stream,
2665 parent);
2666 stream_pos = &cfs->pos;
2667 if (!stream_pos->packet_index)
2668 continue;
2669
2670 for (k = 0; k < stream_pos->packet_index->len; k++) {
2671 struct packet_index *index;
2672
2673 index = &g_array_index(stream_pos->packet_index,
2674 struct packet_index, k);
2675 index->ts_real.timestamp_begin =
2676 ctf_get_real_timestamp(stream,
2677 index->ts_cycles.timestamp_begin);
2678 index->ts_real.timestamp_end =
2679 ctf_get_real_timestamp(stream,
2680 index->ts_cycles.timestamp_end);
2681 }
2682 }
2683 }
2684 return 0;
2685 }
2686
2687 static
2688 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2689 {
2690 int ret;
2691
2692 ret = ctf_fini_pos(&file_stream->pos);
2693 if (ret) {
2694 fprintf(stderr, "Error on ctf_fini_pos\n");
2695 return -1;
2696 }
2697 if (file_stream->pos.fd >= 0) {
2698 ret = close(file_stream->pos.fd);
2699 if (ret) {
2700 perror("Error closing file fd");
2701 return -1;
2702 }
2703 }
2704 return 0;
2705 }
2706
2707 static
2708 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2709 {
2710 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2711 int ret;
2712
2713 if (td->streams) {
2714 int i;
2715
2716 for (i = 0; i < td->streams->len; i++) {
2717 struct ctf_stream_declaration *stream;
2718 int j;
2719
2720 stream = g_ptr_array_index(td->streams, i);
2721 if (!stream)
2722 continue;
2723 for (j = 0; j < stream->streams->len; j++) {
2724 struct ctf_file_stream *file_stream;
2725 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2726 struct ctf_file_stream, parent);
2727 ret = ctf_close_file_stream(file_stream);
2728 if (ret)
2729 return ret;
2730 }
2731 }
2732 }
2733 ctf_destroy_metadata(td);
2734 ctf_scanner_free(td->scanner);
2735 if (td->dirfd >= 0) {
2736 ret = close(td->dirfd);
2737 if (ret) {
2738 perror("Error closing dirfd");
2739 return ret;
2740 }
2741 }
2742 if (td->dir) {
2743 ret = closedir(td->dir);
2744 if (ret) {
2745 perror("Error closedir");
2746 return ret;
2747 }
2748 }
2749 free(td->metadata_string);
2750 trace_debug_info_destroy(td);
2751 g_free(td);
2752 return 0;
2753 }
2754
2755 static
2756 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2757 struct bt_context *ctx)
2758 {
2759 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2760 parent);
2761
2762 td->parent.ctx = ctx;
2763 }
2764
2765 static
2766 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2767 struct bt_trace_handle *handle)
2768 {
2769 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2770 parent);
2771
2772 td->parent.handle = handle;
2773 }
2774
2775 static
2776 void __attribute__((constructor)) ctf_init(void)
2777 {
2778 int ret;
2779
2780 ctf_format.name = g_quark_from_static_string("ctf");
2781 ret = bt_register_format(&ctf_format);
2782 assert(!ret);
2783 }
2784
2785 static
2786 void __attribute__((destructor)) ctf_exit(void)
2787 {
2788 bt_unregister_format(&ctf_format);
2789 }
This page took 0.142427 seconds and 4 git commands to generate.