Fix: Remove extra quote after an include directive
[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 <inttypes.h>
39 #include <stdio.h>
40 #include <sys/mman.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <dirent.h>
46 #include <glib.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49
50 #include "metadata/ctf-scanner.h"
51 #include "metadata/ctf-parser.h"
52 #include "metadata/ctf-ast.h"
53 #include "events-private.h"
54 #include <babeltrace/compat/memstream.h>
55
56 #define LOG2_CHAR_BIT 3
57
58 /*
59 * Length of first attempt at mapping a packet header, in bits.
60 */
61 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
62
63 /*
64 * Lenght of packet to write, in bits.
65 */
66 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
67
68 #ifndef min
69 #define min(a, b) (((a) < (b)) ? (a) : (b))
70 #endif
71
72 #define NSEC_PER_SEC 1000000000ULL
73
74 int opt_clock_cycles,
75 opt_clock_seconds,
76 opt_clock_date,
77 opt_clock_gmt;
78
79 uint64_t opt_clock_offset;
80 uint64_t opt_clock_offset_ns;
81
82 extern int yydebug;
83
84 static
85 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
86 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
87 int whence),
88 FILE *metadata_fp);
89 static
90 struct bt_trace_descriptor *ctf_open_mmap_trace(
91 struct bt_mmap_stream_list *mmap_list,
92 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
93 int whence),
94 FILE *metadata_fp);
95 static
96 void ctf_set_context(struct bt_trace_descriptor *descriptor,
97 struct bt_context *ctx);
98 static
99 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
100 struct bt_trace_handle *handle);
101
102 static
103 int ctf_close_trace(struct bt_trace_descriptor *descriptor);
104 static
105 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
106 struct bt_trace_handle *handle, enum bt_clock_type type);
107 static
108 uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
109 struct bt_trace_handle *handle, enum bt_clock_type type);
110 static
111 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
112
113 static
114 rw_dispatch read_dispatch_table[] = {
115 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
116 [ CTF_TYPE_FLOAT ] = ctf_float_read,
117 [ CTF_TYPE_ENUM ] = ctf_enum_read,
118 [ CTF_TYPE_STRING ] = ctf_string_read,
119 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
120 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
121 [ CTF_TYPE_ARRAY ] = ctf_array_read,
122 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
123 };
124
125 static
126 rw_dispatch write_dispatch_table[] = {
127 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
128 [ CTF_TYPE_FLOAT ] = ctf_float_write,
129 [ CTF_TYPE_ENUM ] = ctf_enum_write,
130 [ CTF_TYPE_STRING ] = ctf_string_write,
131 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
132 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
133 [ CTF_TYPE_ARRAY ] = ctf_array_write,
134 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
135 };
136
137 static
138 struct bt_format ctf_format = {
139 .open_trace = ctf_open_trace,
140 .open_mmap_trace = ctf_open_mmap_trace,
141 .close_trace = ctf_close_trace,
142 .set_context = ctf_set_context,
143 .set_handle = ctf_set_handle,
144 .timestamp_begin = ctf_timestamp_begin,
145 .timestamp_end = ctf_timestamp_end,
146 .convert_index_timestamp = ctf_convert_index_timestamp,
147 };
148
149 static
150 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
151 struct bt_trace_handle *handle, enum bt_clock_type type)
152 {
153 struct ctf_trace *tin;
154 uint64_t begin = ULLONG_MAX;
155 int i, j;
156
157 tin = container_of(descriptor, struct ctf_trace, parent);
158
159 if (!tin)
160 goto error;
161
162 /* for each stream_class */
163 for (i = 0; i < tin->streams->len; i++) {
164 struct ctf_stream_declaration *stream_class;
165
166 stream_class = g_ptr_array_index(tin->streams, i);
167 if (!stream_class)
168 continue;
169 /* for each file_stream */
170 for (j = 0; j < stream_class->streams->len; j++) {
171 struct ctf_stream_definition *stream;
172 struct ctf_file_stream *cfs;
173 struct ctf_stream_pos *stream_pos;
174 struct packet_index *index;
175
176 stream = g_ptr_array_index(stream_class->streams, j);
177 cfs = container_of(stream, struct ctf_file_stream,
178 parent);
179 stream_pos = &cfs->pos;
180
181 if (!stream_pos->packet_real_index)
182 goto error;
183
184 if (stream_pos->packet_real_index->len <= 0)
185 continue;
186
187 if (type == BT_CLOCK_REAL) {
188 index = &g_array_index(stream_pos->packet_real_index,
189 struct packet_index,
190 stream_pos->packet_real_index->len - 1);
191 } else if (type == BT_CLOCK_CYCLES) {
192 index = &g_array_index(stream_pos->packet_cycles_index,
193 struct packet_index,
194 stream_pos->packet_real_index->len - 1);
195
196 } else {
197 goto error;
198 }
199 if (index->timestamp_begin < begin)
200 begin = index->timestamp_begin;
201 }
202 }
203
204 return begin;
205
206 error:
207 return -1ULL;
208 }
209
210 static
211 uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
212 struct bt_trace_handle *handle, enum bt_clock_type type)
213 {
214 struct ctf_trace *tin;
215 uint64_t end = 0;
216 int i, j;
217
218 tin = container_of(descriptor, struct ctf_trace, parent);
219
220 if (!tin)
221 goto error;
222
223 /* for each stream_class */
224 for (i = 0; i < tin->streams->len; i++) {
225 struct ctf_stream_declaration *stream_class;
226
227 stream_class = g_ptr_array_index(tin->streams, i);
228 if (!stream_class)
229 continue;
230 /* for each file_stream */
231 for (j = 0; j < stream_class->streams->len; j++) {
232 struct ctf_stream_definition *stream;
233 struct ctf_file_stream *cfs;
234 struct ctf_stream_pos *stream_pos;
235 struct packet_index *index;
236
237 stream = g_ptr_array_index(stream_class->streams, j);
238 cfs = container_of(stream, struct ctf_file_stream,
239 parent);
240 stream_pos = &cfs->pos;
241
242 if (!stream_pos->packet_real_index)
243 goto error;
244
245 if (stream_pos->packet_real_index->len <= 0)
246 continue;
247
248 if (type == BT_CLOCK_REAL) {
249 index = &g_array_index(stream_pos->packet_real_index,
250 struct packet_index,
251 stream_pos->packet_real_index->len - 1);
252 } else if (type == BT_CLOCK_CYCLES) {
253 index = &g_array_index(stream_pos->packet_cycles_index,
254 struct packet_index,
255 stream_pos->packet_real_index->len - 1);
256
257 } else {
258 goto error;
259 }
260 if (index->timestamp_end > end)
261 end = index->timestamp_end;
262 }
263 }
264
265 return end;
266
267 error:
268 return -1ULL;
269 }
270
271 /*
272 * Update stream current timestamp
273 */
274 static
275 void ctf_update_timestamp(struct ctf_stream_definition *stream,
276 struct definition_integer *integer_definition)
277 {
278 struct declaration_integer *integer_declaration =
279 integer_definition->declaration;
280 uint64_t oldval, newval, updateval;
281
282 if (unlikely(integer_declaration->len == 64)) {
283 stream->prev_cycles_timestamp = stream->cycles_timestamp;
284 stream->cycles_timestamp = integer_definition->value._unsigned;
285 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
286 stream->prev_cycles_timestamp);
287 stream->real_timestamp = ctf_get_real_timestamp(stream,
288 stream->cycles_timestamp);
289 return;
290 }
291 /* keep low bits */
292 oldval = stream->cycles_timestamp;
293 oldval &= (1ULL << integer_declaration->len) - 1;
294 newval = integer_definition->value._unsigned;
295 /* Test for overflow by comparing low bits */
296 if (newval < oldval)
297 newval += 1ULL << integer_declaration->len;
298 /* updateval contains old high bits, and new low bits (sum) */
299 updateval = stream->cycles_timestamp;
300 updateval &= ~((1ULL << integer_declaration->len) - 1);
301 updateval += newval;
302 stream->prev_cycles_timestamp = stream->cycles_timestamp;
303 stream->cycles_timestamp = updateval;
304
305 /* convert to real timestamp */
306 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
307 stream->prev_cycles_timestamp);
308 stream->real_timestamp = ctf_get_real_timestamp(stream,
309 stream->cycles_timestamp);
310 }
311
312 /*
313 * Print timestamp, rescaling clock frequency to nanoseconds and
314 * applying offsets as needed (unix time).
315 */
316 static
317 void ctf_print_timestamp_real(FILE *fp,
318 struct ctf_stream_definition *stream,
319 uint64_t timestamp)
320 {
321 uint64_t ts_sec = 0, ts_nsec;
322
323 ts_nsec = timestamp;
324
325 /* Add command-line offset in ns*/
326 ts_nsec += opt_clock_offset_ns;
327
328 /* Add command-line offset */
329 ts_sec += opt_clock_offset;
330
331 ts_sec += ts_nsec / NSEC_PER_SEC;
332 ts_nsec = ts_nsec % NSEC_PER_SEC;
333
334 if (!opt_clock_seconds) {
335 struct tm tm;
336 time_t time_s = (time_t) ts_sec;
337
338 if (!opt_clock_gmt) {
339 struct tm *res;
340
341 res = localtime_r(&time_s, &tm);
342 if (!res) {
343 fprintf(stderr, "[warning] Unable to get localtime.\n");
344 goto seconds;
345 }
346 } else {
347 struct tm *res;
348
349 res = gmtime_r(&time_s, &tm);
350 if (!res) {
351 fprintf(stderr, "[warning] Unable to get gmtime.\n");
352 goto seconds;
353 }
354 }
355 if (opt_clock_date) {
356 char timestr[26];
357 size_t res;
358
359 /* Print date and time */
360 res = strftime(timestr, sizeof(timestr),
361 "%F ", &tm);
362 if (!res) {
363 fprintf(stderr, "[warning] Unable to print ascii time.\n");
364 goto seconds;
365 }
366 fprintf(fp, "%s", timestr);
367 }
368 /* Print time in HH:MM:SS.ns */
369 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
370 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
371 goto end;
372 }
373 seconds:
374 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
375 ts_sec, ts_nsec);
376
377 end:
378 return;
379 }
380
381 /*
382 * Print timestamp, in cycles
383 */
384 static
385 void ctf_print_timestamp_cycles(FILE *fp,
386 struct ctf_stream_definition *stream,
387 uint64_t timestamp)
388 {
389 fprintf(fp, "%020" PRIu64, timestamp);
390 }
391
392 void ctf_print_timestamp(FILE *fp,
393 struct ctf_stream_definition *stream,
394 uint64_t timestamp)
395 {
396 if (opt_clock_cycles) {
397 ctf_print_timestamp_cycles(fp, stream, timestamp);
398 } else {
399 ctf_print_timestamp_real(fp, stream, timestamp);
400 }
401 }
402
403 static
404 void print_uuid(FILE *fp, unsigned char *uuid)
405 {
406 int i;
407
408 for (i = 0; i < BABELTRACE_UUID_LEN; i++)
409 fprintf(fp, "%x", (unsigned int) uuid[i]);
410 }
411
412 void ctf_print_discarded(FILE *fp, struct ctf_stream_definition *stream,
413 int end_stream)
414 {
415 fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events %sbetween [",
416 stream->events_discarded,
417 end_stream ? "at end of stream " : "");
418 if (opt_clock_cycles) {
419 ctf_print_timestamp(fp, stream,
420 stream->prev_cycles_timestamp);
421 fprintf(fp, "] and [");
422 ctf_print_timestamp(fp, stream,
423 stream->prev_cycles_timestamp_end);
424 } else {
425 ctf_print_timestamp(fp, stream,
426 stream->prev_real_timestamp);
427 fprintf(fp, "] and [");
428 ctf_print_timestamp(fp, stream,
429 stream->prev_real_timestamp_end);
430 }
431 fprintf(fp, "] in trace UUID ");
432 print_uuid(fp, stream->stream_class->trace->uuid);
433 if (stream->stream_class->trace->parent.path[0])
434 fprintf(fp, ", at path: \"%s\"",
435 stream->stream_class->trace->parent.path);
436
437 fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
438 if (stream->path[0])
439 fprintf(fp, ", at relative path: \"%s\"", stream->path);
440 fprintf(fp, ". ");
441 fprintf(fp, "You should consider recording a new trace with larger "
442 "buffers or with fewer events enabled.\n");
443 fflush(fp);
444 }
445
446 static
447 int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
448 {
449 struct ctf_stream_pos *pos =
450 container_of(ppos, struct ctf_stream_pos, parent);
451 struct ctf_stream_declaration *stream_class = stream->stream_class;
452 struct ctf_event_definition *event;
453 uint64_t id = 0;
454 int ret;
455
456 /* We need to check for EOF here for empty files. */
457 if (unlikely(pos->offset == EOF))
458 return EOF;
459
460 ctf_pos_get_event(pos);
461
462 /* save the current position as a restore point */
463 pos->last_offset = pos->offset;
464
465 /*
466 * This is the EOF check after we've advanced the position in
467 * ctf_pos_get_event.
468 */
469 if (unlikely(pos->offset == EOF))
470 return EOF;
471 assert(pos->offset < pos->content_size);
472
473 /* Read event header */
474 if (likely(stream->stream_event_header)) {
475 struct definition_integer *integer_definition;
476 struct bt_definition *variant;
477
478 ret = generic_rw(ppos, &stream->stream_event_header->p);
479 if (unlikely(ret))
480 goto error;
481 /* lookup event id */
482 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
483 if (integer_definition) {
484 id = integer_definition->value._unsigned;
485 } else {
486 struct definition_enum *enum_definition;
487
488 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
489 if (enum_definition) {
490 id = enum_definition->integer->value._unsigned;
491 }
492 }
493
494 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
495 if (variant) {
496 integer_definition = bt_lookup_integer(variant, "id", FALSE);
497 if (integer_definition) {
498 id = integer_definition->value._unsigned;
499 }
500 }
501 stream->event_id = id;
502
503 /* lookup timestamp */
504 stream->has_timestamp = 0;
505 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
506 if (integer_definition) {
507 ctf_update_timestamp(stream, integer_definition);
508 stream->has_timestamp = 1;
509 } else {
510 if (variant) {
511 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
512 if (integer_definition) {
513 ctf_update_timestamp(stream, integer_definition);
514 stream->has_timestamp = 1;
515 }
516 }
517 }
518 }
519
520 /* Read stream-declared event context */
521 if (stream->stream_event_context) {
522 ret = generic_rw(ppos, &stream->stream_event_context->p);
523 if (ret)
524 goto error;
525 }
526
527 if (unlikely(id >= stream_class->events_by_id->len)) {
528 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
529 return -EINVAL;
530 }
531 event = g_ptr_array_index(stream->events_by_id, id);
532 if (unlikely(!event)) {
533 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
534 return -EINVAL;
535 }
536
537 /* Read event-declared event context */
538 if (event->event_context) {
539 ret = generic_rw(ppos, &event->event_context->p);
540 if (ret)
541 goto error;
542 }
543
544 /* Read event payload */
545 if (likely(event->event_fields)) {
546 ret = generic_rw(ppos, &event->event_fields->p);
547 if (ret)
548 goto error;
549 }
550
551 return 0;
552
553 error:
554 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
555 return ret;
556 }
557
558 static
559 int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
560 {
561 struct ctf_stream_declaration *stream_class = stream->stream_class;
562 struct ctf_event_definition *event;
563 uint64_t id;
564 int ret;
565
566 id = stream->event_id;
567
568 /* print event header */
569 if (likely(stream->stream_event_header)) {
570 ret = generic_rw(pos, &stream->stream_event_header->p);
571 if (ret)
572 goto error;
573 }
574
575 /* print stream-declared event context */
576 if (stream->stream_event_context) {
577 ret = generic_rw(pos, &stream->stream_event_context->p);
578 if (ret)
579 goto error;
580 }
581
582 if (unlikely(id >= stream_class->events_by_id->len)) {
583 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
584 return -EINVAL;
585 }
586 event = g_ptr_array_index(stream->events_by_id, id);
587 if (unlikely(!event)) {
588 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
589 return -EINVAL;
590 }
591
592 /* print event-declared event context */
593 if (event->event_context) {
594 ret = generic_rw(pos, &event->event_context->p);
595 if (ret)
596 goto error;
597 }
598
599 /* Read and print event payload */
600 if (likely(event->event_fields)) {
601 ret = generic_rw(pos, &event->event_fields->p);
602 if (ret)
603 goto error;
604 }
605
606 return 0;
607
608 error:
609 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
610 return ret;
611 }
612
613 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
614 int fd, int open_flags)
615 {
616 pos->fd = fd;
617 if (fd >= 0) {
618 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
619 sizeof(struct packet_index));
620 pos->packet_real_index = g_array_new(FALSE, TRUE,
621 sizeof(struct packet_index));
622 } else {
623 pos->packet_cycles_index = NULL;
624 pos->packet_real_index = NULL;
625 }
626 switch (open_flags & O_ACCMODE) {
627 case O_RDONLY:
628 pos->prot = PROT_READ;
629 pos->flags = MAP_PRIVATE;
630 pos->parent.rw_table = read_dispatch_table;
631 pos->parent.event_cb = ctf_read_event;
632 pos->parent.trace = trace;
633 break;
634 case O_RDWR:
635 pos->prot = PROT_WRITE; /* Write has priority */
636 pos->flags = MAP_SHARED;
637 pos->parent.rw_table = write_dispatch_table;
638 pos->parent.event_cb = ctf_write_event;
639 pos->parent.trace = trace;
640 if (fd >= 0)
641 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
642 break;
643 default:
644 assert(0);
645 }
646 return 0;
647 }
648
649 int ctf_fini_pos(struct ctf_stream_pos *pos)
650 {
651 if (pos->prot == PROT_WRITE && pos->content_size_loc)
652 *pos->content_size_loc = pos->offset;
653 if (pos->base_mma) {
654 int ret;
655
656 /* unmap old base */
657 ret = munmap_align(pos->base_mma);
658 if (ret) {
659 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
660 strerror(errno));
661 return -1;
662 }
663 }
664 if (pos->packet_cycles_index)
665 (void) g_array_free(pos->packet_cycles_index, TRUE);
666 if (pos->packet_real_index)
667 (void) g_array_free(pos->packet_real_index, TRUE);
668 return 0;
669 }
670
671 /*
672 * for SEEK_CUR: go to next packet.
673 * for SEEK_POS: go to packet numer (index).
674 */
675 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
676 {
677 struct ctf_stream_pos *pos =
678 container_of(stream_pos, struct ctf_stream_pos, parent);
679 struct ctf_file_stream *file_stream =
680 container_of(pos, struct ctf_file_stream, pos);
681 int ret;
682 off_t off;
683 struct packet_index *packet_index;
684
685 switch (whence) {
686 case SEEK_CUR:
687 case SEEK_SET: /* Fall-through */
688 break; /* OK */
689 default:
690 assert(0);
691 }
692
693 if (pos->prot == PROT_WRITE && pos->content_size_loc)
694 *pos->content_size_loc = pos->offset;
695
696 if (pos->base_mma) {
697 /* unmap old base */
698 ret = munmap_align(pos->base_mma);
699 if (ret) {
700 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
701 strerror(errno));
702 assert(0);
703 }
704 pos->base_mma = NULL;
705 }
706
707 /*
708 * The caller should never ask for ctf_move_pos across packets,
709 * except to get exactly at the beginning of the next packet.
710 */
711 if (pos->prot == PROT_WRITE) {
712 switch (whence) {
713 case SEEK_CUR:
714 /* The writer will add padding */
715 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
716 break;
717 case SEEK_SET:
718 assert(index == 0); /* only seek supported for now */
719 pos->cur_index = 0;
720 break;
721 default:
722 assert(0);
723 }
724 pos->content_size = -1U; /* Unknown at this point */
725 pos->packet_size = WRITE_PACKET_LEN;
726 off = posix_fallocate(pos->fd, pos->mmap_offset,
727 pos->packet_size / CHAR_BIT);
728 assert(off >= 0);
729 pos->offset = 0;
730 } else {
731 read_next_packet:
732 if (pos->cur_index >= pos->packet_cycles_index->len) {
733 pos->offset = EOF;
734 return;
735 }
736 if (pos->cur_index >= pos->packet_real_index->len) {
737 pos->offset = EOF;
738 return;
739 }
740 switch (whence) {
741 case SEEK_CUR:
742 {
743 uint64_t events_discarded_diff;
744
745 if (pos->offset == EOF) {
746 return;
747 }
748 /* For printing discarded event count */
749 packet_index = &g_array_index(pos->packet_cycles_index,
750 struct packet_index, pos->cur_index);
751 file_stream->parent.prev_cycles_timestamp_end =
752 packet_index->timestamp_end;
753 file_stream->parent.prev_cycles_timestamp =
754 packet_index->timestamp_begin;
755
756 packet_index = &g_array_index(pos->packet_real_index,
757 struct packet_index, pos->cur_index);
758 file_stream->parent.prev_real_timestamp_end =
759 packet_index->timestamp_end;
760 file_stream->parent.prev_real_timestamp =
761 packet_index->timestamp_begin;
762
763 events_discarded_diff = packet_index->events_discarded;
764 if (pos->cur_index > 0) {
765 packet_index = &g_array_index(pos->packet_real_index,
766 struct packet_index,
767 pos->cur_index - 1);
768 events_discarded_diff -= packet_index->events_discarded;
769 /*
770 * Deal with 32-bit wrap-around if the
771 * tracer provided a 32-bit field.
772 */
773 if (packet_index->events_discarded_len == 32) {
774 events_discarded_diff = (uint32_t) events_discarded_diff;
775 }
776 }
777 file_stream->parent.events_discarded = events_discarded_diff;
778 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
779 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
780 /* The reader will expect us to skip padding */
781 ++pos->cur_index;
782 break;
783 }
784 case SEEK_SET:
785 packet_index = &g_array_index(pos->packet_cycles_index,
786 struct packet_index, index);
787 pos->last_events_discarded = packet_index->events_discarded;
788 pos->cur_index = index;
789 file_stream->parent.prev_real_timestamp = 0;
790 file_stream->parent.prev_real_timestamp_end = 0;
791 file_stream->parent.prev_cycles_timestamp = 0;
792 file_stream->parent.prev_cycles_timestamp_end = 0;
793 break;
794 default:
795 assert(0);
796 }
797 if (pos->cur_index >= pos->packet_real_index->len) {
798 /*
799 * We need to check if we are in trace read or
800 * called from packet indexing. In this last
801 * case, the collection is not there, so we
802 * cannot print the timestamps.
803 */
804 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
805 /*
806 * When a stream reaches the end of the
807 * file, we need to show the number of
808 * events discarded ourselves, because
809 * there is no next event scheduled to
810 * be printed in the output.
811 */
812 if (file_stream->parent.events_discarded) {
813 fflush(stdout);
814 ctf_print_discarded(stderr,
815 &file_stream->parent,
816 1);
817 file_stream->parent.events_discarded = 0;
818 }
819 }
820 pos->offset = EOF;
821 return;
822 }
823 packet_index = &g_array_index(pos->packet_cycles_index,
824 struct packet_index,
825 pos->cur_index);
826 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
827
828 packet_index = &g_array_index(pos->packet_real_index,
829 struct packet_index,
830 pos->cur_index);
831 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
832 pos->mmap_offset = packet_index->offset;
833
834 /* Lookup context/packet size in index */
835 pos->content_size = packet_index->content_size;
836 pos->packet_size = packet_index->packet_size;
837 if (packet_index->data_offset < packet_index->content_size) {
838 pos->offset = 0; /* will read headers */
839 } else if (packet_index->data_offset == packet_index->content_size) {
840 /* empty packet */
841 pos->offset = packet_index->data_offset;
842 whence = SEEK_CUR;
843 goto read_next_packet;
844 } else {
845 pos->offset = EOF;
846 return;
847 }
848 }
849 /* map new base. Need mapping length from header. */
850 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
851 pos->flags, pos->fd, pos->mmap_offset);
852 if (pos->base_mma == MAP_FAILED) {
853 fprintf(stderr, "[error] mmap error %s.\n",
854 strerror(errno));
855 assert(0);
856 }
857
858 /* update trace_packet_header and stream_packet_context */
859 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
860 /* Read packet header */
861 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
862 assert(!ret);
863 }
864 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
865 /* Read packet context */
866 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
867 assert(!ret);
868 }
869 }
870
871 static
872 int packet_metadata(struct ctf_trace *td, FILE *fp)
873 {
874 uint32_t magic;
875 size_t len;
876 int ret = 0;
877
878 len = fread(&magic, sizeof(magic), 1, fp);
879 if (len != 1) {
880 goto end;
881 }
882 if (magic == TSDL_MAGIC) {
883 ret = 1;
884 td->byte_order = BYTE_ORDER;
885 CTF_TRACE_SET_FIELD(td, byte_order);
886 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
887 ret = 1;
888 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
889 LITTLE_ENDIAN : BIG_ENDIAN;
890 CTF_TRACE_SET_FIELD(td, byte_order);
891 }
892 end:
893 rewind(fp);
894 return ret;
895 }
896
897 /*
898 * Returns 0 on success, -1 on error.
899 */
900 static
901 int check_version(unsigned int major, unsigned int minor)
902 {
903 switch (major) {
904 case 1:
905 switch (minor) {
906 case 8:
907 return 0;
908 default:
909 goto warning;
910 }
911 default:
912 goto warning;
913
914 }
915
916 /* eventually return an error instead of warning */
917 warning:
918 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
919 major, minor);
920 return 0;
921 }
922
923 static
924 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
925 FILE *out)
926 {
927 struct metadata_packet_header header;
928 size_t readlen, writelen, toread;
929 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
930 int ret = 0;
931
932 readlen = fread(&header, header_sizeof(header), 1, in);
933 if (readlen < 1)
934 return -EINVAL;
935
936 if (td->byte_order != BYTE_ORDER) {
937 header.magic = GUINT32_SWAP_LE_BE(header.magic);
938 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
939 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
940 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
941 }
942 if (header.checksum)
943 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
944 if (header.compression_scheme) {
945 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
946 header.compression_scheme);
947 return -EINVAL;
948 }
949 if (header.encryption_scheme) {
950 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
951 header.encryption_scheme);
952 return -EINVAL;
953 }
954 if (header.checksum_scheme) {
955 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
956 header.checksum_scheme);
957 return -EINVAL;
958 }
959 if (check_version(header.major, header.minor) < 0)
960 return -EINVAL;
961 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
962 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
963 CTF_TRACE_SET_FIELD(td, uuid);
964 } else {
965 if (babeltrace_uuid_compare(header.uuid, td->uuid))
966 return -EINVAL;
967 }
968
969 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
970 return -EINVAL;
971
972 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
973
974 for (;;) {
975 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
976 if (ferror(in)) {
977 ret = -EINVAL;
978 break;
979 }
980 if (babeltrace_debug) {
981 buf[readlen] = '\0';
982 fprintf(stderr, "[debug] metadata packet read: %s\n",
983 buf);
984 }
985
986 writelen = fwrite(buf, sizeof(char), readlen, out);
987 if (writelen < readlen) {
988 ret = -EIO;
989 break;
990 }
991 if (ferror(out)) {
992 ret = -EINVAL;
993 break;
994 }
995 toread -= readlen;
996 if (!toread) {
997 ret = 0; /* continue reading next packet */
998 goto read_padding;
999 }
1000 }
1001 return ret;
1002
1003 read_padding:
1004 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1005 ret = fseek(in, toread, SEEK_CUR);
1006 if (ret < 0) {
1007 fprintf(stderr, "[warning] Missing padding at end of file\n");
1008 ret = 0;
1009 }
1010 return ret;
1011 }
1012
1013 static
1014 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1015 char **buf)
1016 {
1017 FILE *in, *out;
1018 size_t size, buflen;
1019 int ret;
1020
1021 in = *fp;
1022 /*
1023 * Using strlen on *buf instead of size of open_memstream
1024 * because its size includes garbage at the end (after final
1025 * \0). This is the allocated size, not the actual string size.
1026 */
1027 out = babeltrace_open_memstream(buf, &size);
1028 if (out == NULL) {
1029 perror("Metadata open_memstream");
1030 return -errno;
1031 }
1032 for (;;) {
1033 ret = ctf_open_trace_metadata_packet_read(td, in, out);
1034 if (ret) {
1035 break;
1036 }
1037 if (feof(in)) {
1038 ret = 0;
1039 break;
1040 }
1041 }
1042 /* close to flush the buffer */
1043 ret = babeltrace_close_memstream(buf, &size, out);
1044 if (ret < 0) {
1045 int closeret;
1046
1047 perror("babeltrace_flush_memstream");
1048 ret = -errno;
1049 closeret = fclose(in);
1050 if (closeret) {
1051 perror("Error in fclose");
1052 }
1053 return ret;
1054 }
1055 ret = fclose(in);
1056 if (ret) {
1057 perror("Error in fclose");
1058 }
1059 /* open for reading */
1060 buflen = strlen(*buf);
1061 if (!buflen) {
1062 *fp = NULL;
1063 return -ENOENT;
1064 }
1065 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1066 if (!*fp) {
1067 perror("Metadata fmemopen");
1068 return -errno;
1069 }
1070 return 0;
1071 }
1072
1073 static
1074 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1075 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1076 int whence), FILE *metadata_fp)
1077 {
1078 struct ctf_scanner *scanner;
1079 struct ctf_file_stream *metadata_stream;
1080 FILE *fp;
1081 char *buf = NULL;
1082 int ret = 0, closeret;
1083
1084 metadata_stream = g_new0(struct ctf_file_stream, 1);
1085 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1086
1087 if (packet_seek) {
1088 metadata_stream->pos.packet_seek = packet_seek;
1089 } else {
1090 fprintf(stderr, "[error] packet_seek function undefined.\n");
1091 ret = -1;
1092 goto end_free;
1093 }
1094
1095 if (metadata_fp) {
1096 fp = metadata_fp;
1097 metadata_stream->pos.fd = -1;
1098 } else {
1099 td->metadata = &metadata_stream->parent;
1100 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1101 if (metadata_stream->pos.fd < 0) {
1102 fprintf(stderr, "Unable to open metadata.\n");
1103 ret = -1;
1104 goto end_free;
1105 }
1106
1107 fp = fdopen(metadata_stream->pos.fd, "r");
1108 if (!fp) {
1109 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1110 perror("Metadata stream open");
1111 ret = -errno;
1112 goto end_stream;
1113 }
1114 /* fd now belongs to fp */
1115 metadata_stream->pos.fd = -1;
1116 }
1117 if (babeltrace_debug)
1118 yydebug = 1;
1119
1120 if (packet_metadata(td, fp)) {
1121 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1122 if (ret) {
1123 /* Warn about empty metadata */
1124 fprintf(stderr, "[warning] Empty metadata.\n");
1125 goto end_packet_read;
1126 }
1127 td->metadata_string = buf;
1128 td->metadata_packetized = 1;
1129 } else {
1130 unsigned int major, minor;
1131 ssize_t nr_items;
1132
1133 td->byte_order = BYTE_ORDER;
1134
1135 /* Check text-only metadata header and version */
1136 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1137 if (nr_items < 2)
1138 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1139 if (check_version(major, minor) < 0) {
1140 ret = -EINVAL;
1141 goto end_packet_read;
1142 }
1143 rewind(fp);
1144 }
1145
1146 scanner = ctf_scanner_alloc(fp);
1147 if (!scanner) {
1148 fprintf(stderr, "[error] Error allocating scanner\n");
1149 ret = -ENOMEM;
1150 goto end_scanner_alloc;
1151 }
1152 ret = ctf_scanner_append_ast(scanner);
1153 if (ret) {
1154 fprintf(stderr, "[error] Error creating AST\n");
1155 goto end;
1156 }
1157
1158 if (babeltrace_debug) {
1159 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1160 if (ret) {
1161 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1162 goto end;
1163 }
1164 }
1165
1166 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1167 if (ret) {
1168 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1169 goto end;
1170 }
1171 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1172 td, td->byte_order);
1173 if (ret) {
1174 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1175 goto end;
1176 }
1177 end:
1178 ctf_scanner_free(scanner);
1179 end_scanner_alloc:
1180 end_packet_read:
1181 if (fp) {
1182 closeret = fclose(fp);
1183 if (closeret) {
1184 perror("Error on fclose");
1185 }
1186 }
1187 end_stream:
1188 if (metadata_stream->pos.fd >= 0) {
1189 closeret = close(metadata_stream->pos.fd);
1190 if (closeret) {
1191 perror("Error on metadata stream fd close");
1192 }
1193 }
1194 end_free:
1195 if (ret)
1196 g_free(metadata_stream);
1197 return ret;
1198 }
1199
1200 static
1201 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1202 struct ctf_stream_definition *stream,
1203 struct ctf_event_declaration *event)
1204 {
1205 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1206
1207 if (event->context_decl) {
1208 struct bt_definition *definition =
1209 event->context_decl->p.definition_new(&event->context_decl->p,
1210 stream->parent_def_scope, 0, 0, "event.context");
1211 if (!definition) {
1212 goto error;
1213 }
1214 stream_event->event_context = container_of(definition,
1215 struct definition_struct, p);
1216 stream->parent_def_scope = stream_event->event_context->p.scope;
1217 }
1218 if (event->fields_decl) {
1219 struct bt_definition *definition =
1220 event->fields_decl->p.definition_new(&event->fields_decl->p,
1221 stream->parent_def_scope, 0, 0, "event.fields");
1222 if (!definition) {
1223 goto error;
1224 }
1225 stream_event->event_fields = container_of(definition,
1226 struct definition_struct, p);
1227 stream->parent_def_scope = stream_event->event_fields->p.scope;
1228 }
1229 stream_event->stream = stream;
1230 return stream_event;
1231
1232 error:
1233 if (stream_event->event_fields)
1234 bt_definition_unref(&stream_event->event_fields->p);
1235 if (stream_event->event_context)
1236 bt_definition_unref(&stream_event->event_context->p);
1237 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1238 g_quark_to_string(event->name));
1239 return NULL;
1240 }
1241
1242 static
1243 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1244 {
1245 struct ctf_stream_declaration *stream_class;
1246 int ret;
1247 int i;
1248
1249 if (stream->stream_definitions_created)
1250 return 0;
1251
1252 stream_class = stream->stream_class;
1253
1254 if (stream_class->packet_context_decl) {
1255 struct bt_definition *definition =
1256 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1257 stream->parent_def_scope, 0, 0, "stream.packet.context");
1258 if (!definition) {
1259 ret = -EINVAL;
1260 goto error;
1261 }
1262 stream->stream_packet_context = container_of(definition,
1263 struct definition_struct, p);
1264 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1265 }
1266 if (stream_class->event_header_decl) {
1267 struct bt_definition *definition =
1268 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1269 stream->parent_def_scope, 0, 0, "stream.event.header");
1270 if (!definition) {
1271 ret = -EINVAL;
1272 goto error;
1273 }
1274 stream->stream_event_header =
1275 container_of(definition, struct definition_struct, p);
1276 stream->parent_def_scope = stream->stream_event_header->p.scope;
1277 }
1278 if (stream_class->event_context_decl) {
1279 struct bt_definition *definition =
1280 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1281 stream->parent_def_scope, 0, 0, "stream.event.context");
1282 if (!definition) {
1283 ret = -EINVAL;
1284 goto error;
1285 }
1286 stream->stream_event_context =
1287 container_of(definition, struct definition_struct, p);
1288 stream->parent_def_scope = stream->stream_event_context->p.scope;
1289 }
1290 stream->events_by_id = g_ptr_array_new();
1291 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1292 for (i = 0; i < stream->events_by_id->len; i++) {
1293 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1294 struct ctf_event_definition *stream_event;
1295
1296 if (!event)
1297 continue;
1298 stream_event = create_event_definitions(td, stream, event);
1299 if (!stream_event) {
1300 ret = -EINVAL;
1301 goto error_event;
1302 }
1303 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1304 }
1305 return 0;
1306
1307 error_event:
1308 for (i = 0; i < stream->events_by_id->len; i++) {
1309 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1310 if (stream_event)
1311 g_free(stream_event);
1312 }
1313 g_ptr_array_free(stream->events_by_id, TRUE);
1314 error:
1315 if (stream->stream_event_context)
1316 bt_definition_unref(&stream->stream_event_context->p);
1317 if (stream->stream_event_header)
1318 bt_definition_unref(&stream->stream_event_header->p);
1319 if (stream->stream_packet_context)
1320 bt_definition_unref(&stream->stream_packet_context->p);
1321 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1322 stream_class->stream_id, strerror(-ret));
1323 return ret;
1324 }
1325
1326 static
1327 int stream_assign_class(struct ctf_trace *td,
1328 struct ctf_file_stream *file_stream,
1329 uint64_t stream_id)
1330 {
1331 struct ctf_stream_declaration *stream;
1332 int ret;
1333
1334 file_stream->parent.stream_id = stream_id;
1335 if (stream_id >= td->streams->len) {
1336 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1337 return -EINVAL;
1338 }
1339 stream = g_ptr_array_index(td->streams, stream_id);
1340 if (!stream) {
1341 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1342 return -EINVAL;
1343 }
1344 file_stream->parent.stream_class = stream;
1345 ret = create_stream_definitions(td, &file_stream->parent);
1346 if (ret)
1347 return ret;
1348 return 0;
1349 }
1350
1351 static
1352 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1353 struct ctf_trace *td,
1354 struct ctf_file_stream *file_stream,
1355 size_t filesize)
1356 {
1357 struct packet_index packet_index;
1358 uint64_t stream_id = 0;
1359 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1360 int first_packet = 0;
1361 int len_index;
1362 int ret;
1363
1364 begin:
1365 if (!pos->mmap_offset) {
1366 first_packet = 1;
1367 }
1368
1369 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1370 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1371 }
1372
1373 if (pos->base_mma) {
1374 /* unmap old base */
1375 ret = munmap_align(pos->base_mma);
1376 if (ret) {
1377 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1378 strerror(errno));
1379 return ret;
1380 }
1381 pos->base_mma = NULL;
1382 }
1383 /* map new base. Need mapping length from header. */
1384 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1385 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1386 assert(pos->base_mma != MAP_FAILED);
1387 /*
1388 * Use current mapping size as temporary content and packet
1389 * size.
1390 */
1391 pos->content_size = packet_map_len;
1392 pos->packet_size = packet_map_len;
1393 pos->offset = 0; /* Position of the packet header */
1394
1395 packet_index.offset = pos->mmap_offset;
1396 packet_index.content_size = 0;
1397 packet_index.packet_size = 0;
1398 packet_index.timestamp_begin = 0;
1399 packet_index.timestamp_end = 0;
1400 packet_index.events_discarded = 0;
1401 packet_index.events_discarded_len = 0;
1402
1403 /* read and check header, set stream id (and check) */
1404 if (file_stream->parent.trace_packet_header) {
1405 /* Read packet header */
1406 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1407 if (ret) {
1408 if (ret == -EFAULT)
1409 goto retry;
1410 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1411 return ret;
1412 }
1413 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1414 if (len_index >= 0) {
1415 struct bt_definition *field;
1416 uint64_t magic;
1417
1418 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1419 magic = bt_get_unsigned_int(field);
1420 if (magic != CTF_MAGIC) {
1421 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1422 magic,
1423 file_stream->pos.packet_cycles_index->len,
1424 (ssize_t) pos->mmap_offset);
1425 return -EINVAL;
1426 }
1427 }
1428
1429 /* check uuid */
1430 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1431 if (len_index >= 0) {
1432 struct definition_array *defarray;
1433 struct bt_definition *field;
1434 uint64_t i;
1435 uint8_t uuidval[BABELTRACE_UUID_LEN];
1436
1437 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1438 assert(field->declaration->id == CTF_TYPE_ARRAY);
1439 defarray = container_of(field, struct definition_array, p);
1440 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1441
1442 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1443 struct bt_definition *elem;
1444
1445 elem = bt_array_index(defarray, i);
1446 uuidval[i] = bt_get_unsigned_int(elem);
1447 }
1448 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1449 if (ret) {
1450 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1451 return -EINVAL;
1452 }
1453 }
1454
1455 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1456 if (len_index >= 0) {
1457 struct bt_definition *field;
1458
1459 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1460 stream_id = bt_get_unsigned_int(field);
1461 }
1462 }
1463
1464 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1465 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1466 stream_id,
1467 file_stream->parent.stream_id);
1468 return -EINVAL;
1469 }
1470 if (first_packet) {
1471 ret = stream_assign_class(td, file_stream, stream_id);
1472 if (ret)
1473 return ret;
1474 }
1475
1476 if (file_stream->parent.stream_packet_context) {
1477 /* Read packet context */
1478 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1479 if (ret) {
1480 if (ret == -EFAULT)
1481 goto retry;
1482 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1483 return ret;
1484 }
1485 /* read content size from header */
1486 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1487 if (len_index >= 0) {
1488 struct bt_definition *field;
1489
1490 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1491 packet_index.content_size = bt_get_unsigned_int(field);
1492 } else {
1493 /* Use file size for packet size */
1494 packet_index.content_size = filesize * CHAR_BIT;
1495 }
1496
1497 /* read packet size from header */
1498 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1499 if (len_index >= 0) {
1500 struct bt_definition *field;
1501
1502 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1503 packet_index.packet_size = bt_get_unsigned_int(field);
1504 } else {
1505 /* Use content size if non-zero, else file size */
1506 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1507 }
1508
1509 /* read timestamp begin from header */
1510 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1511 if (len_index >= 0) {
1512 struct bt_definition *field;
1513
1514 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1515 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1516 if (file_stream->parent.stream_class->trace->parent.collection) {
1517 packet_index.timestamp_begin =
1518 ctf_get_real_timestamp(
1519 &file_stream->parent,
1520 packet_index.timestamp_begin);
1521 }
1522 }
1523
1524 /* read timestamp end from header */
1525 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1526 if (len_index >= 0) {
1527 struct bt_definition *field;
1528
1529 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1530 packet_index.timestamp_end = bt_get_unsigned_int(field);
1531 if (file_stream->parent.stream_class->trace->parent.collection) {
1532 packet_index.timestamp_end =
1533 ctf_get_real_timestamp(
1534 &file_stream->parent,
1535 packet_index.timestamp_end);
1536 }
1537 }
1538
1539 /* read events discarded from header */
1540 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1541 if (len_index >= 0) {
1542 struct bt_definition *field;
1543
1544 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1545 packet_index.events_discarded = bt_get_unsigned_int(field);
1546 packet_index.events_discarded_len = bt_get_int_len(field);
1547 }
1548 } else {
1549 /* Use file size for packet size */
1550 packet_index.content_size = filesize * CHAR_BIT;
1551 /* Use content size if non-zero, else file size */
1552 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1553 }
1554
1555 /* Validate content size and packet size values */
1556 if (packet_index.content_size > packet_index.packet_size) {
1557 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1558 packet_index.content_size, packet_index.packet_size);
1559 return -EINVAL;
1560 }
1561
1562 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1563 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1564 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1565 return -EINVAL;
1566 }
1567
1568 /* Save position after header and context */
1569 packet_index.data_offset = pos->offset;
1570
1571 /* add index to packet array */
1572 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1573
1574 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1575
1576 return 0;
1577
1578 /* Retry with larger mapping */
1579 retry:
1580 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1581 /*
1582 * Reached EOF, but still expecting header/context data.
1583 */
1584 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1585 return -EFAULT;
1586 }
1587 /* Double the mapping len, and retry */
1588 tmp_map_len = packet_map_len << 1;
1589 if (tmp_map_len >> 1 != packet_map_len) {
1590 /* Overflow */
1591 fprintf(stderr, "[error] Packet mapping length overflow\n");
1592 return -EFAULT;
1593 }
1594 packet_map_len = tmp_map_len;
1595 goto begin;
1596 }
1597
1598 static
1599 int create_stream_packet_index(struct ctf_trace *td,
1600 struct ctf_file_stream *file_stream)
1601 {
1602 struct ctf_stream_pos *pos;
1603 struct stat filestats;
1604 int ret;
1605
1606 pos = &file_stream->pos;
1607
1608 ret = fstat(pos->fd, &filestats);
1609 if (ret < 0)
1610 return ret;
1611
1612 /* Deal with empty files */
1613 if (!filestats.st_size) {
1614 if (file_stream->parent.trace_packet_header
1615 || file_stream->parent.stream_packet_context) {
1616 /*
1617 * We expect a trace packet header and/or stream packet
1618 * context. Since a trace needs to have at least one
1619 * packet, empty files are therefore not accepted.
1620 */
1621 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1622 return -EINVAL;
1623 } else {
1624 /*
1625 * Without trace packet header nor stream packet
1626 * context, a one-packet trace can indeed be empty. This
1627 * is only valid if there is only one stream class: 0.
1628 */
1629 ret = stream_assign_class(td, file_stream, 0);
1630 if (ret)
1631 return ret;
1632 return 0;
1633 }
1634 }
1635
1636 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1637 ret = create_stream_one_packet_index(pos, td, file_stream,
1638 filestats.st_size);
1639 if (ret)
1640 return ret;
1641 }
1642 return 0;
1643 }
1644
1645 static
1646 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1647 {
1648 int ret;
1649
1650 if (td->packet_header_decl) {
1651 struct bt_definition *definition =
1652 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1653 stream->parent_def_scope, 0, 0, "trace.packet.header");
1654 if (!definition) {
1655 ret = -EINVAL;
1656 goto error;
1657 }
1658 stream->trace_packet_header =
1659 container_of(definition, struct definition_struct, p);
1660 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1661 }
1662
1663 return 0;
1664
1665 error:
1666 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
1667 return ret;
1668 }
1669
1670 /*
1671 * Note: many file streams can inherit from the same stream class
1672 * description (metadata).
1673 */
1674 static
1675 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1676 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1677 int whence))
1678 {
1679 int ret, fd, closeret;
1680 struct ctf_file_stream *file_stream;
1681 struct stat statbuf;
1682
1683 fd = openat(td->dirfd, path, flags);
1684 if (fd < 0) {
1685 perror("File stream openat()");
1686 ret = fd;
1687 goto error;
1688 }
1689
1690 /* Don't try to mmap subdirectories. Skip them, return success. */
1691 ret = fstat(fd, &statbuf);
1692 if (ret) {
1693 perror("File stream fstat()");
1694 goto fstat_error;
1695 }
1696 if (S_ISDIR(statbuf.st_mode)) {
1697 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1698 ret = 0;
1699 goto fd_is_dir_ok;
1700 }
1701
1702 file_stream = g_new0(struct ctf_file_stream, 1);
1703 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1704
1705 strncpy(file_stream->parent.path, path, PATH_MAX);
1706 file_stream->parent.path[PATH_MAX - 1] = '\0';
1707
1708 if (packet_seek) {
1709 file_stream->pos.packet_seek = packet_seek;
1710 } else {
1711 fprintf(stderr, "[error] packet_seek function undefined.\n");
1712 ret = -1;
1713 goto error_def;
1714 }
1715
1716 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
1717 if (ret)
1718 goto error_def;
1719 ret = create_trace_definitions(td, &file_stream->parent);
1720 if (ret)
1721 goto error_def;
1722 /*
1723 * For now, only a single clock per trace is supported.
1724 */
1725 file_stream->parent.current_clock = td->parent.single_clock;
1726 ret = create_stream_packet_index(td, file_stream);
1727 if (ret) {
1728 fprintf(stderr, "[error] Stream index creation error.\n");
1729 goto error_index;
1730 }
1731 /* Add stream file to stream class */
1732 g_ptr_array_add(file_stream->parent.stream_class->streams,
1733 &file_stream->parent);
1734 return 0;
1735
1736 error_index:
1737 if (file_stream->parent.trace_packet_header)
1738 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1739 error_def:
1740 closeret = ctf_fini_pos(&file_stream->pos);
1741 if (closeret) {
1742 fprintf(stderr, "Error on ctf_fini_pos\n");
1743 }
1744 g_free(file_stream);
1745 fd_is_dir_ok:
1746 fstat_error:
1747 closeret = close(fd);
1748 if (closeret) {
1749 perror("Error on fd close");
1750 }
1751 error:
1752 return ret;
1753 }
1754
1755 static
1756 int ctf_open_trace_read(struct ctf_trace *td,
1757 const char *path, int flags,
1758 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1759 int whence), FILE *metadata_fp)
1760 {
1761 int ret, closeret;
1762 struct dirent *dirent;
1763 struct dirent *diriter;
1764 size_t dirent_len;
1765
1766 td->flags = flags;
1767
1768 /* Open trace directory */
1769 td->dir = opendir(path);
1770 if (!td->dir) {
1771 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1772 ret = -ENOENT;
1773 goto error;
1774 }
1775
1776 td->dirfd = open(path, 0);
1777 if (td->dirfd < 0) {
1778 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1779 perror("Trace directory open");
1780 ret = -errno;
1781 goto error_dirfd;
1782 }
1783 strncpy(td->parent.path, path, sizeof(td->parent.path));
1784 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
1785
1786 /*
1787 * Keep the metadata file separate.
1788 */
1789
1790 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1791 if (ret) {
1792 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1793 goto error_metadata;
1794 }
1795
1796 /*
1797 * Open each stream: for each file, try to open, check magic
1798 * number, and get the stream ID to add to the right location in
1799 * the stream array.
1800 */
1801
1802 dirent_len = offsetof(struct dirent, d_name) +
1803 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1804
1805 dirent = malloc(dirent_len);
1806
1807 for (;;) {
1808 ret = readdir_r(td->dir, dirent, &diriter);
1809 if (ret) {
1810 fprintf(stderr, "[error] Readdir error.\n");
1811 goto readdir_error;
1812 }
1813 if (!diriter)
1814 break;
1815 /* Ignore hidden files, ., .. and metadata. */
1816 if (!strncmp(diriter->d_name, ".", 1)
1817 || !strcmp(diriter->d_name, "..")
1818 || !strcmp(diriter->d_name, "metadata"))
1819 continue;
1820 ret = ctf_open_file_stream_read(td, diriter->d_name,
1821 flags, packet_seek);
1822 if (ret) {
1823 fprintf(stderr, "[error] Open file stream error.\n");
1824 goto readdir_error;
1825 }
1826 }
1827
1828 free(dirent);
1829 return 0;
1830
1831 readdir_error:
1832 free(dirent);
1833 error_metadata:
1834 closeret = close(td->dirfd);
1835 if (closeret) {
1836 perror("Error on fd close");
1837 }
1838 error_dirfd:
1839 closeret = closedir(td->dir);
1840 if (closeret) {
1841 perror("Error on closedir");
1842 }
1843 error:
1844 return ret;
1845 }
1846
1847 /*
1848 * ctf_open_trace: Open a CTF trace and index it.
1849 * Note that the user must seek the trace after the open (using the iterator)
1850 * since the index creation read it entirely.
1851 */
1852 static
1853 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1854 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1855 int whence), FILE *metadata_fp)
1856 {
1857 struct ctf_trace *td;
1858 int ret;
1859
1860 /*
1861 * If packet_seek is NULL, we provide our default version.
1862 */
1863 if (!packet_seek)
1864 packet_seek = ctf_packet_seek;
1865
1866 td = g_new0(struct ctf_trace, 1);
1867
1868 switch (flags & O_ACCMODE) {
1869 case O_RDONLY:
1870 if (!path) {
1871 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1872 goto error;
1873 }
1874 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1875 if (ret)
1876 goto error;
1877 break;
1878 case O_RDWR:
1879 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1880 goto error;
1881 default:
1882 fprintf(stderr, "[error] Incorrect open flags.\n");
1883 goto error;
1884 }
1885
1886 return &td->parent;
1887 error:
1888 g_free(td);
1889 return NULL;
1890 }
1891
1892 static
1893 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1894 struct bt_mmap_stream *mmap_info)
1895 {
1896 pos->mmap_offset = 0;
1897 pos->packet_size = 0;
1898 pos->content_size = 0;
1899 pos->content_size_loc = NULL;
1900 pos->fd = mmap_info->fd;
1901 pos->base_mma = NULL;
1902 pos->offset = 0;
1903 pos->dummy = false;
1904 pos->cur_index = 0;
1905 pos->packet_cycles_index = NULL;
1906 pos->packet_real_index = NULL;
1907 pos->prot = PROT_READ;
1908 pos->flags = MAP_PRIVATE;
1909 pos->parent.rw_table = read_dispatch_table;
1910 pos->parent.event_cb = ctf_read_event;
1911 }
1912
1913 static
1914 int prepare_mmap_stream_definition(struct ctf_trace *td,
1915 struct ctf_file_stream *file_stream)
1916 {
1917 struct ctf_stream_declaration *stream;
1918 uint64_t stream_id = 0;
1919 int ret;
1920
1921 file_stream->parent.stream_id = stream_id;
1922 if (stream_id >= td->streams->len) {
1923 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1924 "in metadata.\n", stream_id);
1925 ret = -EINVAL;
1926 goto end;
1927 }
1928 stream = g_ptr_array_index(td->streams, stream_id);
1929 if (!stream) {
1930 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1931 "in metadata.\n", stream_id);
1932 ret = -EINVAL;
1933 goto end;
1934 }
1935 file_stream->parent.stream_class = stream;
1936 ret = create_stream_definitions(td, &file_stream->parent);
1937 end:
1938 return ret;
1939 }
1940
1941 static
1942 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1943 struct bt_mmap_stream *mmap_info,
1944 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1945 int whence))
1946 {
1947 int ret;
1948 struct ctf_file_stream *file_stream;
1949
1950 file_stream = g_new0(struct ctf_file_stream, 1);
1951 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1952 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1953
1954 file_stream->pos.packet_seek = packet_seek;
1955
1956 ret = create_trace_definitions(td, &file_stream->parent);
1957 if (ret) {
1958 goto error_def;
1959 }
1960
1961 ret = prepare_mmap_stream_definition(td, file_stream);
1962 if (ret)
1963 goto error_index;
1964
1965 /*
1966 * For now, only a single clock per trace is supported.
1967 */
1968 file_stream->parent.current_clock = td->parent.single_clock;
1969
1970 /* Add stream file to stream class */
1971 g_ptr_array_add(file_stream->parent.stream_class->streams,
1972 &file_stream->parent);
1973 return 0;
1974
1975 error_index:
1976 if (file_stream->parent.trace_packet_header)
1977 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1978 error_def:
1979 g_free(file_stream);
1980 return ret;
1981 }
1982
1983 static
1984 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1985 struct bt_mmap_stream_list *mmap_list,
1986 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1987 int whence),
1988 FILE *metadata_fp)
1989 {
1990 int ret;
1991 struct bt_mmap_stream *mmap_info;
1992
1993 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1994 if (ret) {
1995 goto error;
1996 }
1997
1998 /*
1999 * for each stream, try to open, check magic number, and get the
2000 * stream ID to add to the right location in the stream array.
2001 */
2002 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2003 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2004 if (ret) {
2005 fprintf(stderr, "[error] Open file mmap stream error.\n");
2006 goto error;
2007 }
2008 }
2009
2010 return 0;
2011
2012 error:
2013 return ret;
2014 }
2015
2016 static
2017 struct bt_trace_descriptor *ctf_open_mmap_trace(
2018 struct bt_mmap_stream_list *mmap_list,
2019 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2020 int whence),
2021 FILE *metadata_fp)
2022 {
2023 struct ctf_trace *td;
2024 int ret;
2025
2026 if (!metadata_fp) {
2027 fprintf(stderr, "[error] No metadata file pointer associated, "
2028 "required for mmap parsing\n");
2029 goto error;
2030 }
2031 if (!packet_seek) {
2032 fprintf(stderr, "[error] packet_seek function undefined.\n");
2033 goto error;
2034 }
2035 td = g_new0(struct ctf_trace, 1);
2036 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2037 if (ret)
2038 goto error_free;
2039
2040 return &td->parent;
2041
2042 error_free:
2043 g_free(td);
2044 error:
2045 return NULL;
2046 }
2047
2048 static
2049 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2050 {
2051 int i, j, k;
2052 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2053
2054 /* for each stream_class */
2055 for (i = 0; i < td->streams->len; i++) {
2056 struct ctf_stream_declaration *stream_class;
2057
2058 stream_class = g_ptr_array_index(td->streams, i);
2059 if (!stream_class)
2060 continue;
2061 /* for each file_stream */
2062 for (j = 0; j < stream_class->streams->len; j++) {
2063 struct ctf_stream_definition *stream;
2064 struct ctf_stream_pos *stream_pos;
2065 struct ctf_file_stream *cfs;
2066
2067 stream = g_ptr_array_index(stream_class->streams, j);
2068 if (!stream)
2069 continue;
2070 cfs = container_of(stream, struct ctf_file_stream,
2071 parent);
2072 stream_pos = &cfs->pos;
2073 if (!stream_pos->packet_cycles_index)
2074 continue;
2075
2076 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2077 struct packet_index *index;
2078 struct packet_index new_index;
2079
2080 index = &g_array_index(stream_pos->packet_cycles_index,
2081 struct packet_index, k);
2082 memcpy(&new_index, index,
2083 sizeof(struct packet_index));
2084 new_index.timestamp_begin =
2085 ctf_get_real_timestamp(stream,
2086 index->timestamp_begin);
2087 new_index.timestamp_end =
2088 ctf_get_real_timestamp(stream,
2089 index->timestamp_end);
2090 g_array_append_val(stream_pos->packet_real_index,
2091 new_index);
2092 }
2093 }
2094 }
2095 return 0;
2096 }
2097
2098 static
2099 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2100 {
2101 int ret;
2102
2103 ret = ctf_fini_pos(&file_stream->pos);
2104 if (ret) {
2105 fprintf(stderr, "Error on ctf_fini_pos\n");
2106 return -1;
2107 }
2108 ret = close(file_stream->pos.fd);
2109 if (ret) {
2110 perror("Error closing file fd");
2111 return -1;
2112 }
2113 return 0;
2114 }
2115
2116 static
2117 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2118 {
2119 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2120 int ret;
2121
2122 if (td->streams) {
2123 int i;
2124
2125 for (i = 0; i < td->streams->len; i++) {
2126 struct ctf_stream_declaration *stream;
2127 int j;
2128
2129 stream = g_ptr_array_index(td->streams, i);
2130 if (!stream)
2131 continue;
2132 for (j = 0; j < stream->streams->len; j++) {
2133 struct ctf_file_stream *file_stream;
2134 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2135 struct ctf_file_stream, parent);
2136 ret = ctf_close_file_stream(file_stream);
2137 if (ret)
2138 return ret;
2139 }
2140 }
2141 }
2142 ctf_destroy_metadata(td);
2143 ret = close(td->dirfd);
2144 if (ret) {
2145 perror("Error closing dirfd");
2146 return ret;
2147 }
2148 ret = closedir(td->dir);
2149 if (ret) {
2150 perror("Error closedir");
2151 return ret;
2152 }
2153 free(td->metadata_string);
2154 g_free(td);
2155 return 0;
2156 }
2157
2158 static
2159 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2160 struct bt_context *ctx)
2161 {
2162 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2163 parent);
2164
2165 td->parent.ctx = ctx;
2166 }
2167
2168 static
2169 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2170 struct bt_trace_handle *handle)
2171 {
2172 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2173 parent);
2174
2175 td->parent.handle = handle;
2176 }
2177
2178 static
2179 void __attribute__((constructor)) ctf_init(void)
2180 {
2181 int ret;
2182
2183 ctf_format.name = g_quark_from_static_string("ctf");
2184 ret = bt_register_format(&ctf_format);
2185 assert(!ret);
2186 }
2187
2188 static
2189 void __attribute__((destructor)) ctf_exit(void)
2190 {
2191 bt_unregister_format(&ctf_format);
2192 }
This page took 0.118774 seconds and 5 git commands to generate.