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