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