Fix: reversed logic in packet vs content size
[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 if (pos->last_offset == pos->offset) {
552 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
553 return -EINVAL;
554 }
555
556 return 0;
557
558 error:
559 fprintf(stderr, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
560 return ret;
561 }
562
563 static
564 int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
565 {
566 struct ctf_stream_declaration *stream_class = stream->stream_class;
567 struct ctf_event_definition *event;
568 uint64_t id;
569 int ret;
570
571 id = stream->event_id;
572
573 /* print event header */
574 if (likely(stream->stream_event_header)) {
575 ret = generic_rw(pos, &stream->stream_event_header->p);
576 if (ret)
577 goto error;
578 }
579
580 /* print stream-declared event context */
581 if (stream->stream_event_context) {
582 ret = generic_rw(pos, &stream->stream_event_context->p);
583 if (ret)
584 goto error;
585 }
586
587 if (unlikely(id >= stream_class->events_by_id->len)) {
588 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
589 return -EINVAL;
590 }
591 event = g_ptr_array_index(stream->events_by_id, id);
592 if (unlikely(!event)) {
593 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
594 return -EINVAL;
595 }
596
597 /* print event-declared event context */
598 if (event->event_context) {
599 ret = generic_rw(pos, &event->event_context->p);
600 if (ret)
601 goto error;
602 }
603
604 /* Read and print event payload */
605 if (likely(event->event_fields)) {
606 ret = generic_rw(pos, &event->event_fields->p);
607 if (ret)
608 goto error;
609 }
610
611 return 0;
612
613 error:
614 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
615 return ret;
616 }
617
618 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
619 int fd, int open_flags)
620 {
621 pos->fd = fd;
622 if (fd >= 0) {
623 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
624 sizeof(struct packet_index));
625 pos->packet_real_index = g_array_new(FALSE, TRUE,
626 sizeof(struct packet_index));
627 } else {
628 pos->packet_cycles_index = NULL;
629 pos->packet_real_index = NULL;
630 }
631 switch (open_flags & O_ACCMODE) {
632 case O_RDONLY:
633 pos->prot = PROT_READ;
634 pos->flags = MAP_PRIVATE;
635 pos->parent.rw_table = read_dispatch_table;
636 pos->parent.event_cb = ctf_read_event;
637 pos->parent.trace = trace;
638 break;
639 case O_RDWR:
640 pos->prot = PROT_WRITE; /* Write has priority */
641 pos->flags = MAP_SHARED;
642 pos->parent.rw_table = write_dispatch_table;
643 pos->parent.event_cb = ctf_write_event;
644 pos->parent.trace = trace;
645 if (fd >= 0)
646 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
647 break;
648 default:
649 assert(0);
650 }
651 return 0;
652 }
653
654 int ctf_fini_pos(struct ctf_stream_pos *pos)
655 {
656 if (pos->prot == PROT_WRITE && pos->content_size_loc)
657 *pos->content_size_loc = pos->offset;
658 if (pos->base_mma) {
659 int ret;
660
661 /* unmap old base */
662 ret = munmap_align(pos->base_mma);
663 if (ret) {
664 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
665 strerror(errno));
666 return -1;
667 }
668 }
669 if (pos->packet_cycles_index)
670 (void) g_array_free(pos->packet_cycles_index, TRUE);
671 if (pos->packet_real_index)
672 (void) g_array_free(pos->packet_real_index, TRUE);
673 return 0;
674 }
675
676 /*
677 * for SEEK_CUR: go to next packet.
678 * for SEEK_SET: go to packet numer (index).
679 */
680 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
681 {
682 struct ctf_stream_pos *pos =
683 container_of(stream_pos, struct ctf_stream_pos, parent);
684 struct ctf_file_stream *file_stream =
685 container_of(pos, struct ctf_file_stream, pos);
686 int ret;
687 off_t off;
688 struct packet_index *packet_index;
689
690 switch (whence) {
691 case SEEK_CUR:
692 case SEEK_SET: /* Fall-through */
693 break; /* OK */
694 default:
695 assert(0);
696 }
697
698 if (pos->prot == PROT_WRITE && pos->content_size_loc)
699 *pos->content_size_loc = pos->offset;
700
701 if (pos->base_mma) {
702 /* unmap old base */
703 ret = munmap_align(pos->base_mma);
704 if (ret) {
705 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
706 strerror(errno));
707 assert(0);
708 }
709 pos->base_mma = NULL;
710 }
711
712 /*
713 * The caller should never ask for ctf_move_pos across packets,
714 * except to get exactly at the beginning of the next packet.
715 */
716 if (pos->prot == PROT_WRITE) {
717 switch (whence) {
718 case SEEK_CUR:
719 /* The writer will add padding */
720 pos->mmap_offset += pos->packet_size / CHAR_BIT;
721 break;
722 case SEEK_SET:
723 assert(index == 0); /* only seek supported for now */
724 pos->cur_index = 0;
725 break;
726 default:
727 assert(0);
728 }
729 pos->content_size = -1U; /* Unknown at this point */
730 pos->packet_size = WRITE_PACKET_LEN;
731 off = posix_fallocate(pos->fd, pos->mmap_offset,
732 pos->packet_size / CHAR_BIT);
733 assert(off >= 0);
734 pos->offset = 0;
735 } else {
736 read_next_packet:
737 switch (whence) {
738 case SEEK_CUR:
739 {
740 uint64_t events_discarded_diff;
741
742 if (pos->offset == EOF) {
743 return;
744 }
745 assert(pos->cur_index < pos->packet_cycles_index->len);
746 assert(pos->cur_index < pos->packet_real_index->len);
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 if (index >= pos->packet_cycles_index->len) {
786 pos->offset = EOF;
787 return;
788 }
789 packet_index = &g_array_index(pos->packet_cycles_index,
790 struct packet_index, index);
791 pos->last_events_discarded = packet_index->events_discarded;
792 pos->cur_index = index;
793 file_stream->parent.prev_real_timestamp = 0;
794 file_stream->parent.prev_real_timestamp_end = 0;
795 file_stream->parent.prev_cycles_timestamp = 0;
796 file_stream->parent.prev_cycles_timestamp_end = 0;
797 break;
798 default:
799 assert(0);
800 }
801 if (pos->cur_index >= pos->packet_real_index->len) {
802 /*
803 * We need to check if we are in trace read or
804 * called from packet indexing. In this last
805 * case, the collection is not there, so we
806 * cannot print the timestamps.
807 */
808 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
809 /*
810 * When a stream reaches the end of the
811 * file, we need to show the number of
812 * events discarded ourselves, because
813 * there is no next event scheduled to
814 * be printed in the output.
815 */
816 if (file_stream->parent.events_discarded) {
817 fflush(stdout);
818 ctf_print_discarded(stderr,
819 &file_stream->parent,
820 1);
821 file_stream->parent.events_discarded = 0;
822 }
823 }
824 pos->offset = EOF;
825 return;
826 }
827 packet_index = &g_array_index(pos->packet_cycles_index,
828 struct packet_index,
829 pos->cur_index);
830 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
831
832 packet_index = &g_array_index(pos->packet_real_index,
833 struct packet_index,
834 pos->cur_index);
835 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
836 pos->mmap_offset = packet_index->offset;
837
838 /* Lookup context/packet size in index */
839 pos->content_size = packet_index->content_size;
840 pos->packet_size = packet_index->packet_size;
841 if (packet_index->data_offset < packet_index->content_size) {
842 pos->offset = 0; /* will read headers */
843 } else if (packet_index->data_offset == packet_index->content_size) {
844 /* empty packet */
845 pos->offset = packet_index->data_offset;
846 whence = SEEK_CUR;
847 goto read_next_packet;
848 } else {
849 pos->offset = EOF;
850 return;
851 }
852 }
853 /* map new base. Need mapping length from header. */
854 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
855 pos->flags, pos->fd, pos->mmap_offset);
856 if (pos->base_mma == MAP_FAILED) {
857 fprintf(stderr, "[error] mmap error %s.\n",
858 strerror(errno));
859 assert(0);
860 }
861
862 /* update trace_packet_header and stream_packet_context */
863 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
864 /* Read packet header */
865 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
866 assert(!ret);
867 }
868 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
869 /* Read packet context */
870 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
871 assert(!ret);
872 }
873 }
874
875 static
876 int packet_metadata(struct ctf_trace *td, FILE *fp)
877 {
878 uint32_t magic;
879 size_t len;
880 int ret = 0;
881
882 len = fread(&magic, sizeof(magic), 1, fp);
883 if (len != 1) {
884 goto end;
885 }
886 if (magic == TSDL_MAGIC) {
887 ret = 1;
888 td->byte_order = BYTE_ORDER;
889 CTF_TRACE_SET_FIELD(td, byte_order);
890 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
891 ret = 1;
892 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
893 LITTLE_ENDIAN : BIG_ENDIAN;
894 CTF_TRACE_SET_FIELD(td, byte_order);
895 }
896 end:
897 rewind(fp);
898 return ret;
899 }
900
901 /*
902 * Returns 0 on success, -1 on error.
903 */
904 static
905 int check_version(unsigned int major, unsigned int minor)
906 {
907 switch (major) {
908 case 1:
909 switch (minor) {
910 case 8:
911 return 0;
912 default:
913 goto warning;
914 }
915 default:
916 goto warning;
917
918 }
919
920 /* eventually return an error instead of warning */
921 warning:
922 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
923 major, minor);
924 return 0;
925 }
926
927 static
928 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
929 FILE *out)
930 {
931 struct metadata_packet_header header;
932 size_t readlen, writelen, toread;
933 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
934 int ret = 0;
935
936 readlen = fread(&header, header_sizeof(header), 1, in);
937 if (readlen < 1)
938 return -EINVAL;
939
940 if (td->byte_order != BYTE_ORDER) {
941 header.magic = GUINT32_SWAP_LE_BE(header.magic);
942 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
943 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
944 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
945 }
946 if (header.checksum)
947 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
948 if (header.compression_scheme) {
949 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
950 header.compression_scheme);
951 return -EINVAL;
952 }
953 if (header.encryption_scheme) {
954 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
955 header.encryption_scheme);
956 return -EINVAL;
957 }
958 if (header.checksum_scheme) {
959 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
960 header.checksum_scheme);
961 return -EINVAL;
962 }
963 if (check_version(header.major, header.minor) < 0)
964 return -EINVAL;
965 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
966 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
967 CTF_TRACE_SET_FIELD(td, uuid);
968 } else {
969 if (babeltrace_uuid_compare(header.uuid, td->uuid))
970 return -EINVAL;
971 }
972
973 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
974 return -EINVAL;
975
976 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
977
978 for (;;) {
979 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
980 if (ferror(in)) {
981 ret = -EINVAL;
982 break;
983 }
984 if (babeltrace_debug) {
985 buf[readlen] = '\0';
986 fprintf(stderr, "[debug] metadata packet read: %s\n",
987 buf);
988 }
989
990 writelen = fwrite(buf, sizeof(char), readlen, out);
991 if (writelen < readlen) {
992 ret = -EIO;
993 break;
994 }
995 if (ferror(out)) {
996 ret = -EINVAL;
997 break;
998 }
999 toread -= readlen;
1000 if (!toread) {
1001 ret = 0; /* continue reading next packet */
1002 goto read_padding;
1003 }
1004 }
1005 return ret;
1006
1007 read_padding:
1008 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1009 ret = fseek(in, toread, SEEK_CUR);
1010 if (ret < 0) {
1011 fprintf(stderr, "[warning] Missing padding at end of file\n");
1012 ret = 0;
1013 }
1014 return ret;
1015 }
1016
1017 static
1018 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1019 char **buf)
1020 {
1021 FILE *in, *out;
1022 size_t size, buflen;
1023 int ret;
1024
1025 in = *fp;
1026 /*
1027 * Using strlen on *buf instead of size of open_memstream
1028 * because its size includes garbage at the end (after final
1029 * \0). This is the allocated size, not the actual string size.
1030 */
1031 out = babeltrace_open_memstream(buf, &size);
1032 if (out == NULL) {
1033 perror("Metadata open_memstream");
1034 return -errno;
1035 }
1036 for (;;) {
1037 ret = ctf_open_trace_metadata_packet_read(td, in, out);
1038 if (ret) {
1039 break;
1040 }
1041 if (feof(in)) {
1042 ret = 0;
1043 break;
1044 }
1045 }
1046 /* close to flush the buffer */
1047 ret = babeltrace_close_memstream(buf, &size, out);
1048 if (ret < 0) {
1049 int closeret;
1050
1051 perror("babeltrace_flush_memstream");
1052 ret = -errno;
1053 closeret = fclose(in);
1054 if (closeret) {
1055 perror("Error in fclose");
1056 }
1057 return ret;
1058 }
1059 ret = fclose(in);
1060 if (ret) {
1061 perror("Error in fclose");
1062 }
1063 /* open for reading */
1064 buflen = strlen(*buf);
1065 if (!buflen) {
1066 *fp = NULL;
1067 return -ENOENT;
1068 }
1069 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1070 if (!*fp) {
1071 perror("Metadata fmemopen");
1072 return -errno;
1073 }
1074 return 0;
1075 }
1076
1077 static
1078 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1079 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1080 int whence), FILE *metadata_fp)
1081 {
1082 struct ctf_scanner *scanner;
1083 struct ctf_file_stream *metadata_stream;
1084 FILE *fp;
1085 char *buf = NULL;
1086 int ret = 0, closeret;
1087
1088 metadata_stream = g_new0(struct ctf_file_stream, 1);
1089 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1090
1091 if (packet_seek) {
1092 metadata_stream->pos.packet_seek = packet_seek;
1093 } else {
1094 fprintf(stderr, "[error] packet_seek function undefined.\n");
1095 ret = -1;
1096 goto end_free;
1097 }
1098
1099 if (metadata_fp) {
1100 fp = metadata_fp;
1101 metadata_stream->pos.fd = -1;
1102 } else {
1103 td->metadata = &metadata_stream->parent;
1104 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1105 if (metadata_stream->pos.fd < 0) {
1106 fprintf(stderr, "Unable to open metadata.\n");
1107 ret = -1;
1108 goto end_free;
1109 }
1110
1111 fp = fdopen(metadata_stream->pos.fd, "r");
1112 if (!fp) {
1113 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1114 perror("Metadata stream open");
1115 ret = -errno;
1116 goto end_stream;
1117 }
1118 /* fd now belongs to fp */
1119 metadata_stream->pos.fd = -1;
1120 }
1121 if (babeltrace_debug)
1122 yydebug = 1;
1123
1124 if (packet_metadata(td, fp)) {
1125 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1126 if (ret) {
1127 /* Warn about empty metadata */
1128 fprintf(stderr, "[warning] Empty metadata.\n");
1129 goto end_packet_read;
1130 }
1131 td->metadata_string = buf;
1132 td->metadata_packetized = 1;
1133 } else {
1134 unsigned int major, minor;
1135 ssize_t nr_items;
1136
1137 td->byte_order = BYTE_ORDER;
1138
1139 /* Check text-only metadata header and version */
1140 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1141 if (nr_items < 2)
1142 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1143 if (check_version(major, minor) < 0) {
1144 ret = -EINVAL;
1145 goto end_packet_read;
1146 }
1147 rewind(fp);
1148 }
1149
1150 scanner = ctf_scanner_alloc(fp);
1151 if (!scanner) {
1152 fprintf(stderr, "[error] Error allocating scanner\n");
1153 ret = -ENOMEM;
1154 goto end_scanner_alloc;
1155 }
1156 ret = ctf_scanner_append_ast(scanner);
1157 if (ret) {
1158 fprintf(stderr, "[error] Error creating AST\n");
1159 goto end;
1160 }
1161
1162 if (babeltrace_debug) {
1163 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1164 if (ret) {
1165 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1166 goto end;
1167 }
1168 }
1169
1170 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1171 if (ret) {
1172 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1173 goto end;
1174 }
1175 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1176 td, td->byte_order);
1177 if (ret) {
1178 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1179 goto end;
1180 }
1181 end:
1182 ctf_scanner_free(scanner);
1183 end_scanner_alloc:
1184 end_packet_read:
1185 if (fp) {
1186 closeret = fclose(fp);
1187 if (closeret) {
1188 perror("Error on fclose");
1189 }
1190 }
1191 end_stream:
1192 if (metadata_stream->pos.fd >= 0) {
1193 closeret = close(metadata_stream->pos.fd);
1194 if (closeret) {
1195 perror("Error on metadata stream fd close");
1196 }
1197 }
1198 end_free:
1199 if (ret)
1200 g_free(metadata_stream);
1201 return ret;
1202 }
1203
1204 static
1205 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1206 struct ctf_stream_definition *stream,
1207 struct ctf_event_declaration *event)
1208 {
1209 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1210
1211 if (event->context_decl) {
1212 struct bt_definition *definition =
1213 event->context_decl->p.definition_new(&event->context_decl->p,
1214 stream->parent_def_scope, 0, 0, "event.context");
1215 if (!definition) {
1216 goto error;
1217 }
1218 stream_event->event_context = container_of(definition,
1219 struct definition_struct, p);
1220 stream->parent_def_scope = stream_event->event_context->p.scope;
1221 }
1222 if (event->fields_decl) {
1223 struct bt_definition *definition =
1224 event->fields_decl->p.definition_new(&event->fields_decl->p,
1225 stream->parent_def_scope, 0, 0, "event.fields");
1226 if (!definition) {
1227 goto error;
1228 }
1229 stream_event->event_fields = container_of(definition,
1230 struct definition_struct, p);
1231 stream->parent_def_scope = stream_event->event_fields->p.scope;
1232 }
1233 stream_event->stream = stream;
1234 return stream_event;
1235
1236 error:
1237 if (stream_event->event_fields)
1238 bt_definition_unref(&stream_event->event_fields->p);
1239 if (stream_event->event_context)
1240 bt_definition_unref(&stream_event->event_context->p);
1241 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1242 g_quark_to_string(event->name));
1243 return NULL;
1244 }
1245
1246 static
1247 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1248 {
1249 struct ctf_stream_declaration *stream_class;
1250 int ret;
1251 int i;
1252
1253 if (stream->stream_definitions_created)
1254 return 0;
1255
1256 stream_class = stream->stream_class;
1257
1258 if (stream_class->packet_context_decl) {
1259 struct bt_definition *definition =
1260 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1261 stream->parent_def_scope, 0, 0, "stream.packet.context");
1262 if (!definition) {
1263 ret = -EINVAL;
1264 goto error;
1265 }
1266 stream->stream_packet_context = container_of(definition,
1267 struct definition_struct, p);
1268 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1269 }
1270 if (stream_class->event_header_decl) {
1271 struct bt_definition *definition =
1272 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1273 stream->parent_def_scope, 0, 0, "stream.event.header");
1274 if (!definition) {
1275 ret = -EINVAL;
1276 goto error;
1277 }
1278 stream->stream_event_header =
1279 container_of(definition, struct definition_struct, p);
1280 stream->parent_def_scope = stream->stream_event_header->p.scope;
1281 }
1282 if (stream_class->event_context_decl) {
1283 struct bt_definition *definition =
1284 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1285 stream->parent_def_scope, 0, 0, "stream.event.context");
1286 if (!definition) {
1287 ret = -EINVAL;
1288 goto error;
1289 }
1290 stream->stream_event_context =
1291 container_of(definition, struct definition_struct, p);
1292 stream->parent_def_scope = stream->stream_event_context->p.scope;
1293 }
1294 stream->events_by_id = g_ptr_array_new();
1295 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1296 for (i = 0; i < stream->events_by_id->len; i++) {
1297 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1298 struct ctf_event_definition *stream_event;
1299
1300 if (!event)
1301 continue;
1302 stream_event = create_event_definitions(td, stream, event);
1303 if (!stream_event) {
1304 ret = -EINVAL;
1305 goto error_event;
1306 }
1307 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1308 }
1309 return 0;
1310
1311 error_event:
1312 for (i = 0; i < stream->events_by_id->len; i++) {
1313 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1314 if (stream_event)
1315 g_free(stream_event);
1316 }
1317 g_ptr_array_free(stream->events_by_id, TRUE);
1318 error:
1319 if (stream->stream_event_context)
1320 bt_definition_unref(&stream->stream_event_context->p);
1321 if (stream->stream_event_header)
1322 bt_definition_unref(&stream->stream_event_header->p);
1323 if (stream->stream_packet_context)
1324 bt_definition_unref(&stream->stream_packet_context->p);
1325 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1326 stream_class->stream_id, strerror(-ret));
1327 return ret;
1328 }
1329
1330 static
1331 int stream_assign_class(struct ctf_trace *td,
1332 struct ctf_file_stream *file_stream,
1333 uint64_t stream_id)
1334 {
1335 struct ctf_stream_declaration *stream;
1336 int ret;
1337
1338 file_stream->parent.stream_id = stream_id;
1339 if (stream_id >= td->streams->len) {
1340 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1341 return -EINVAL;
1342 }
1343 stream = g_ptr_array_index(td->streams, stream_id);
1344 if (!stream) {
1345 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1346 return -EINVAL;
1347 }
1348 file_stream->parent.stream_class = stream;
1349 ret = create_stream_definitions(td, &file_stream->parent);
1350 if (ret)
1351 return ret;
1352 return 0;
1353 }
1354
1355 static
1356 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1357 struct ctf_trace *td,
1358 struct ctf_file_stream *file_stream,
1359 size_t filesize)
1360 {
1361 struct packet_index packet_index;
1362 uint64_t stream_id = 0;
1363 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1364 int first_packet = 0;
1365 int len_index;
1366 int ret;
1367
1368 begin:
1369 if (!pos->mmap_offset) {
1370 first_packet = 1;
1371 }
1372
1373 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1374 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1375 }
1376
1377 if (pos->base_mma) {
1378 /* unmap old base */
1379 ret = munmap_align(pos->base_mma);
1380 if (ret) {
1381 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1382 strerror(errno));
1383 return ret;
1384 }
1385 pos->base_mma = NULL;
1386 }
1387 /* map new base. Need mapping length from header. */
1388 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1389 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1390 assert(pos->base_mma != MAP_FAILED);
1391 /*
1392 * Use current mapping size as temporary content and packet
1393 * size.
1394 */
1395 pos->content_size = packet_map_len;
1396 pos->packet_size = packet_map_len;
1397 pos->offset = 0; /* Position of the packet header */
1398
1399 packet_index.offset = pos->mmap_offset;
1400 packet_index.content_size = 0;
1401 packet_index.packet_size = 0;
1402 packet_index.timestamp_begin = 0;
1403 packet_index.timestamp_end = 0;
1404 packet_index.events_discarded = 0;
1405 packet_index.events_discarded_len = 0;
1406
1407 /* read and check header, set stream id (and check) */
1408 if (file_stream->parent.trace_packet_header) {
1409 /* Read packet header */
1410 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1411 if (ret) {
1412 if (ret == -EFAULT)
1413 goto retry;
1414 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1415 return ret;
1416 }
1417 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1418 if (len_index >= 0) {
1419 struct bt_definition *field;
1420 uint64_t magic;
1421
1422 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1423 magic = bt_get_unsigned_int(field);
1424 if (magic != CTF_MAGIC) {
1425 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1426 magic,
1427 file_stream->pos.packet_cycles_index->len,
1428 (ssize_t) pos->mmap_offset);
1429 return -EINVAL;
1430 }
1431 }
1432
1433 /* check uuid */
1434 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1435 if (len_index >= 0) {
1436 struct definition_array *defarray;
1437 struct bt_definition *field;
1438 uint64_t i;
1439 uint8_t uuidval[BABELTRACE_UUID_LEN];
1440
1441 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1442 assert(field->declaration->id == CTF_TYPE_ARRAY);
1443 defarray = container_of(field, struct definition_array, p);
1444 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1445
1446 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1447 struct bt_definition *elem;
1448
1449 elem = bt_array_index(defarray, i);
1450 uuidval[i] = bt_get_unsigned_int(elem);
1451 }
1452 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1453 if (ret) {
1454 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1455 return -EINVAL;
1456 }
1457 }
1458
1459 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1460 if (len_index >= 0) {
1461 struct bt_definition *field;
1462
1463 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1464 stream_id = bt_get_unsigned_int(field);
1465 }
1466 }
1467
1468 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1469 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1470 stream_id,
1471 file_stream->parent.stream_id);
1472 return -EINVAL;
1473 }
1474 if (first_packet) {
1475 ret = stream_assign_class(td, file_stream, stream_id);
1476 if (ret)
1477 return ret;
1478 }
1479
1480 if (file_stream->parent.stream_packet_context) {
1481 /* Read packet context */
1482 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1483 if (ret) {
1484 if (ret == -EFAULT)
1485 goto retry;
1486 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1487 return ret;
1488 }
1489 /* read packet size from header */
1490 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1491 if (len_index >= 0) {
1492 struct bt_definition *field;
1493
1494 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1495 packet_index.packet_size = bt_get_unsigned_int(field);
1496 } else {
1497 /* Use file size for packet size */
1498 packet_index.packet_size = filesize * CHAR_BIT;
1499 }
1500
1501 /* read content size from header */
1502 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1503 if (len_index >= 0) {
1504 struct bt_definition *field;
1505
1506 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1507 packet_index.content_size = bt_get_unsigned_int(field);
1508 } else {
1509 /* Use packet size if non-zero, else file size */
1510 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1511 }
1512
1513 /* read timestamp begin from header */
1514 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1515 if (len_index >= 0) {
1516 struct bt_definition *field;
1517
1518 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1519 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1520 if (file_stream->parent.stream_class->trace->parent.collection) {
1521 packet_index.timestamp_begin =
1522 ctf_get_real_timestamp(
1523 &file_stream->parent,
1524 packet_index.timestamp_begin);
1525 }
1526 }
1527
1528 /* read timestamp end from header */
1529 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1530 if (len_index >= 0) {
1531 struct bt_definition *field;
1532
1533 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1534 packet_index.timestamp_end = bt_get_unsigned_int(field);
1535 if (file_stream->parent.stream_class->trace->parent.collection) {
1536 packet_index.timestamp_end =
1537 ctf_get_real_timestamp(
1538 &file_stream->parent,
1539 packet_index.timestamp_end);
1540 }
1541 }
1542
1543 /* read events discarded from header */
1544 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1545 if (len_index >= 0) {
1546 struct bt_definition *field;
1547
1548 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1549 packet_index.events_discarded = bt_get_unsigned_int(field);
1550 packet_index.events_discarded_len = bt_get_int_len(field);
1551 }
1552 } else {
1553 /* Use file size for packet size */
1554 packet_index.content_size = filesize * CHAR_BIT;
1555 /* Use content size if non-zero, else file size */
1556 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1557 }
1558
1559 /* Validate content size and packet size values */
1560 if (packet_index.content_size > packet_index.packet_size) {
1561 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1562 packet_index.content_size, packet_index.packet_size);
1563 return -EINVAL;
1564 }
1565
1566 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1567 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1568 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1569 return -EINVAL;
1570 }
1571
1572 /* Save position after header and context */
1573 packet_index.data_offset = pos->offset;
1574
1575 /* add index to packet array */
1576 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1577
1578 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1579
1580 return 0;
1581
1582 /* Retry with larger mapping */
1583 retry:
1584 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1585 /*
1586 * Reached EOF, but still expecting header/context data.
1587 */
1588 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1589 return -EFAULT;
1590 }
1591 /* Double the mapping len, and retry */
1592 tmp_map_len = packet_map_len << 1;
1593 if (tmp_map_len >> 1 != packet_map_len) {
1594 /* Overflow */
1595 fprintf(stderr, "[error] Packet mapping length overflow\n");
1596 return -EFAULT;
1597 }
1598 packet_map_len = tmp_map_len;
1599 goto begin;
1600 }
1601
1602 static
1603 int create_stream_packet_index(struct ctf_trace *td,
1604 struct ctf_file_stream *file_stream)
1605 {
1606 struct ctf_stream_pos *pos;
1607 struct stat filestats;
1608 int ret;
1609
1610 pos = &file_stream->pos;
1611
1612 ret = fstat(pos->fd, &filestats);
1613 if (ret < 0)
1614 return ret;
1615
1616 /* Deal with empty files */
1617 if (!filestats.st_size) {
1618 if (file_stream->parent.trace_packet_header
1619 || file_stream->parent.stream_packet_context) {
1620 /*
1621 * We expect a trace packet header and/or stream packet
1622 * context. Since a trace needs to have at least one
1623 * packet, empty files are therefore not accepted.
1624 */
1625 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1626 return -EINVAL;
1627 } else {
1628 /*
1629 * Without trace packet header nor stream packet
1630 * context, a one-packet trace can indeed be empty. This
1631 * is only valid if there is only one stream class: 0.
1632 */
1633 ret = stream_assign_class(td, file_stream, 0);
1634 if (ret)
1635 return ret;
1636 return 0;
1637 }
1638 }
1639
1640 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1641 ret = create_stream_one_packet_index(pos, td, file_stream,
1642 filestats.st_size);
1643 if (ret)
1644 return ret;
1645 }
1646 return 0;
1647 }
1648
1649 static
1650 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1651 {
1652 int ret;
1653
1654 if (td->packet_header_decl) {
1655 struct bt_definition *definition =
1656 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1657 stream->parent_def_scope, 0, 0, "trace.packet.header");
1658 if (!definition) {
1659 ret = -EINVAL;
1660 goto error;
1661 }
1662 stream->trace_packet_header =
1663 container_of(definition, struct definition_struct, p);
1664 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1665 }
1666
1667 return 0;
1668
1669 error:
1670 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
1671 return ret;
1672 }
1673
1674 /*
1675 * Note: many file streams can inherit from the same stream class
1676 * description (metadata).
1677 */
1678 static
1679 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1680 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1681 int whence))
1682 {
1683 int ret, fd, closeret;
1684 struct ctf_file_stream *file_stream;
1685 struct stat statbuf;
1686
1687 fd = openat(td->dirfd, path, flags);
1688 if (fd < 0) {
1689 perror("File stream openat()");
1690 ret = fd;
1691 goto error;
1692 }
1693
1694 /* Don't try to mmap subdirectories. Skip them, return success. */
1695 ret = fstat(fd, &statbuf);
1696 if (ret) {
1697 perror("File stream fstat()");
1698 goto fstat_error;
1699 }
1700 if (S_ISDIR(statbuf.st_mode)) {
1701 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1702 ret = 0;
1703 goto fd_is_dir_ok;
1704 }
1705
1706 file_stream = g_new0(struct ctf_file_stream, 1);
1707 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1708
1709 strncpy(file_stream->parent.path, path, PATH_MAX);
1710 file_stream->parent.path[PATH_MAX - 1] = '\0';
1711
1712 if (packet_seek) {
1713 file_stream->pos.packet_seek = packet_seek;
1714 } else {
1715 fprintf(stderr, "[error] packet_seek function undefined.\n");
1716 ret = -1;
1717 goto error_def;
1718 }
1719
1720 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
1721 if (ret)
1722 goto error_def;
1723 ret = create_trace_definitions(td, &file_stream->parent);
1724 if (ret)
1725 goto error_def;
1726 /*
1727 * For now, only a single clock per trace is supported.
1728 */
1729 file_stream->parent.current_clock = td->parent.single_clock;
1730 ret = create_stream_packet_index(td, file_stream);
1731 if (ret) {
1732 fprintf(stderr, "[error] Stream index creation error.\n");
1733 goto error_index;
1734 }
1735 /* Add stream file to stream class */
1736 g_ptr_array_add(file_stream->parent.stream_class->streams,
1737 &file_stream->parent);
1738 return 0;
1739
1740 error_index:
1741 if (file_stream->parent.trace_packet_header)
1742 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1743 error_def:
1744 closeret = ctf_fini_pos(&file_stream->pos);
1745 if (closeret) {
1746 fprintf(stderr, "Error on ctf_fini_pos\n");
1747 }
1748 g_free(file_stream);
1749 fd_is_dir_ok:
1750 fstat_error:
1751 closeret = close(fd);
1752 if (closeret) {
1753 perror("Error on fd close");
1754 }
1755 error:
1756 return ret;
1757 }
1758
1759 static
1760 int ctf_open_trace_read(struct ctf_trace *td,
1761 const char *path, int flags,
1762 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1763 int whence), FILE *metadata_fp)
1764 {
1765 int ret, closeret;
1766 struct dirent *dirent;
1767 struct dirent *diriter;
1768 size_t dirent_len;
1769
1770 td->flags = flags;
1771
1772 /* Open trace directory */
1773 td->dir = opendir(path);
1774 if (!td->dir) {
1775 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1776 ret = -ENOENT;
1777 goto error;
1778 }
1779
1780 td->dirfd = open(path, 0);
1781 if (td->dirfd < 0) {
1782 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1783 perror("Trace directory open");
1784 ret = -errno;
1785 goto error_dirfd;
1786 }
1787 strncpy(td->parent.path, path, sizeof(td->parent.path));
1788 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
1789
1790 /*
1791 * Keep the metadata file separate.
1792 */
1793
1794 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1795 if (ret) {
1796 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1797 goto error_metadata;
1798 }
1799
1800 /*
1801 * Open each stream: for each file, try to open, check magic
1802 * number, and get the stream ID to add to the right location in
1803 * the stream array.
1804 */
1805
1806 dirent_len = offsetof(struct dirent, d_name) +
1807 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1808
1809 dirent = malloc(dirent_len);
1810
1811 for (;;) {
1812 ret = readdir_r(td->dir, dirent, &diriter);
1813 if (ret) {
1814 fprintf(stderr, "[error] Readdir error.\n");
1815 goto readdir_error;
1816 }
1817 if (!diriter)
1818 break;
1819 /* Ignore hidden files, ., .. and metadata. */
1820 if (!strncmp(diriter->d_name, ".", 1)
1821 || !strcmp(diriter->d_name, "..")
1822 || !strcmp(diriter->d_name, "metadata"))
1823 continue;
1824 ret = ctf_open_file_stream_read(td, diriter->d_name,
1825 flags, packet_seek);
1826 if (ret) {
1827 fprintf(stderr, "[error] Open file stream error.\n");
1828 goto readdir_error;
1829 }
1830 }
1831
1832 free(dirent);
1833 return 0;
1834
1835 readdir_error:
1836 free(dirent);
1837 error_metadata:
1838 closeret = close(td->dirfd);
1839 if (closeret) {
1840 perror("Error on fd close");
1841 }
1842 error_dirfd:
1843 closeret = closedir(td->dir);
1844 if (closeret) {
1845 perror("Error on closedir");
1846 }
1847 error:
1848 return ret;
1849 }
1850
1851 /*
1852 * ctf_open_trace: Open a CTF trace and index it.
1853 * Note that the user must seek the trace after the open (using the iterator)
1854 * since the index creation read it entirely.
1855 */
1856 static
1857 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1858 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1859 int whence), FILE *metadata_fp)
1860 {
1861 struct ctf_trace *td;
1862 int ret;
1863
1864 /*
1865 * If packet_seek is NULL, we provide our default version.
1866 */
1867 if (!packet_seek)
1868 packet_seek = ctf_packet_seek;
1869
1870 td = g_new0(struct ctf_trace, 1);
1871
1872 switch (flags & O_ACCMODE) {
1873 case O_RDONLY:
1874 if (!path) {
1875 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1876 goto error;
1877 }
1878 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1879 if (ret)
1880 goto error;
1881 break;
1882 case O_RDWR:
1883 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1884 goto error;
1885 default:
1886 fprintf(stderr, "[error] Incorrect open flags.\n");
1887 goto error;
1888 }
1889
1890 return &td->parent;
1891 error:
1892 g_free(td);
1893 return NULL;
1894 }
1895
1896 static
1897 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1898 struct bt_mmap_stream *mmap_info)
1899 {
1900 pos->mmap_offset = 0;
1901 pos->packet_size = 0;
1902 pos->content_size = 0;
1903 pos->content_size_loc = NULL;
1904 pos->fd = mmap_info->fd;
1905 pos->base_mma = NULL;
1906 pos->offset = 0;
1907 pos->dummy = false;
1908 pos->cur_index = 0;
1909 pos->packet_cycles_index = NULL;
1910 pos->packet_real_index = NULL;
1911 pos->prot = PROT_READ;
1912 pos->flags = MAP_PRIVATE;
1913 pos->parent.rw_table = read_dispatch_table;
1914 pos->parent.event_cb = ctf_read_event;
1915 }
1916
1917 static
1918 int prepare_mmap_stream_definition(struct ctf_trace *td,
1919 struct ctf_file_stream *file_stream)
1920 {
1921 struct ctf_stream_declaration *stream;
1922 uint64_t stream_id = 0;
1923 int ret;
1924
1925 file_stream->parent.stream_id = stream_id;
1926 if (stream_id >= td->streams->len) {
1927 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1928 "in metadata.\n", stream_id);
1929 ret = -EINVAL;
1930 goto end;
1931 }
1932 stream = g_ptr_array_index(td->streams, stream_id);
1933 if (!stream) {
1934 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1935 "in metadata.\n", stream_id);
1936 ret = -EINVAL;
1937 goto end;
1938 }
1939 file_stream->parent.stream_class = stream;
1940 ret = create_stream_definitions(td, &file_stream->parent);
1941 end:
1942 return ret;
1943 }
1944
1945 static
1946 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1947 struct bt_mmap_stream *mmap_info,
1948 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1949 int whence))
1950 {
1951 int ret;
1952 struct ctf_file_stream *file_stream;
1953
1954 file_stream = g_new0(struct ctf_file_stream, 1);
1955 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1956 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1957
1958 file_stream->pos.packet_seek = packet_seek;
1959
1960 ret = create_trace_definitions(td, &file_stream->parent);
1961 if (ret) {
1962 goto error_def;
1963 }
1964
1965 ret = prepare_mmap_stream_definition(td, file_stream);
1966 if (ret)
1967 goto error_index;
1968
1969 /*
1970 * For now, only a single clock per trace is supported.
1971 */
1972 file_stream->parent.current_clock = td->parent.single_clock;
1973
1974 /* Add stream file to stream class */
1975 g_ptr_array_add(file_stream->parent.stream_class->streams,
1976 &file_stream->parent);
1977 return 0;
1978
1979 error_index:
1980 if (file_stream->parent.trace_packet_header)
1981 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1982 error_def:
1983 g_free(file_stream);
1984 return ret;
1985 }
1986
1987 static
1988 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1989 struct bt_mmap_stream_list *mmap_list,
1990 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1991 int whence),
1992 FILE *metadata_fp)
1993 {
1994 int ret;
1995 struct bt_mmap_stream *mmap_info;
1996
1997 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1998 if (ret) {
1999 goto error;
2000 }
2001
2002 /*
2003 * for each stream, try to open, check magic number, and get the
2004 * stream ID to add to the right location in the stream array.
2005 */
2006 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2007 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2008 if (ret) {
2009 fprintf(stderr, "[error] Open file mmap stream error.\n");
2010 goto error;
2011 }
2012 }
2013
2014 return 0;
2015
2016 error:
2017 return ret;
2018 }
2019
2020 static
2021 struct bt_trace_descriptor *ctf_open_mmap_trace(
2022 struct bt_mmap_stream_list *mmap_list,
2023 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2024 int whence),
2025 FILE *metadata_fp)
2026 {
2027 struct ctf_trace *td;
2028 int ret;
2029
2030 if (!metadata_fp) {
2031 fprintf(stderr, "[error] No metadata file pointer associated, "
2032 "required for mmap parsing\n");
2033 goto error;
2034 }
2035 if (!packet_seek) {
2036 fprintf(stderr, "[error] packet_seek function undefined.\n");
2037 goto error;
2038 }
2039 td = g_new0(struct ctf_trace, 1);
2040 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2041 if (ret)
2042 goto error_free;
2043
2044 return &td->parent;
2045
2046 error_free:
2047 g_free(td);
2048 error:
2049 return NULL;
2050 }
2051
2052 static
2053 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2054 {
2055 int i, j, k;
2056 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2057
2058 /* for each stream_class */
2059 for (i = 0; i < td->streams->len; i++) {
2060 struct ctf_stream_declaration *stream_class;
2061
2062 stream_class = g_ptr_array_index(td->streams, i);
2063 if (!stream_class)
2064 continue;
2065 /* for each file_stream */
2066 for (j = 0; j < stream_class->streams->len; j++) {
2067 struct ctf_stream_definition *stream;
2068 struct ctf_stream_pos *stream_pos;
2069 struct ctf_file_stream *cfs;
2070
2071 stream = g_ptr_array_index(stream_class->streams, j);
2072 if (!stream)
2073 continue;
2074 cfs = container_of(stream, struct ctf_file_stream,
2075 parent);
2076 stream_pos = &cfs->pos;
2077 if (!stream_pos->packet_cycles_index)
2078 continue;
2079
2080 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2081 struct packet_index *index;
2082 struct packet_index new_index;
2083
2084 index = &g_array_index(stream_pos->packet_cycles_index,
2085 struct packet_index, k);
2086 memcpy(&new_index, index,
2087 sizeof(struct packet_index));
2088 new_index.timestamp_begin =
2089 ctf_get_real_timestamp(stream,
2090 index->timestamp_begin);
2091 new_index.timestamp_end =
2092 ctf_get_real_timestamp(stream,
2093 index->timestamp_end);
2094 g_array_append_val(stream_pos->packet_real_index,
2095 new_index);
2096 }
2097 }
2098 }
2099 return 0;
2100 }
2101
2102 static
2103 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2104 {
2105 int ret;
2106
2107 ret = ctf_fini_pos(&file_stream->pos);
2108 if (ret) {
2109 fprintf(stderr, "Error on ctf_fini_pos\n");
2110 return -1;
2111 }
2112 ret = close(file_stream->pos.fd);
2113 if (ret) {
2114 perror("Error closing file fd");
2115 return -1;
2116 }
2117 return 0;
2118 }
2119
2120 static
2121 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2122 {
2123 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2124 int ret;
2125
2126 if (td->streams) {
2127 int i;
2128
2129 for (i = 0; i < td->streams->len; i++) {
2130 struct ctf_stream_declaration *stream;
2131 int j;
2132
2133 stream = g_ptr_array_index(td->streams, i);
2134 if (!stream)
2135 continue;
2136 for (j = 0; j < stream->streams->len; j++) {
2137 struct ctf_file_stream *file_stream;
2138 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2139 struct ctf_file_stream, parent);
2140 ret = ctf_close_file_stream(file_stream);
2141 if (ret)
2142 return ret;
2143 }
2144 }
2145 }
2146 ctf_destroy_metadata(td);
2147 ret = close(td->dirfd);
2148 if (ret) {
2149 perror("Error closing dirfd");
2150 return ret;
2151 }
2152 ret = closedir(td->dir);
2153 if (ret) {
2154 perror("Error closedir");
2155 return ret;
2156 }
2157 free(td->metadata_string);
2158 g_free(td);
2159 return 0;
2160 }
2161
2162 static
2163 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2164 struct bt_context *ctx)
2165 {
2166 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2167 parent);
2168
2169 td->parent.ctx = ctx;
2170 }
2171
2172 static
2173 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2174 struct bt_trace_handle *handle)
2175 {
2176 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2177 parent);
2178
2179 td->parent.handle = handle;
2180 }
2181
2182 static
2183 void __attribute__((constructor)) ctf_init(void)
2184 {
2185 int ret;
2186
2187 ctf_format.name = g_quark_from_static_string("ctf");
2188 ret = bt_register_format(&ctf_format);
2189 assert(!ret);
2190 }
2191
2192 static
2193 void __attribute__((destructor)) ctf_exit(void)
2194 {
2195 bt_unregister_format(&ctf_format);
2196 }
This page took 0.080934 seconds and 4 git commands to generate.