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