Update message
[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 CTF_TRACE_SET_FIELD(td, byte_order);
462 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
463 ret = 1;
464 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
465 LITTLE_ENDIAN : BIG_ENDIAN;
466 CTF_TRACE_SET_FIELD(td, byte_order);
467 }
468 end:
469 rewind(fp);
470 return ret;
471 }
472
473 /*
474 * Returns 0 on success, -1 on error.
475 */
476 static
477 int check_version(unsigned int major, unsigned int minor)
478 {
479 switch (major) {
480 case 1:
481 switch (minor) {
482 case 8:
483 return 0;
484 default:
485 goto warning;
486 }
487 default:
488 goto warning;
489
490 }
491
492 /* eventually return an error instead of warning */
493 warning:
494 fprintf(stdout, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
495 major, minor);
496 return 0;
497 }
498
499 static
500 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
501 FILE *out)
502 {
503 struct metadata_packet_header header;
504 size_t readlen, writelen, toread;
505 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
506 int ret = 0;
507
508 readlen = fread(&header, header_sizeof(header), 1, in);
509 if (readlen < 1)
510 return -EINVAL;
511
512 if (td->byte_order != BYTE_ORDER) {
513 header.magic = GUINT32_SWAP_LE_BE(header.magic);
514 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
515 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
516 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
517 }
518 if (header.checksum)
519 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
520 if (header.compression_scheme) {
521 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
522 header.compression_scheme);
523 return -EINVAL;
524 }
525 if (header.encryption_scheme) {
526 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
527 header.encryption_scheme);
528 return -EINVAL;
529 }
530 if (header.checksum_scheme) {
531 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
532 header.checksum_scheme);
533 return -EINVAL;
534 }
535 if (check_version(header.major, header.minor) < 0)
536 return -EINVAL;
537 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
538 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
539 CTF_TRACE_SET_FIELD(td, uuid);
540 } else {
541 if (uuid_compare(header.uuid, td->uuid))
542 return -EINVAL;
543 }
544
545 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
546
547 for (;;) {
548 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
549 if (ferror(in)) {
550 ret = -EINVAL;
551 break;
552 }
553 if (babeltrace_debug) {
554 buf[readlen] = '\0';
555 fprintf(stdout, "[debug] metadata packet read: %s\n",
556 buf);
557 }
558
559 writelen = fwrite(buf, sizeof(char), readlen, out);
560 if (writelen < readlen) {
561 ret = -EIO;
562 break;
563 }
564 if (ferror(out)) {
565 ret = -EINVAL;
566 break;
567 }
568 toread -= readlen;
569 if (!toread) {
570 ret = 0; /* continue reading next packet */
571 break;
572 }
573 }
574 return ret;
575 }
576
577 static
578 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
579 char **buf)
580 {
581 FILE *in, *out;
582 size_t size;
583 int ret;
584
585 in = *fp;
586 /*
587 * Using strlen on *buf instead of size of open_memstream
588 * because its size includes garbage at the end (after final
589 * \0). This is the allocated size, not the actual string size.
590 */
591 out = open_memstream(buf, &size);
592 if (out == NULL)
593 return -errno;
594
595 for (;;) {
596 ret = ctf_open_trace_metadata_packet_read(td, in, out);
597 if (ret) {
598 break;
599 }
600 if (feof(in)) {
601 ret = 0;
602 break;
603 }
604 }
605 fclose(out); /* flush the buffer */
606 fclose(in);
607 /* open for reading */
608 *fp = fmemopen(*buf, strlen(*buf), "rb");
609 return 0;
610 }
611
612 static
613 int ctf_open_trace_metadata_read(struct ctf_trace *td)
614 {
615 struct ctf_scanner *scanner;
616 struct ctf_file_stream *metadata_stream;
617 FILE *fp;
618 char *buf = NULL;
619 int ret = 0;
620
621 metadata_stream = g_new0(struct ctf_file_stream, 1);
622 td->metadata = &metadata_stream->parent;
623 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
624 if (metadata_stream->pos.fd < 0) {
625 fprintf(stdout, "Unable to open metadata.\n");
626 return metadata_stream->pos.fd;
627 }
628
629 if (babeltrace_debug)
630 yydebug = 1;
631
632 fp = fdopen(metadata_stream->pos.fd, "r");
633 if (!fp) {
634 fprintf(stdout, "[error] Unable to open metadata stream.\n");
635 ret = -errno;
636 goto end_stream;
637 }
638
639 if (packet_metadata(td, fp)) {
640 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
641 if (ret)
642 goto end_packet_read;
643 } else {
644 unsigned int major, minor;
645 ssize_t nr_items;
646
647 td->byte_order = BYTE_ORDER;
648
649 /* Check text-only metadata header and version */
650 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
651 if (nr_items < 2)
652 fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
653 if (check_version(major, minor) < 0) {
654 ret = -EINVAL;
655 goto end_packet_read;
656 }
657 rewind(fp);
658 }
659
660 scanner = ctf_scanner_alloc(fp);
661 if (!scanner) {
662 fprintf(stdout, "[error] Error allocating scanner\n");
663 ret = -ENOMEM;
664 goto end_scanner_alloc;
665 }
666 ret = ctf_scanner_append_ast(scanner);
667 if (ret) {
668 fprintf(stdout, "[error] Error creating AST\n");
669 goto end;
670 }
671
672 if (babeltrace_debug) {
673 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
674 if (ret) {
675 fprintf(stdout, "[error] Error visiting AST for XML output\n");
676 goto end;
677 }
678 }
679
680 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
681 if (ret) {
682 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
683 goto end;
684 }
685 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
686 td, td->byte_order);
687 if (ret) {
688 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
689 goto end;
690 }
691 end:
692 ctf_scanner_free(scanner);
693 end_scanner_alloc:
694 end_packet_read:
695 fclose(fp);
696 free(buf);
697 end_stream:
698 close(metadata_stream->pos.fd);
699 if (ret)
700 g_free(metadata_stream);
701 return ret;
702 }
703
704 static
705 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
706 struct ctf_stream *stream,
707 struct ctf_event *event)
708 {
709 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
710
711 if (event->context_decl) {
712 struct definition *definition =
713 event->context_decl->p.definition_new(&event->context_decl->p,
714 stream->parent_def_scope, 0, 0, "event.context");
715 if (!definition) {
716 goto error;
717 }
718 stream_event->event_context = container_of(definition,
719 struct definition_struct, p);
720 stream->parent_def_scope = stream_event->event_context->p.scope;
721 }
722 if (event->fields_decl) {
723 struct definition *definition =
724 event->fields_decl->p.definition_new(&event->fields_decl->p,
725 stream->parent_def_scope, 0, 0, "event.fields");
726 if (!definition) {
727 goto error;
728 }
729 stream_event->event_fields = container_of(definition,
730 struct definition_struct, p);
731 stream->parent_def_scope = stream_event->event_fields->p.scope;
732 }
733 return stream_event;
734
735 error:
736 if (stream_event->event_fields)
737 definition_unref(&stream_event->event_fields->p);
738 if (stream_event->event_context)
739 definition_unref(&stream_event->event_context->p);
740 return NULL;
741 }
742
743 static
744 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
745 {
746 struct ctf_stream_class *stream_class;
747 int ret;
748 int i;
749
750 if (stream->stream_definitions_created)
751 return 0;
752
753 stream_class = stream->stream_class;
754
755 if (stream_class->packet_context_decl) {
756 struct definition *definition =
757 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
758 stream->parent_def_scope, 0, 0, "stream.packet.context");
759 if (!definition) {
760 ret = -EINVAL;
761 goto error;
762 }
763 stream->stream_packet_context = container_of(definition,
764 struct definition_struct, p);
765 stream->parent_def_scope = stream->stream_packet_context->p.scope;
766 }
767 if (stream_class->event_header_decl) {
768 struct definition *definition =
769 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
770 stream->parent_def_scope, 0, 0, "stream.event.header");
771 if (!definition) {
772 ret = -EINVAL;
773 goto error;
774 }
775 stream->stream_event_header =
776 container_of(definition, struct definition_struct, p);
777 stream->parent_def_scope = stream->stream_event_header->p.scope;
778 }
779 if (stream_class->event_context_decl) {
780 struct definition *definition =
781 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
782 stream->parent_def_scope, 0, 0, "stream.event.context");
783 if (!definition) {
784 ret = -EINVAL;
785 goto error;
786 }
787 stream->stream_event_context =
788 container_of(definition, struct definition_struct, p);
789 stream->parent_def_scope = stream->stream_event_context->p.scope;
790 }
791 stream->events_by_id = g_ptr_array_new();
792 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
793 for (i = 0; i < stream->events_by_id->len; i++) {
794 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
795 struct ctf_stream_event *stream_event;
796
797 if (!event)
798 continue;
799 stream_event = create_event_definitions(td, stream, event);
800 if (!stream_event)
801 goto error_event;
802 g_ptr_array_index(stream->events_by_id, i) = stream_event;
803 }
804 return 0;
805
806 error_event:
807 for (i = 0; i < stream->events_by_id->len; i++) {
808 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
809 if (stream_event)
810 g_free(stream_event);
811 }
812 g_ptr_array_free(stream->events_by_id, TRUE);
813 error:
814 if (stream->stream_event_context)
815 definition_unref(&stream->stream_event_context->p);
816 if (stream->stream_event_header)
817 definition_unref(&stream->stream_event_header->p);
818 if (stream->stream_packet_context)
819 definition_unref(&stream->stream_packet_context->p);
820 return ret;
821 }
822
823
824 static
825 int create_stream_packet_index(struct ctf_trace *td,
826 struct ctf_file_stream *file_stream)
827 {
828 struct ctf_stream_class *stream;
829 int len_index;
830 struct ctf_stream_pos *pos;
831 struct stat filestats;
832 struct packet_index packet_index;
833 int first_packet = 1;
834 int ret;
835
836 pos = &file_stream->pos;
837
838 ret = fstat(pos->fd, &filestats);
839 if (ret < 0)
840 return ret;
841
842 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
843 return -EINVAL;
844
845 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
846 uint64_t stream_id = 0;
847
848 if (pos->base) {
849 /* unmap old base */
850 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
851 if (ret) {
852 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
853 strerror(errno));
854 return ret;
855 }
856 pos->base = NULL;
857 }
858 /* map new base. Need mapping length from header. */
859 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
860 MAP_PRIVATE, pos->fd, pos->mmap_offset);
861 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
862 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
863 pos->offset = 0; /* Position of the packet header */
864
865 packet_index.offset = pos->mmap_offset;
866 packet_index.content_size = 0;
867 packet_index.packet_size = 0;
868 packet_index.timestamp_begin = 0;
869 packet_index.timestamp_end = 0;
870
871 /* read and check header, set stream id (and check) */
872 if (file_stream->parent.trace_packet_header) {
873 /* Read packet header */
874 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
875 if (ret)
876 return ret;
877 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
878 if (len_index >= 0) {
879 struct definition_integer *defint;
880 struct definition *field;
881
882 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
883 assert(field->declaration->id == CTF_TYPE_INTEGER);
884 defint = container_of(field, struct definition_integer, p);
885 assert(defint->declaration->signedness == FALSE);
886 if (defint->value._unsigned != CTF_MAGIC) {
887 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
888 defint->value._unsigned,
889 file_stream->pos.packet_index->len,
890 (ssize_t) pos->mmap_offset);
891 return -EINVAL;
892 }
893 }
894
895 /* check uuid */
896 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
897 if (len_index >= 0) {
898 struct definition_array *defarray;
899 struct definition *field;
900 uint64_t i;
901 uint8_t uuidval[UUID_LEN];
902
903 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
904 assert(field->declaration->id == CTF_TYPE_ARRAY);
905 defarray = container_of(field, struct definition_array, p);
906 assert(array_len(defarray) == UUID_LEN);
907 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
908
909 for (i = 0; i < UUID_LEN; i++) {
910 struct definition *elem;
911 struct definition_integer *defint;
912
913 elem = array_index(defarray, i);
914 assert(elem);
915 defint = container_of(elem, struct definition_integer, p);
916 uuidval[i] = defint->value._unsigned;
917 }
918 ret = uuid_compare(td->uuid, uuidval);
919 if (ret) {
920 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
921 return -EINVAL;
922 }
923 }
924
925
926 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
927 if (len_index >= 0) {
928 struct definition_integer *defint;
929 struct definition *field;
930
931 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
932 assert(field->declaration->id == CTF_TYPE_INTEGER);
933 defint = container_of(field, struct definition_integer, p);
934 assert(defint->declaration->signedness == FALSE);
935 stream_id = defint->value._unsigned;
936 }
937 }
938
939 if (!first_packet && file_stream->parent.stream_id != stream_id) {
940 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
941 return -EINVAL;
942 }
943 if (first_packet) {
944 file_stream->parent.stream_id = stream_id;
945 if (stream_id >= td->streams->len) {
946 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
947 return -EINVAL;
948 }
949 stream = g_ptr_array_index(td->streams, stream_id);
950 if (!stream) {
951 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
952 return -EINVAL;
953 }
954 file_stream->parent.stream_class = stream;
955 ret = create_stream_definitions(td, &file_stream->parent);
956 if (ret)
957 return ret;
958 }
959 first_packet = 0;
960
961 if (file_stream->parent.stream_packet_context) {
962 /* Read packet context */
963 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
964 if (ret)
965 return ret;
966 /* read content size from header */
967 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
968 if (len_index >= 0) {
969 struct definition_integer *defint;
970 struct definition *field;
971
972 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
973 assert(field->declaration->id == CTF_TYPE_INTEGER);
974 defint = container_of(field, struct definition_integer, p);
975 assert(defint->declaration->signedness == FALSE);
976 packet_index.content_size = defint->value._unsigned;
977 } else {
978 /* Use file size for packet size */
979 packet_index.content_size = filestats.st_size * CHAR_BIT;
980 }
981
982 /* read packet size from header */
983 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
984 if (len_index >= 0) {
985 struct definition_integer *defint;
986 struct definition *field;
987
988 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
989 assert(field->declaration->id == CTF_TYPE_INTEGER);
990 defint = container_of(field, struct definition_integer, p);
991 assert(defint->declaration->signedness == FALSE);
992 packet_index.packet_size = defint->value._unsigned;
993 } else {
994 /* Use content size if non-zero, else file size */
995 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
996 }
997
998 /* read timestamp begin from header */
999 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1000 if (len_index >= 0) {
1001 struct definition_integer *defint;
1002 struct definition *field;
1003
1004 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1005 assert(field->declaration->id == CTF_TYPE_INTEGER);
1006 defint = container_of(field, struct definition_integer, p);
1007 assert(defint->declaration->signedness == FALSE);
1008 packet_index.timestamp_begin = defint->value._unsigned;
1009 }
1010
1011 /* read timestamp end from header */
1012 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1013 if (len_index >= 0) {
1014 struct definition_integer *defint;
1015 struct definition *field;
1016
1017 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1018 assert(field->declaration->id == CTF_TYPE_INTEGER);
1019 defint = container_of(field, struct definition_integer, p);
1020 assert(defint->declaration->signedness == FALSE);
1021 packet_index.timestamp_end = defint->value._unsigned;
1022 }
1023 } else {
1024 /* Use file size for packet size */
1025 packet_index.content_size = filestats.st_size * CHAR_BIT;
1026 /* Use content size if non-zero, else file size */
1027 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1028 }
1029
1030 /* Validate content size and packet size values */
1031 if (packet_index.content_size > packet_index.packet_size) {
1032 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1033 packet_index.content_size, packet_index.packet_size);
1034 return -EINVAL;
1035 }
1036
1037 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1038 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1039 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
1040 return -EINVAL;
1041 }
1042
1043 /* Save position after header and context */
1044 packet_index.data_offset = pos->offset;
1045
1046 /* add index to packet array */
1047 g_array_append_val(file_stream->pos.packet_index, packet_index);
1048
1049 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1050 }
1051
1052 /* Move pos back to beginning of file */
1053 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1054
1055 return 0;
1056 }
1057
1058 static
1059 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1060 {
1061 int ret;
1062
1063 if (td->packet_header_decl) {
1064 struct definition *definition =
1065 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1066 stream->parent_def_scope, 0, 0, "trace.packet.header");
1067 if (!definition) {
1068 ret = -EINVAL;
1069 goto error;
1070 }
1071 stream->trace_packet_header =
1072 container_of(definition, struct definition_struct, p);
1073 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1074 }
1075
1076 return 0;
1077
1078 error:
1079 return ret;
1080 }
1081
1082 /*
1083 * Note: many file streams can inherit from the same stream class
1084 * description (metadata).
1085 */
1086 static
1087 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
1088 {
1089 int ret;
1090 struct ctf_file_stream *file_stream;
1091
1092 ret = openat(td->dirfd, path, flags);
1093 if (ret < 0)
1094 goto error;
1095 file_stream = g_new0(struct ctf_file_stream, 1);
1096 ctf_init_pos(&file_stream->pos, ret, flags);
1097 ret = create_trace_definitions(td, &file_stream->parent);
1098 if (ret)
1099 goto error_def;
1100 ret = create_stream_packet_index(td, file_stream);
1101 if (ret)
1102 goto error_index;
1103 /* Add stream file to stream class */
1104 g_ptr_array_add(file_stream->parent.stream_class->streams,
1105 &file_stream->parent);
1106 return 0;
1107
1108 error_index:
1109 if (file_stream->parent.trace_packet_header)
1110 definition_unref(&file_stream->parent.trace_packet_header->p);
1111 error_def:
1112 ctf_fini_pos(&file_stream->pos);
1113 close(file_stream->pos.fd);
1114 g_free(file_stream);
1115 error:
1116 return ret;
1117 }
1118
1119 static
1120 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
1121 {
1122 int ret;
1123 struct dirent *dirent;
1124 struct dirent *diriter;
1125 size_t dirent_len;
1126
1127 td->flags = flags;
1128
1129 /* Open trace directory */
1130 td->dir = opendir(path);
1131 if (!td->dir) {
1132 fprintf(stdout, "[error] Unable to open trace directory.\n");
1133 ret = -ENOENT;
1134 goto error;
1135 }
1136
1137 td->dirfd = open(path, 0);
1138 if (td->dirfd < 0) {
1139 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
1140 ret = -ENOENT;
1141 goto error_dirfd;
1142 }
1143
1144 /*
1145 * Keep the metadata file separate.
1146 */
1147
1148 ret = ctf_open_trace_metadata_read(td);
1149 if (ret) {
1150 goto error_metadata;
1151 }
1152
1153 /*
1154 * Open each stream: for each file, try to open, check magic
1155 * number, and get the stream ID to add to the right location in
1156 * the stream array.
1157 */
1158
1159 dirent_len = offsetof(struct dirent, d_name) +
1160 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1161
1162 dirent = malloc(dirent_len);
1163
1164 for (;;) {
1165 ret = readdir_r(td->dir, dirent, &diriter);
1166 if (ret) {
1167 fprintf(stdout, "[error] Readdir error.\n");
1168 goto readdir_error;
1169 }
1170 if (!diriter)
1171 break;
1172 /* Ignore hidden files, ., .. and metadata. */
1173 if (!strncmp(diriter->d_name, ".", 1)
1174 || !strcmp(diriter->d_name, "..")
1175 || !strcmp(diriter->d_name, "metadata"))
1176 continue;
1177 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
1178 if (ret) {
1179 fprintf(stdout, "[error] Open file stream error.\n");
1180 goto readdir_error;
1181 }
1182 }
1183
1184 free(dirent);
1185 return 0;
1186
1187 readdir_error:
1188 free(dirent);
1189 error_metadata:
1190 close(td->dirfd);
1191 error_dirfd:
1192 closedir(td->dir);
1193 error:
1194 return ret;
1195 }
1196
1197 static
1198 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
1199 {
1200 struct ctf_trace *td;
1201 int ret;
1202
1203 td = g_new0(struct ctf_trace, 1);
1204
1205 switch (flags & O_ACCMODE) {
1206 case O_RDONLY:
1207 if (!path) {
1208 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1209 goto error;
1210 }
1211 ret = ctf_open_trace_read(td, path, flags);
1212 if (ret)
1213 goto error;
1214 break;
1215 case O_RDWR:
1216 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1217 goto error;
1218 default:
1219 fprintf(stdout, "[error] Incorrect open flags.\n");
1220 goto error;
1221 }
1222
1223 return &td->parent;
1224 error:
1225 g_free(td);
1226 return NULL;
1227 }
1228
1229 static
1230 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1231 {
1232 ctf_fini_pos(&file_stream->pos);
1233 close(file_stream->pos.fd);
1234 }
1235
1236 static
1237 void ctf_close_trace(struct trace_descriptor *tdp)
1238 {
1239 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1240 int i;
1241
1242 if (td->streams) {
1243 for (i = 0; i < td->streams->len; i++) {
1244 struct ctf_stream_class *stream;
1245 int j;
1246
1247 stream = g_ptr_array_index(td->streams, i);
1248 if (!stream)
1249 continue;
1250 for (j = 0; j < stream->streams->len; j++) {
1251 struct ctf_file_stream *file_stream;
1252 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1253 ctf_close_file_stream(file_stream);
1254 }
1255
1256 }
1257 g_ptr_array_free(td->streams, TRUE);
1258 }
1259 closedir(td->dir);
1260 g_free(td);
1261 }
1262
1263 void __attribute__((constructor)) ctf_init(void)
1264 {
1265 int ret;
1266
1267 ctf_format.name = g_quark_from_static_string("ctf");
1268 ret = bt_register_format(&ctf_format);
1269 assert(!ret);
1270 }
1271
1272 /* TODO: finalize */
This page took 0.060826 seconds and 4 git commands to generate.