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