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