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