fix: move read event from iterator to event.h
[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
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <inttypes.h>
27 #include <stdio.h>
28 #include <uuid/uuid.h>
29 #include <sys/mman.h>
30 #include <errno.h>
31 #include <endian.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <dirent.h>
36 #include <glib.h>
37 #include <unistd.h>
38 #include <stdlib.h>
39
40 #include "metadata/ctf-scanner.h"
41 #include "metadata/ctf-parser.h"
42 #include "metadata/ctf-ast.h"
43
44 /*
45 * We currently simply map a page to read the packet header and packet
46 * context to get the packet length and content length. (in bits)
47 */
48 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
49 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
50 #define UUID_LEN 16 /* uuid by value len */
51
52 #ifndef min
53 #define min(a, b) (((a) < (b)) ? (a) : (b))
54 #endif
55
56 #define NSEC_PER_SEC 1000000000ULL
57
58 int opt_clock_raw,
59 opt_clock_seconds,
60 opt_clock_date,
61 opt_clock_gmt;
62
63 uint64_t opt_clock_offset;
64
65 extern int yydebug;
66
67 static
68 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
69 void (*packet_seek)(struct stream_pos *pos, size_t index,
70 int whence),
71 FILE *metadata_fp);
72 static
73 struct trace_descriptor *ctf_open_mmap_trace(
74 struct mmap_stream_list *mmap_list,
75 void (*packet_seek)(struct stream_pos *pos, size_t index,
76 int whence),
77 FILE *metadata_fp);
78
79 static
80 void ctf_close_trace(struct trace_descriptor *descriptor);
81
82 static
83 rw_dispatch read_dispatch_table[] = {
84 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
85 [ CTF_TYPE_FLOAT ] = ctf_float_read,
86 [ CTF_TYPE_ENUM ] = ctf_enum_read,
87 [ CTF_TYPE_STRING ] = ctf_string_read,
88 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
89 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
90 [ CTF_TYPE_ARRAY ] = ctf_array_read,
91 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
92 };
93
94 static
95 rw_dispatch write_dispatch_table[] = {
96 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
97 [ CTF_TYPE_FLOAT ] = ctf_float_write,
98 [ CTF_TYPE_ENUM ] = ctf_enum_write,
99 [ CTF_TYPE_STRING ] = ctf_string_write,
100 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
101 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
102 [ CTF_TYPE_ARRAY ] = ctf_array_write,
103 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
104 };
105
106 static
107 struct format ctf_format = {
108 .open_trace = ctf_open_trace,
109 .open_mmap_trace = ctf_open_mmap_trace,
110 .close_trace = ctf_close_trace,
111 };
112
113 /*
114 * Update stream current timestamp, keep at clock frequency.
115 */
116 static
117 void ctf_update_timestamp(struct ctf_stream *stream,
118 struct definition_integer *integer_definition)
119 {
120 struct declaration_integer *integer_declaration =
121 integer_definition->declaration;
122 uint64_t oldval, newval, updateval;
123
124 if (unlikely(integer_declaration->len == 64)) {
125 stream->timestamp = integer_definition->value._unsigned;
126 return;
127 }
128 /* keep low bits */
129 oldval = stream->timestamp;
130 oldval &= (1ULL << integer_declaration->len) - 1;
131 newval = integer_definition->value._unsigned;
132 /* Test for overflow by comparing low bits */
133 if (newval < oldval)
134 newval += 1ULL << integer_declaration->len;
135 /* updateval contains old high bits, and new low bits (sum) */
136 updateval = stream->timestamp;
137 updateval &= ~((1ULL << integer_declaration->len) - 1);
138 updateval += newval;
139 stream->prev_timestamp = stream->timestamp;
140 stream->timestamp = updateval;
141 }
142
143 /*
144 * Print timestamp, rescaling clock frequency to nanoseconds and
145 * applying offsets as needed (unix time).
146 */
147 void ctf_print_timestamp(FILE *fp,
148 struct ctf_stream *stream,
149 uint64_t timestamp)
150 {
151 uint64_t ts_sec = 0, ts_nsec;
152 struct ctf_trace *trace = stream->stream_class->trace;
153 struct trace_collection *tc = trace->collection;
154 uint64_t tc_offset = tc->single_clock_offset_avg;
155
156 if (stream->current_clock->freq == 1000000000ULL) {
157 ts_nsec = timestamp;
158 } else {
159 ts_nsec = (uint64_t) ((double) timestamp * 1000000000.0
160 / (double) stream->current_clock->freq);
161 }
162
163 /* Add offsets */
164 if (!opt_clock_raw) {
165 ts_nsec += tc_offset;
166 }
167 ts_sec += opt_clock_offset;
168
169 ts_sec += ts_nsec / NSEC_PER_SEC;
170 ts_nsec = ts_nsec % NSEC_PER_SEC;
171
172 if (!opt_clock_seconds) {
173 struct tm tm;
174 time_t time_s = (time_t) ts_sec;
175
176 if (!opt_clock_gmt) {
177 struct tm *res;
178
179 res = localtime_r(&time_s, &tm);
180 if (!res) {
181 fprintf(stderr, "[warning] Unable to get localtime.\n");
182 goto seconds;
183 }
184 } else {
185 struct tm *res;
186
187 res = gmtime_r(&time_s, &tm);
188 if (!res) {
189 fprintf(stderr, "[warning] Unable to get gmtime.\n");
190 goto seconds;
191 }
192 }
193 if (opt_clock_date) {
194 char timestr[26];
195 size_t res;
196
197 /* Print date and time */
198 res = strftime(timestr, sizeof(timestr),
199 "%F ", &tm);
200 if (!res) {
201 fprintf(stderr, "[warning] Unable to print ascii time.\n");
202 goto seconds;
203 }
204 fprintf(fp, "%s", timestr);
205 }
206 /* Print time in HH:MM:SS.ns */
207 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
208 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
209 goto end;
210 }
211 seconds:
212 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
213 ts_sec, ts_nsec);
214
215 end:
216 return;
217 }
218
219 static
220 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
221 {
222 struct ctf_stream_pos *pos =
223 container_of(ppos, struct ctf_stream_pos, parent);
224 struct ctf_stream_class *stream_class = stream->stream_class;
225 struct ctf_stream_event *event;
226 uint64_t id = 0;
227 int ret;
228
229 /* We need to check for EOF here for empty files. */
230 if (unlikely(pos->offset == EOF))
231 return EOF;
232
233 ctf_pos_get_event(pos);
234
235 /* save the current position as a restore point */
236 pos->last_offset = pos->offset;
237 /* we just read the event, it is consumed when used by the caller */
238 stream->consumed = 0;
239
240 /*
241 * This is the EOF check after we've advanced the position in
242 * ctf_pos_get_event.
243 */
244 if (unlikely(pos->offset == EOF))
245 return EOF;
246 assert(pos->offset < pos->content_size);
247
248 /* Read event header */
249 if (likely(stream->stream_event_header)) {
250 struct definition_integer *integer_definition;
251 struct definition *variant;
252
253 ret = generic_rw(ppos, &stream->stream_event_header->p);
254 if (unlikely(ret))
255 goto error;
256 /* lookup event id */
257 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
258 if (integer_definition) {
259 id = integer_definition->value._unsigned;
260 } else {
261 struct definition_enum *enum_definition;
262
263 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
264 if (enum_definition) {
265 id = enum_definition->integer->value._unsigned;
266 }
267 }
268
269 variant = lookup_variant(&stream->stream_event_header->p, "v");
270 if (variant) {
271 integer_definition = lookup_integer(variant, "id", FALSE);
272 if (integer_definition) {
273 id = integer_definition->value._unsigned;
274 }
275 }
276 stream->event_id = id;
277
278 /* lookup timestamp */
279 stream->has_timestamp = 0;
280 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
281 if (integer_definition) {
282 ctf_update_timestamp(stream, integer_definition);
283 stream->has_timestamp = 1;
284 } else {
285 if (variant) {
286 integer_definition = lookup_integer(variant, "timestamp", FALSE);
287 if (integer_definition) {
288 ctf_update_timestamp(stream, integer_definition);
289 stream->has_timestamp = 1;
290 }
291 }
292 }
293 }
294
295 /* Read stream-declared event context */
296 if (stream->stream_event_context) {
297 ret = generic_rw(ppos, &stream->stream_event_context->p);
298 if (ret)
299 goto error;
300 }
301
302 if (unlikely(id >= stream_class->events_by_id->len)) {
303 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
304 return -EINVAL;
305 }
306 event = g_ptr_array_index(stream->events_by_id, id);
307 if (unlikely(!event)) {
308 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
309 return -EINVAL;
310 }
311
312 /* Read event-declared event context */
313 if (event->event_context) {
314 ret = generic_rw(ppos, &event->event_context->p);
315 if (ret)
316 goto error;
317 }
318
319 /* Read event payload */
320 if (likely(event->event_fields)) {
321 ret = generic_rw(ppos, &event->event_fields->p);
322 if (ret)
323 goto error;
324 }
325
326 return 0;
327
328 error:
329 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
330 return ret;
331 }
332
333 static
334 int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
335 {
336 struct ctf_stream_class *stream_class = stream->stream_class;
337 struct ctf_stream_event *event;
338 uint64_t id;
339 int ret;
340
341 id = stream->event_id;
342
343 /* print event header */
344 if (likely(stream->stream_event_header)) {
345 ret = generic_rw(pos, &stream->stream_event_header->p);
346 if (ret)
347 goto error;
348 }
349
350 /* print stream-declared event context */
351 if (stream->stream_event_context) {
352 ret = generic_rw(pos, &stream->stream_event_context->p);
353 if (ret)
354 goto error;
355 }
356
357 if (unlikely(id >= stream_class->events_by_id->len)) {
358 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
359 return -EINVAL;
360 }
361 event = g_ptr_array_index(stream->events_by_id, id);
362 if (unlikely(!event)) {
363 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
364 return -EINVAL;
365 }
366
367 /* print event-declared event context */
368 if (event->event_context) {
369 ret = generic_rw(pos, &event->event_context->p);
370 if (ret)
371 goto error;
372 }
373
374 /* Read and print event payload */
375 if (likely(event->event_fields)) {
376 ret = generic_rw(pos, &event->event_fields->p);
377 if (ret)
378 goto error;
379 }
380
381 return 0;
382
383 error:
384 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
385 return ret;
386 }
387
388 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
389 {
390 pos->fd = fd;
391 pos->mmap_offset = 0;
392 pos->packet_size = 0;
393 pos->content_size = 0;
394 pos->content_size_loc = NULL;
395 pos->base = NULL;
396 pos->offset = 0;
397 pos->dummy = false;
398 pos->cur_index = 0;
399 if (fd >= 0)
400 pos->packet_index = g_array_new(FALSE, TRUE,
401 sizeof(struct packet_index));
402 else
403 pos->packet_index = NULL;
404 switch (open_flags & O_ACCMODE) {
405 case O_RDONLY:
406 pos->prot = PROT_READ;
407 pos->flags = MAP_PRIVATE;
408 pos->parent.rw_table = read_dispatch_table;
409 pos->parent.event_cb = ctf_read_event;
410 break;
411 case O_RDWR:
412 pos->prot = PROT_WRITE; /* Write has priority */
413 pos->flags = MAP_SHARED;
414 pos->parent.rw_table = write_dispatch_table;
415 pos->parent.event_cb = ctf_write_event;
416 if (fd >= 0)
417 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
418 break;
419 default:
420 assert(0);
421 }
422 }
423
424 void ctf_fini_pos(struct ctf_stream_pos *pos)
425 {
426 int ret;
427
428 if (pos->prot == PROT_WRITE && pos->content_size_loc)
429 *pos->content_size_loc = pos->offset;
430 if (pos->base) {
431 /* unmap old base */
432 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
433 if (ret) {
434 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
435 strerror(errno));
436 assert(0);
437 }
438 }
439 (void) g_array_free(pos->packet_index, TRUE);
440 }
441
442 /*
443 * for SEEK_CUR: go to next packet.
444 * for SEEK_POS: go to packet numer (index).
445 */
446 void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
447 {
448 struct ctf_stream_pos *pos =
449 container_of(stream_pos, struct ctf_stream_pos, parent);
450 struct ctf_file_stream *file_stream =
451 container_of(pos, struct ctf_file_stream, pos);
452 int ret;
453 off_t off;
454 struct packet_index *packet_index;
455
456 if (pos->prot == PROT_WRITE && pos->content_size_loc)
457 *pos->content_size_loc = pos->offset;
458
459 if (pos->base) {
460 /* unmap old base */
461 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
462 if (ret) {
463 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
464 strerror(errno));
465 assert(0);
466 }
467 pos->base = NULL;
468 }
469
470 /*
471 * The caller should never ask for ctf_move_pos across packets,
472 * except to get exactly at the beginning of the next packet.
473 */
474 if (pos->prot == PROT_WRITE) {
475 switch (whence) {
476 case SEEK_CUR:
477 /* The writer will add padding */
478 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
479 break;
480 case SEEK_SET:
481 assert(index == 0); /* only seek supported for now */
482 pos->cur_index = 0;
483 break;
484 default:
485 assert(0);
486 }
487 pos->content_size = -1U; /* Unknown at this point */
488 pos->packet_size = WRITE_PACKET_LEN;
489 off = posix_fallocate(pos->fd, pos->mmap_offset,
490 pos->packet_size / CHAR_BIT);
491 assert(off >= 0);
492 pos->offset = 0;
493 } else {
494 read_next_packet:
495 switch (whence) {
496 case SEEK_CUR:
497 {
498 uint32_t events_discarded_diff;
499
500 if (pos->offset == EOF) {
501 return;
502 }
503 /* For printing discarded event count */
504 packet_index = &g_array_index(pos->packet_index,
505 struct packet_index, pos->cur_index);
506 events_discarded_diff = packet_index->events_discarded;
507 file_stream->parent.prev_timestamp_end =
508 packet_index->timestamp_end;
509 if (pos->cur_index > 0) {
510 packet_index = &g_array_index(pos->packet_index,
511 struct packet_index,
512 pos->cur_index - 1);
513 events_discarded_diff -= packet_index->events_discarded;
514 }
515 file_stream->parent.events_discarded = events_discarded_diff;
516 file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
517 /* The reader will expect us to skip padding */
518 ++pos->cur_index;
519 break;
520 }
521 case SEEK_SET:
522 pos->cur_index = index;
523 file_stream->parent.prev_timestamp = 0;
524 file_stream->parent.prev_timestamp_end = 0;
525 break;
526 default:
527 assert(0);
528 }
529 if (pos->cur_index >= pos->packet_index->len) {
530 /*
531 * When a stream reaches the end of the
532 * file, we need to show the number of
533 * events discarded ourselves, because
534 * there is no next event scheduled to
535 * be printed in the output.
536 */
537 if (file_stream->parent.events_discarded) {
538 fflush(stdout);
539 fprintf(stderr, "[warning] Tracer discarded %d events at end of stream between [",
540 file_stream->parent.events_discarded);
541 ctf_print_timestamp(stderr, &file_stream->parent,
542 file_stream->parent.prev_timestamp);
543 fprintf(stderr, "] and [");
544 ctf_print_timestamp(stderr, &file_stream->parent,
545 file_stream->parent.prev_timestamp_end);
546 fprintf(stderr, "]. You should consider increasing the buffer size.\n");
547 fflush(stderr);
548 file_stream->parent.events_discarded = 0;
549 }
550 pos->offset = EOF;
551 return;
552 }
553 packet_index = &g_array_index(pos->packet_index, struct packet_index,
554 pos->cur_index);
555 pos->mmap_offset = packet_index->offset;
556
557 /* Lookup context/packet size in index */
558 file_stream->parent.timestamp = packet_index->timestamp_begin;
559 pos->content_size = packet_index->content_size;
560 pos->packet_size = packet_index->packet_size;
561 if (packet_index->data_offset < packet_index->content_size) {
562 pos->offset = 0; /* will read headers */
563 } else if (packet_index->data_offset == packet_index->content_size) {
564 /* empty packet */
565 pos->offset = packet_index->data_offset;
566 whence = SEEK_CUR;
567 goto read_next_packet;
568 } else {
569 pos->offset = EOF;
570 return;
571 }
572 }
573 /* map new base. Need mapping length from header. */
574 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
575 pos->flags, pos->fd, pos->mmap_offset);
576 if (pos->base == MAP_FAILED) {
577 fprintf(stderr, "[error] mmap error %s.\n",
578 strerror(errno));
579 assert(0);
580 }
581
582 /* update trace_packet_header and stream_packet_context */
583 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
584 /* Read packet header */
585 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
586 assert(!ret);
587 }
588 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
589 /* Read packet context */
590 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
591 assert(!ret);
592 }
593 }
594
595 static
596 int packet_metadata(struct ctf_trace *td, FILE *fp)
597 {
598 uint32_t magic;
599 size_t len;
600 int ret = 0;
601
602 len = fread(&magic, sizeof(magic), 1, fp);
603 if (len != 1) {
604 goto end;
605 }
606 if (magic == TSDL_MAGIC) {
607 ret = 1;
608 td->byte_order = BYTE_ORDER;
609 CTF_TRACE_SET_FIELD(td, byte_order);
610 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
611 ret = 1;
612 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
613 LITTLE_ENDIAN : BIG_ENDIAN;
614 CTF_TRACE_SET_FIELD(td, byte_order);
615 }
616 end:
617 rewind(fp);
618 return ret;
619 }
620
621 /*
622 * Returns 0 on success, -1 on error.
623 */
624 static
625 int check_version(unsigned int major, unsigned int minor)
626 {
627 switch (major) {
628 case 1:
629 switch (minor) {
630 case 8:
631 return 0;
632 default:
633 goto warning;
634 }
635 default:
636 goto warning;
637
638 }
639
640 /* eventually return an error instead of warning */
641 warning:
642 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
643 major, minor);
644 return 0;
645 }
646
647 static
648 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
649 FILE *out)
650 {
651 struct metadata_packet_header header;
652 size_t readlen, writelen, toread;
653 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
654 int ret = 0;
655
656 readlen = fread(&header, header_sizeof(header), 1, in);
657 if (readlen < 1)
658 return -EINVAL;
659
660 if (td->byte_order != BYTE_ORDER) {
661 header.magic = GUINT32_SWAP_LE_BE(header.magic);
662 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
663 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
664 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
665 }
666 if (header.checksum)
667 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
668 if (header.compression_scheme) {
669 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
670 header.compression_scheme);
671 return -EINVAL;
672 }
673 if (header.encryption_scheme) {
674 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
675 header.encryption_scheme);
676 return -EINVAL;
677 }
678 if (header.checksum_scheme) {
679 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
680 header.checksum_scheme);
681 return -EINVAL;
682 }
683 if (check_version(header.major, header.minor) < 0)
684 return -EINVAL;
685 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
686 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
687 CTF_TRACE_SET_FIELD(td, uuid);
688 } else {
689 if (uuid_compare(header.uuid, td->uuid))
690 return -EINVAL;
691 }
692
693 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
694
695 for (;;) {
696 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
697 if (ferror(in)) {
698 ret = -EINVAL;
699 break;
700 }
701 if (babeltrace_debug) {
702 buf[readlen] = '\0';
703 fprintf(stderr, "[debug] metadata packet read: %s\n",
704 buf);
705 }
706
707 writelen = fwrite(buf, sizeof(char), readlen, out);
708 if (writelen < readlen) {
709 ret = -EIO;
710 break;
711 }
712 if (ferror(out)) {
713 ret = -EINVAL;
714 break;
715 }
716 toread -= readlen;
717 if (!toread) {
718 ret = 0; /* continue reading next packet */
719 goto read_padding;
720 }
721 }
722 return ret;
723
724 read_padding:
725 toread = (header.packet_size - header.content_size) / CHAR_BIT;
726 ret = fseek(in, toread, SEEK_CUR);
727 if (ret < 0) {
728 fprintf(stderr, "[warning] Missing padding at end of file\n");
729 ret = 0;
730 }
731 return ret;
732 }
733
734 static
735 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
736 char **buf)
737 {
738 FILE *in, *out;
739 size_t size;
740 int ret;
741
742 in = *fp;
743 /*
744 * Using strlen on *buf instead of size of open_memstream
745 * because its size includes garbage at the end (after final
746 * \0). This is the allocated size, not the actual string size.
747 */
748 out = open_memstream(buf, &size);
749 if (out == NULL) {
750 perror("Metadata open_memstream");
751 return -errno;
752 }
753 for (;;) {
754 ret = ctf_open_trace_metadata_packet_read(td, in, out);
755 if (ret) {
756 break;
757 }
758 if (feof(in)) {
759 ret = 0;
760 break;
761 }
762 }
763 fclose(out); /* flush the buffer */
764 fclose(in);
765 /* open for reading */
766 *fp = fmemopen(*buf, strlen(*buf), "rb");
767 if (!*fp) {
768 perror("Metadata fmemopen");
769 return -errno;
770 }
771 return 0;
772 }
773
774 static
775 int ctf_open_trace_metadata_read(struct ctf_trace *td,
776 void (*packet_seek)(struct stream_pos *pos, size_t index,
777 int whence), FILE *metadata_fp)
778 {
779 struct ctf_scanner *scanner;
780 struct ctf_file_stream *metadata_stream;
781 FILE *fp;
782 char *buf = NULL;
783 int ret = 0;
784
785 metadata_stream = g_new0(struct ctf_file_stream, 1);
786
787 if (packet_seek) {
788 metadata_stream->pos.packet_seek = packet_seek;
789 } else {
790 fprintf(stderr, "[error] packet_seek function undefined.\n");
791 ret = -1;
792 goto end_stream;
793 }
794
795 if (metadata_fp) {
796 fp = metadata_fp;
797 } else {
798 td->metadata = &metadata_stream->parent;
799 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
800 if (metadata_stream->pos.fd < 0) {
801 fprintf(stderr, "Unable to open metadata.\n");
802 return metadata_stream->pos.fd;
803 }
804
805 fp = fdopen(metadata_stream->pos.fd, "r");
806 if (!fp) {
807 fprintf(stderr, "[error] Unable to open metadata stream.\n");
808 perror("Metadata stream open");
809 ret = -errno;
810 goto end_stream;
811 }
812 }
813 if (babeltrace_debug)
814 yydebug = 1;
815
816 if (packet_metadata(td, fp)) {
817 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
818 if (ret)
819 goto end_packet_read;
820 } else {
821 unsigned int major, minor;
822 ssize_t nr_items;
823
824 td->byte_order = BYTE_ORDER;
825
826 /* Check text-only metadata header and version */
827 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
828 if (nr_items < 2)
829 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
830 if (check_version(major, minor) < 0) {
831 ret = -EINVAL;
832 goto end_packet_read;
833 }
834 rewind(fp);
835 }
836
837 scanner = ctf_scanner_alloc(fp);
838 if (!scanner) {
839 fprintf(stderr, "[error] Error allocating scanner\n");
840 ret = -ENOMEM;
841 goto end_scanner_alloc;
842 }
843 ret = ctf_scanner_append_ast(scanner);
844 if (ret) {
845 fprintf(stderr, "[error] Error creating AST\n");
846 goto end;
847 }
848
849 if (babeltrace_debug) {
850 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
851 if (ret) {
852 fprintf(stderr, "[error] Error visiting AST for XML output\n");
853 goto end;
854 }
855 }
856
857 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
858 if (ret) {
859 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
860 goto end;
861 }
862 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
863 td, td->byte_order);
864 if (ret) {
865 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
866 goto end;
867 }
868 end:
869 ctf_scanner_free(scanner);
870 end_scanner_alloc:
871 end_packet_read:
872 fclose(fp);
873 free(buf);
874 end_stream:
875 close(metadata_stream->pos.fd);
876 if (ret)
877 g_free(metadata_stream);
878 return ret;
879 }
880
881 static
882 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
883 struct ctf_stream *stream,
884 struct ctf_event *event)
885 {
886 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
887
888 if (event->context_decl) {
889 struct definition *definition =
890 event->context_decl->p.definition_new(&event->context_decl->p,
891 stream->parent_def_scope, 0, 0, "event.context");
892 if (!definition) {
893 goto error;
894 }
895 stream_event->event_context = container_of(definition,
896 struct definition_struct, p);
897 stream->parent_def_scope = stream_event->event_context->p.scope;
898 }
899 if (event->fields_decl) {
900 struct definition *definition =
901 event->fields_decl->p.definition_new(&event->fields_decl->p,
902 stream->parent_def_scope, 0, 0, "event.fields");
903 if (!definition) {
904 goto error;
905 }
906 stream_event->event_fields = container_of(definition,
907 struct definition_struct, p);
908 stream->parent_def_scope = stream_event->event_fields->p.scope;
909 }
910 return stream_event;
911
912 error:
913 if (stream_event->event_fields)
914 definition_unref(&stream_event->event_fields->p);
915 if (stream_event->event_context)
916 definition_unref(&stream_event->event_context->p);
917 return NULL;
918 }
919
920 static
921 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
922 {
923 struct ctf_stream_class *stream_class;
924 int ret;
925 int i;
926
927 if (stream->stream_definitions_created)
928 return 0;
929
930 stream_class = stream->stream_class;
931
932 if (stream_class->packet_context_decl) {
933 struct definition *definition =
934 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
935 stream->parent_def_scope, 0, 0, "stream.packet.context");
936 if (!definition) {
937 ret = -EINVAL;
938 goto error;
939 }
940 stream->stream_packet_context = container_of(definition,
941 struct definition_struct, p);
942 stream->parent_def_scope = stream->stream_packet_context->p.scope;
943 }
944 if (stream_class->event_header_decl) {
945 struct definition *definition =
946 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
947 stream->parent_def_scope, 0, 0, "stream.event.header");
948 if (!definition) {
949 ret = -EINVAL;
950 goto error;
951 }
952 stream->stream_event_header =
953 container_of(definition, struct definition_struct, p);
954 stream->parent_def_scope = stream->stream_event_header->p.scope;
955 }
956 if (stream_class->event_context_decl) {
957 struct definition *definition =
958 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
959 stream->parent_def_scope, 0, 0, "stream.event.context");
960 if (!definition) {
961 ret = -EINVAL;
962 goto error;
963 }
964 stream->stream_event_context =
965 container_of(definition, struct definition_struct, p);
966 stream->parent_def_scope = stream->stream_event_context->p.scope;
967 }
968 stream->events_by_id = g_ptr_array_new();
969 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
970 for (i = 0; i < stream->events_by_id->len; i++) {
971 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
972 struct ctf_stream_event *stream_event;
973
974 if (!event)
975 continue;
976 stream_event = create_event_definitions(td, stream, event);
977 if (!stream_event)
978 goto error_event;
979 g_ptr_array_index(stream->events_by_id, i) = stream_event;
980 }
981 return 0;
982
983 error_event:
984 for (i = 0; i < stream->events_by_id->len; i++) {
985 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
986 if (stream_event)
987 g_free(stream_event);
988 }
989 g_ptr_array_free(stream->events_by_id, TRUE);
990 error:
991 if (stream->stream_event_context)
992 definition_unref(&stream->stream_event_context->p);
993 if (stream->stream_event_header)
994 definition_unref(&stream->stream_event_header->p);
995 if (stream->stream_packet_context)
996 definition_unref(&stream->stream_packet_context->p);
997 return ret;
998 }
999
1000
1001 static
1002 int create_stream_packet_index(struct ctf_trace *td,
1003 struct ctf_file_stream *file_stream)
1004 {
1005 struct ctf_stream_class *stream;
1006 int len_index;
1007 struct ctf_stream_pos *pos;
1008 struct stat filestats;
1009 struct packet_index packet_index;
1010 int first_packet = 1;
1011 int ret;
1012
1013 pos = &file_stream->pos;
1014
1015 ret = fstat(pos->fd, &filestats);
1016 if (ret < 0)
1017 return ret;
1018
1019 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1020 return -EINVAL;
1021
1022 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1023 uint64_t stream_id = 0;
1024
1025 if (pos->base) {
1026 /* unmap old base */
1027 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
1028 if (ret) {
1029 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1030 strerror(errno));
1031 return ret;
1032 }
1033 pos->base = NULL;
1034 }
1035 /* map new base. Need mapping length from header. */
1036 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
1037 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1038 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1039 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1040 pos->offset = 0; /* Position of the packet header */
1041
1042 packet_index.offset = pos->mmap_offset;
1043 packet_index.content_size = 0;
1044 packet_index.packet_size = 0;
1045 packet_index.timestamp_begin = 0;
1046 packet_index.timestamp_end = 0;
1047 packet_index.events_discarded = 0;
1048
1049 /* read and check header, set stream id (and check) */
1050 if (file_stream->parent.trace_packet_header) {
1051 /* Read packet header */
1052 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1053 if (ret)
1054 return ret;
1055 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1056 if (len_index >= 0) {
1057 struct definition *field;
1058 uint64_t magic;
1059
1060 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1061 magic = get_unsigned_int(field);
1062 if (magic != CTF_MAGIC) {
1063 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1064 magic,
1065 file_stream->pos.packet_index->len,
1066 (ssize_t) pos->mmap_offset);
1067 return -EINVAL;
1068 }
1069 }
1070
1071 /* check uuid */
1072 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1073 if (len_index >= 0) {
1074 struct definition_array *defarray;
1075 struct definition *field;
1076 uint64_t i;
1077 uint8_t uuidval[UUID_LEN];
1078
1079 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1080 assert(field->declaration->id == CTF_TYPE_ARRAY);
1081 defarray = container_of(field, struct definition_array, p);
1082 assert(array_len(defarray) == UUID_LEN);
1083
1084 for (i = 0; i < UUID_LEN; i++) {
1085 struct definition *elem;
1086
1087 elem = array_index(defarray, i);
1088 uuidval[i] = get_unsigned_int(elem);
1089 }
1090 ret = uuid_compare(td->uuid, uuidval);
1091 if (ret) {
1092 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1093 return -EINVAL;
1094 }
1095 }
1096
1097
1098 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1099 if (len_index >= 0) {
1100 struct definition *field;
1101
1102 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1103 stream_id = get_unsigned_int(field);
1104 }
1105 }
1106
1107 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1108 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
1109 return -EINVAL;
1110 }
1111 if (first_packet) {
1112 file_stream->parent.stream_id = stream_id;
1113 if (stream_id >= td->streams->len) {
1114 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1115 return -EINVAL;
1116 }
1117 stream = g_ptr_array_index(td->streams, stream_id);
1118 if (!stream) {
1119 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1120 return -EINVAL;
1121 }
1122 file_stream->parent.stream_class = stream;
1123 ret = create_stream_definitions(td, &file_stream->parent);
1124 if (ret)
1125 return ret;
1126 }
1127 first_packet = 0;
1128
1129 if (file_stream->parent.stream_packet_context) {
1130 /* Read packet context */
1131 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1132 if (ret)
1133 return ret;
1134 /* read content size from header */
1135 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1136 if (len_index >= 0) {
1137 struct definition *field;
1138
1139 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1140 packet_index.content_size = get_unsigned_int(field);
1141 } else {
1142 /* Use file size for packet size */
1143 packet_index.content_size = filestats.st_size * CHAR_BIT;
1144 }
1145
1146 /* read packet size from header */
1147 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1148 if (len_index >= 0) {
1149 struct definition *field;
1150
1151 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1152 packet_index.packet_size = get_unsigned_int(field);
1153 } else {
1154 /* Use content size if non-zero, else file size */
1155 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1156 }
1157
1158 /* read timestamp begin from header */
1159 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1160 if (len_index >= 0) {
1161 struct definition *field;
1162
1163 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1164 packet_index.timestamp_begin = get_unsigned_int(field);
1165 }
1166
1167 /* read timestamp end from header */
1168 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1169 if (len_index >= 0) {
1170 struct definition *field;
1171
1172 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1173 packet_index.timestamp_end = get_unsigned_int(field);
1174 }
1175
1176 /* read events discarded from header */
1177 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1178 if (len_index >= 0) {
1179 struct definition *field;
1180
1181 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1182 packet_index.events_discarded = get_unsigned_int(field);
1183 }
1184 } else {
1185 /* Use file size for packet size */
1186 packet_index.content_size = filestats.st_size * CHAR_BIT;
1187 /* Use content size if non-zero, else file size */
1188 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1189 }
1190
1191 /* Validate content size and packet size values */
1192 if (packet_index.content_size > packet_index.packet_size) {
1193 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1194 packet_index.content_size, packet_index.packet_size);
1195 return -EINVAL;
1196 }
1197
1198 if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
1199 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1200 packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
1201 return -EINVAL;
1202 }
1203
1204 /* Save position after header and context */
1205 packet_index.data_offset = pos->offset;
1206
1207 /* add index to packet array */
1208 g_array_append_val(file_stream->pos.packet_index, packet_index);
1209
1210 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1211 }
1212
1213 /* Move pos back to beginning of file */
1214 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
1215
1216 return 0;
1217 }
1218
1219 static
1220 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1221 {
1222 int ret;
1223
1224 if (td->packet_header_decl) {
1225 struct definition *definition =
1226 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1227 stream->parent_def_scope, 0, 0, "trace.packet.header");
1228 if (!definition) {
1229 ret = -EINVAL;
1230 goto error;
1231 }
1232 stream->trace_packet_header =
1233 container_of(definition, struct definition_struct, p);
1234 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1235 }
1236
1237 return 0;
1238
1239 error:
1240 return ret;
1241 }
1242
1243 /*
1244 * Note: many file streams can inherit from the same stream class
1245 * description (metadata).
1246 */
1247 static
1248 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1249 void (*packet_seek)(struct stream_pos *pos, size_t index,
1250 int whence))
1251 {
1252 int ret;
1253 struct ctf_file_stream *file_stream;
1254
1255 ret = openat(td->dirfd, path, flags);
1256 if (ret < 0) {
1257 perror("File stream openat()");
1258 goto error;
1259 }
1260 file_stream = g_new0(struct ctf_file_stream, 1);
1261
1262 if (packet_seek) {
1263 file_stream->pos.packet_seek = packet_seek;
1264 } else {
1265 fprintf(stderr, "[error] packet_seek function undefined.\n");
1266 ret = -1;
1267 goto error_def;
1268 }
1269
1270 ctf_init_pos(&file_stream->pos, ret, flags);
1271 ret = create_trace_definitions(td, &file_stream->parent);
1272 if (ret)
1273 goto error_def;
1274 /*
1275 * For now, only a single slock is supported.
1276 */
1277 file_stream->parent.current_clock = td->single_clock;
1278 ret = create_stream_packet_index(td, file_stream);
1279 if (ret)
1280 goto error_index;
1281 /* Add stream file to stream class */
1282 g_ptr_array_add(file_stream->parent.stream_class->streams,
1283 &file_stream->parent);
1284 return 0;
1285
1286 error_index:
1287 if (file_stream->parent.trace_packet_header)
1288 definition_unref(&file_stream->parent.trace_packet_header->p);
1289 error_def:
1290 ctf_fini_pos(&file_stream->pos);
1291 close(file_stream->pos.fd);
1292 g_free(file_stream);
1293 error:
1294 return ret;
1295 }
1296
1297 static
1298 int ctf_open_trace_read(struct ctf_trace *td,
1299 const char *path, int flags,
1300 void (*packet_seek)(struct stream_pos *pos, size_t index,
1301 int whence), FILE *metadata_fp)
1302 {
1303 int ret;
1304 struct dirent *dirent;
1305 struct dirent *diriter;
1306 size_t dirent_len;
1307
1308 td->flags = flags;
1309
1310 /* Open trace directory */
1311 td->dir = opendir(path);
1312 if (!td->dir) {
1313 fprintf(stderr, "[error] Unable to open trace directory.\n");
1314 ret = -ENOENT;
1315 goto error;
1316 }
1317
1318 td->dirfd = open(path, 0);
1319 if (td->dirfd < 0) {
1320 fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
1321 perror("Trace directory open");
1322 ret = -errno;
1323 goto error_dirfd;
1324 }
1325 strncpy(td->path, path, sizeof(td->path));
1326 td->path[sizeof(td->path) - 1] = '\0';
1327
1328 /*
1329 * Keep the metadata file separate.
1330 */
1331
1332 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1333 if (ret) {
1334 goto error_metadata;
1335 }
1336
1337 /*
1338 * Open each stream: for each file, try to open, check magic
1339 * number, and get the stream ID to add to the right location in
1340 * the stream array.
1341 */
1342
1343 dirent_len = offsetof(struct dirent, d_name) +
1344 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1345
1346 dirent = malloc(dirent_len);
1347
1348 for (;;) {
1349 ret = readdir_r(td->dir, dirent, &diriter);
1350 if (ret) {
1351 fprintf(stderr, "[error] Readdir error.\n");
1352 goto readdir_error;
1353 }
1354 if (!diriter)
1355 break;
1356 /* Ignore hidden files, ., .. and metadata. */
1357 if (!strncmp(diriter->d_name, ".", 1)
1358 || !strcmp(diriter->d_name, "..")
1359 || !strcmp(diriter->d_name, "metadata"))
1360 continue;
1361 ret = ctf_open_file_stream_read(td, diriter->d_name,
1362 flags, packet_seek);
1363 if (ret) {
1364 fprintf(stderr, "[error] Open file stream error.\n");
1365 goto readdir_error;
1366 }
1367 }
1368
1369 free(dirent);
1370 return 0;
1371
1372 readdir_error:
1373 free(dirent);
1374 error_metadata:
1375 close(td->dirfd);
1376 error_dirfd:
1377 closedir(td->dir);
1378 error:
1379 return ret;
1380 }
1381
1382 static
1383 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1384 void (*packet_seek)(struct stream_pos *pos, size_t index,
1385 int whence), FILE *metadata_fp)
1386 {
1387 struct ctf_trace *td;
1388 int ret;
1389
1390 /*
1391 * If packet_seek is NULL, we provide our default version.
1392 */
1393 if (!packet_seek)
1394 packet_seek = ctf_packet_seek;
1395
1396 td = g_new0(struct ctf_trace, 1);
1397
1398 switch (flags & O_ACCMODE) {
1399 case O_RDONLY:
1400 if (!path) {
1401 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1402 goto error;
1403 }
1404 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1405 if (ret)
1406 goto error;
1407 break;
1408 case O_RDWR:
1409 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1410 goto error;
1411 default:
1412 fprintf(stderr, "[error] Incorrect open flags.\n");
1413 goto error;
1414 }
1415
1416 return &td->parent;
1417 error:
1418 g_free(td);
1419 return NULL;
1420 }
1421
1422
1423 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1424 struct mmap_stream *mmap_info)
1425 {
1426 pos->mmap_offset = 0;
1427 pos->packet_size = 0;
1428 pos->content_size = 0;
1429 pos->content_size_loc = NULL;
1430 pos->fd = mmap_info->fd;
1431 pos->base = 0;
1432 pos->offset = 0;
1433 pos->dummy = false;
1434 pos->cur_index = 0;
1435 pos->packet_index = NULL;
1436 pos->prot = PROT_READ;
1437 pos->flags = MAP_PRIVATE;
1438 pos->parent.rw_table = read_dispatch_table;
1439 pos->parent.event_cb = ctf_read_event;
1440 }
1441
1442 static
1443 int prepare_mmap_stream_definition(struct ctf_trace *td,
1444 struct ctf_file_stream *file_stream)
1445 {
1446 struct ctf_stream_class *stream;
1447 uint64_t stream_id = 0;
1448 int ret;
1449
1450 file_stream->parent.stream_id = stream_id;
1451 if (stream_id >= td->streams->len) {
1452 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1453 "in metadata.\n", stream_id);
1454 ret = -EINVAL;
1455 goto end;
1456 }
1457 stream = g_ptr_array_index(td->streams, stream_id);
1458 if (!stream) {
1459 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1460 "in metadata.\n", stream_id);
1461 ret = -EINVAL;
1462 goto end;
1463 }
1464 file_stream->parent.stream_class = stream;
1465 ret = create_stream_definitions(td, &file_stream->parent);
1466 end:
1467 return ret;
1468 }
1469
1470 static
1471 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1472 struct mmap_stream *mmap_info,
1473 void (*packet_seek)(struct stream_pos *pos, size_t index,
1474 int whence))
1475 {
1476 int ret;
1477 struct ctf_file_stream *file_stream;
1478
1479 file_stream = g_new0(struct ctf_file_stream, 1);
1480 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1481
1482 file_stream->pos.packet_seek = packet_seek;
1483
1484 ret = create_trace_definitions(td, &file_stream->parent);
1485 if (ret) {
1486 goto error_def;
1487 }
1488
1489 ret = prepare_mmap_stream_definition(td, file_stream);
1490 if (ret)
1491 goto error_index;
1492
1493 /* Add stream file to stream class */
1494 g_ptr_array_add(file_stream->parent.stream_class->streams,
1495 &file_stream->parent);
1496 return 0;
1497
1498 error_index:
1499 if (file_stream->parent.trace_packet_header)
1500 definition_unref(&file_stream->parent.trace_packet_header->p);
1501 error_def:
1502 g_free(file_stream);
1503 return ret;
1504 }
1505
1506 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1507 struct mmap_stream_list *mmap_list,
1508 void (*packet_seek)(struct stream_pos *pos, size_t index,
1509 int whence),
1510 FILE *metadata_fp)
1511 {
1512 int ret;
1513 struct mmap_stream *mmap_info;
1514
1515 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1516 if (ret) {
1517 goto error;
1518 }
1519
1520 /*
1521 * for each stream, try to open, check magic number, and get the
1522 * stream ID to add to the right location in the stream array.
1523 */
1524 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1525 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
1526 if (ret) {
1527 fprintf(stderr, "[error] Open file mmap stream error.\n");
1528 goto error;
1529 }
1530 }
1531
1532 return 0;
1533
1534 error:
1535 return ret;
1536 }
1537
1538 static
1539 struct trace_descriptor *ctf_open_mmap_trace(
1540 struct mmap_stream_list *mmap_list,
1541 void (*packet_seek)(struct stream_pos *pos, size_t index,
1542 int whence),
1543 FILE *metadata_fp)
1544 {
1545 struct ctf_trace *td;
1546 int ret;
1547
1548 if (!metadata_fp) {
1549 fprintf(stderr, "[error] No metadata file pointer associated, "
1550 "required for mmap parsing\n");
1551 goto error;
1552 }
1553 if (!packet_seek) {
1554 fprintf(stderr, "[error] packet_seek function undefined.\n");
1555 goto error;
1556 }
1557 td = g_new0(struct ctf_trace, 1);
1558 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
1559 if (ret)
1560 goto error_free;
1561
1562 return &td->parent;
1563
1564 error_free:
1565 g_free(td);
1566 error:
1567 return NULL;
1568 }
1569
1570 static
1571 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1572 {
1573 ctf_fini_pos(&file_stream->pos);
1574 close(file_stream->pos.fd);
1575 }
1576
1577 static
1578 void ctf_close_trace(struct trace_descriptor *tdp)
1579 {
1580 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1581 int i;
1582
1583 if (td->streams) {
1584 for (i = 0; i < td->streams->len; i++) {
1585 struct ctf_stream_class *stream;
1586 int j;
1587
1588 stream = g_ptr_array_index(td->streams, i);
1589 if (!stream)
1590 continue;
1591 for (j = 0; j < stream->streams->len; j++) {
1592 struct ctf_file_stream *file_stream;
1593 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1594 ctf_close_file_stream(file_stream);
1595 }
1596
1597 }
1598 g_ptr_array_free(td->streams, TRUE);
1599 }
1600 closedir(td->dir);
1601 g_free(td);
1602 }
1603
1604 void __attribute__((constructor)) ctf_init(void)
1605 {
1606 int ret;
1607
1608 ctf_format.name = g_quark_from_static_string("ctf");
1609 ret = bt_register_format(&ctf_format);
1610 assert(!ret);
1611 }
1612
1613 /* TODO: finalize */
This page took 0.097321 seconds and 4 git commands to generate.