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