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