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