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