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