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