Optional file pointer to metadata
[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), FILE *metadata_fp);
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), FILE *metadata_fp)
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 if (metadata_fp) {
635 fp = metadata_fp;
636 } else {
637 td->metadata = &metadata_stream->parent;
638 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
639 if (metadata_stream->pos.fd < 0) {
640 fprintf(stdout, "Unable to open metadata.\n");
641 return metadata_stream->pos.fd;
642 }
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 (babeltrace_debug)
652 yydebug = 1;
653
654 if (packet_metadata(td, fp)) {
655 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
656 if (ret)
657 goto end_packet_read;
658 } else {
659 unsigned int major, minor;
660 ssize_t nr_items;
661
662 td->byte_order = BYTE_ORDER;
663
664 /* Check text-only metadata header and version */
665 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
666 if (nr_items < 2)
667 fprintf(stdout, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
668 if (check_version(major, minor) < 0) {
669 ret = -EINVAL;
670 goto end_packet_read;
671 }
672 rewind(fp);
673 }
674
675 scanner = ctf_scanner_alloc(fp);
676 if (!scanner) {
677 fprintf(stdout, "[error] Error allocating scanner\n");
678 ret = -ENOMEM;
679 goto end_scanner_alloc;
680 }
681 ret = ctf_scanner_append_ast(scanner);
682 if (ret) {
683 fprintf(stdout, "[error] Error creating AST\n");
684 goto end;
685 }
686
687 if (babeltrace_debug) {
688 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
689 if (ret) {
690 fprintf(stdout, "[error] Error visiting AST for XML output\n");
691 goto end;
692 }
693 }
694
695 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
696 if (ret) {
697 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
698 goto end;
699 }
700 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
701 td, td->byte_order);
702 if (ret) {
703 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
704 goto end;
705 }
706 end:
707 ctf_scanner_free(scanner);
708 end_scanner_alloc:
709 end_packet_read:
710 fclose(fp);
711 free(buf);
712 end_stream:
713 close(metadata_stream->pos.fd);
714 if (ret)
715 g_free(metadata_stream);
716 return ret;
717 }
718
719 static
720 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
721 struct ctf_stream *stream,
722 struct ctf_event *event)
723 {
724 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
725
726 if (event->context_decl) {
727 struct definition *definition =
728 event->context_decl->p.definition_new(&event->context_decl->p,
729 stream->parent_def_scope, 0, 0, "event.context");
730 if (!definition) {
731 goto error;
732 }
733 stream_event->event_context = container_of(definition,
734 struct definition_struct, p);
735 stream->parent_def_scope = stream_event->event_context->p.scope;
736 }
737 if (event->fields_decl) {
738 struct definition *definition =
739 event->fields_decl->p.definition_new(&event->fields_decl->p,
740 stream->parent_def_scope, 0, 0, "event.fields");
741 if (!definition) {
742 goto error;
743 }
744 stream_event->event_fields = container_of(definition,
745 struct definition_struct, p);
746 stream->parent_def_scope = stream_event->event_fields->p.scope;
747 }
748 return stream_event;
749
750 error:
751 if (stream_event->event_fields)
752 definition_unref(&stream_event->event_fields->p);
753 if (stream_event->event_context)
754 definition_unref(&stream_event->event_context->p);
755 return NULL;
756 }
757
758 static
759 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
760 {
761 struct ctf_stream_class *stream_class;
762 int ret;
763 int i;
764
765 if (stream->stream_definitions_created)
766 return 0;
767
768 stream_class = stream->stream_class;
769
770 if (stream_class->packet_context_decl) {
771 struct definition *definition =
772 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
773 stream->parent_def_scope, 0, 0, "stream.packet.context");
774 if (!definition) {
775 ret = -EINVAL;
776 goto error;
777 }
778 stream->stream_packet_context = container_of(definition,
779 struct definition_struct, p);
780 stream->parent_def_scope = stream->stream_packet_context->p.scope;
781 }
782 if (stream_class->event_header_decl) {
783 struct definition *definition =
784 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
785 stream->parent_def_scope, 0, 0, "stream.event.header");
786 if (!definition) {
787 ret = -EINVAL;
788 goto error;
789 }
790 stream->stream_event_header =
791 container_of(definition, struct definition_struct, p);
792 stream->parent_def_scope = stream->stream_event_header->p.scope;
793 }
794 if (stream_class->event_context_decl) {
795 struct definition *definition =
796 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
797 stream->parent_def_scope, 0, 0, "stream.event.context");
798 if (!definition) {
799 ret = -EINVAL;
800 goto error;
801 }
802 stream->stream_event_context =
803 container_of(definition, struct definition_struct, p);
804 stream->parent_def_scope = stream->stream_event_context->p.scope;
805 }
806 stream->events_by_id = g_ptr_array_new();
807 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
808 for (i = 0; i < stream->events_by_id->len; i++) {
809 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
810 struct ctf_stream_event *stream_event;
811
812 if (!event)
813 continue;
814 stream_event = create_event_definitions(td, stream, event);
815 if (!stream_event)
816 goto error_event;
817 g_ptr_array_index(stream->events_by_id, i) = stream_event;
818 }
819 return 0;
820
821 error_event:
822 for (i = 0; i < stream->events_by_id->len; i++) {
823 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
824 if (stream_event)
825 g_free(stream_event);
826 }
827 g_ptr_array_free(stream->events_by_id, TRUE);
828 error:
829 if (stream->stream_event_context)
830 definition_unref(&stream->stream_event_context->p);
831 if (stream->stream_event_header)
832 definition_unref(&stream->stream_event_header->p);
833 if (stream->stream_packet_context)
834 definition_unref(&stream->stream_packet_context->p);
835 return ret;
836 }
837
838
839 static
840 int create_stream_packet_index(struct ctf_trace *td,
841 struct ctf_file_stream *file_stream)
842 {
843 struct ctf_stream_class *stream;
844 int len_index;
845 struct ctf_stream_pos *pos;
846 struct stat filestats;
847 struct packet_index packet_index;
848 int first_packet = 1;
849 int ret;
850
851 pos = &file_stream->pos;
852
853 ret = fstat(pos->fd, &filestats);
854 if (ret < 0)
855 return ret;
856
857 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
858 return -EINVAL;
859
860 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
861 uint64_t stream_id = 0;
862
863 if (pos->base) {
864 /* unmap old base */
865 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
866 if (ret) {
867 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
868 strerror(errno));
869 return ret;
870 }
871 pos->base = NULL;
872 }
873 /* map new base. Need mapping length from header. */
874 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
875 MAP_PRIVATE, pos->fd, pos->mmap_offset);
876 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
877 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
878 pos->offset = 0; /* Position of the packet header */
879
880 packet_index.offset = pos->mmap_offset;
881 packet_index.content_size = 0;
882 packet_index.packet_size = 0;
883 packet_index.timestamp_begin = 0;
884 packet_index.timestamp_end = 0;
885
886 /* read and check header, set stream id (and check) */
887 if (file_stream->parent.trace_packet_header) {
888 /* Read packet header */
889 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
890 if (ret)
891 return ret;
892 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
893 if (len_index >= 0) {
894 struct definition_integer *defint;
895 struct definition *field;
896
897 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
898 assert(field->declaration->id == CTF_TYPE_INTEGER);
899 defint = container_of(field, struct definition_integer, p);
900 assert(defint->declaration->signedness == FALSE);
901 if (defint->value._unsigned != CTF_MAGIC) {
902 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
903 defint->value._unsigned,
904 file_stream->pos.packet_index->len,
905 (ssize_t) pos->mmap_offset);
906 return -EINVAL;
907 }
908 }
909
910 /* check uuid */
911 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
912 if (len_index >= 0) {
913 struct definition_array *defarray;
914 struct definition *field;
915 uint64_t i;
916 uint8_t uuidval[UUID_LEN];
917
918 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
919 assert(field->declaration->id == CTF_TYPE_ARRAY);
920 defarray = container_of(field, struct definition_array, p);
921 assert(array_len(defarray) == UUID_LEN);
922 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
923
924 for (i = 0; i < UUID_LEN; i++) {
925 struct definition *elem;
926 struct definition_integer *defint;
927
928 elem = array_index(defarray, i);
929 assert(elem);
930 defint = container_of(elem, struct definition_integer, p);
931 uuidval[i] = defint->value._unsigned;
932 }
933 ret = uuid_compare(td->uuid, uuidval);
934 if (ret) {
935 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
936 return -EINVAL;
937 }
938 }
939
940
941 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
942 if (len_index >= 0) {
943 struct definition_integer *defint;
944 struct definition *field;
945
946 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
947 assert(field->declaration->id == CTF_TYPE_INTEGER);
948 defint = container_of(field, struct definition_integer, p);
949 assert(defint->declaration->signedness == FALSE);
950 stream_id = defint->value._unsigned;
951 }
952 }
953
954 if (!first_packet && file_stream->parent.stream_id != stream_id) {
955 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
956 return -EINVAL;
957 }
958 if (first_packet) {
959 file_stream->parent.stream_id = stream_id;
960 if (stream_id >= td->streams->len) {
961 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
962 return -EINVAL;
963 }
964 stream = g_ptr_array_index(td->streams, stream_id);
965 if (!stream) {
966 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
967 return -EINVAL;
968 }
969 file_stream->parent.stream_class = stream;
970 ret = create_stream_definitions(td, &file_stream->parent);
971 if (ret)
972 return ret;
973 }
974 first_packet = 0;
975
976 if (file_stream->parent.stream_packet_context) {
977 /* Read packet context */
978 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
979 if (ret)
980 return ret;
981 /* read content size from header */
982 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_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.content_size = defint->value._unsigned;
992 } else {
993 /* Use file size for packet size */
994 packet_index.content_size = filestats.st_size * CHAR_BIT;
995 }
996
997 /* read packet size from header */
998 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
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.packet_size = defint->value._unsigned;
1008 } else {
1009 /* Use content size if non-zero, else file size */
1010 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1011 }
1012
1013 /* read timestamp begin from header */
1014 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1015 if (len_index >= 0) {
1016 struct definition_integer *defint;
1017 struct definition *field;
1018
1019 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1020 assert(field->declaration->id == CTF_TYPE_INTEGER);
1021 defint = container_of(field, struct definition_integer, p);
1022 assert(defint->declaration->signedness == FALSE);
1023 packet_index.timestamp_begin = defint->value._unsigned;
1024 }
1025
1026 /* read timestamp end from header */
1027 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1028 if (len_index >= 0) {
1029 struct definition_integer *defint;
1030 struct definition *field;
1031
1032 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1033 assert(field->declaration->id == CTF_TYPE_INTEGER);
1034 defint = container_of(field, struct definition_integer, p);
1035 assert(defint->declaration->signedness == FALSE);
1036 packet_index.timestamp_end = defint->value._unsigned;
1037 }
1038 } else {
1039 /* Use file size for packet size */
1040 packet_index.content_size = filestats.st_size * CHAR_BIT;
1041 /* Use content size if non-zero, else file size */
1042 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1043 }
1044
1045 /* Validate content size and packet size values */
1046 if (packet_index.content_size > packet_index.packet_size) {
1047 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1048 packet_index.content_size, packet_index.packet_size);
1049 return -EINVAL;
1050 }
1051
1052 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1053 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1054 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
1055 return -EINVAL;
1056 }
1057
1058 /* Save position after header and context */
1059 packet_index.data_offset = pos->offset;
1060
1061 /* add index to packet array */
1062 g_array_append_val(file_stream->pos.packet_index, packet_index);
1063
1064 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1065 }
1066
1067 /* Move pos back to beginning of file */
1068 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1069
1070 return 0;
1071 }
1072
1073 static
1074 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1075 {
1076 int ret;
1077
1078 if (td->packet_header_decl) {
1079 struct definition *definition =
1080 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1081 stream->parent_def_scope, 0, 0, "trace.packet.header");
1082 if (!definition) {
1083 ret = -EINVAL;
1084 goto error;
1085 }
1086 stream->trace_packet_header =
1087 container_of(definition, struct definition_struct, p);
1088 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1089 }
1090
1091 return 0;
1092
1093 error:
1094 return ret;
1095 }
1096
1097 /*
1098 * Note: many file streams can inherit from the same stream class
1099 * description (metadata).
1100 */
1101 static
1102 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1103 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1104 int whence))
1105 {
1106 int ret;
1107 struct ctf_file_stream *file_stream;
1108
1109 ret = openat(td->dirfd, path, flags);
1110 if (ret < 0)
1111 goto error;
1112 file_stream = g_new0(struct ctf_file_stream, 1);
1113
1114 if (move_pos_slow) {
1115 file_stream->pos.move_pos_slow = move_pos_slow;
1116 } else {
1117 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1118 ret = -1;
1119 goto error_def;
1120 }
1121
1122 ctf_init_pos(&file_stream->pos, ret, flags);
1123 ret = create_trace_definitions(td, &file_stream->parent);
1124 if (ret)
1125 goto error_def;
1126 ret = create_stream_packet_index(td, file_stream);
1127 if (ret)
1128 goto error_index;
1129 /* Add stream file to stream class */
1130 g_ptr_array_add(file_stream->parent.stream_class->streams,
1131 &file_stream->parent);
1132 return 0;
1133
1134 error_index:
1135 if (file_stream->parent.trace_packet_header)
1136 definition_unref(&file_stream->parent.trace_packet_header->p);
1137 error_def:
1138 ctf_fini_pos(&file_stream->pos);
1139 close(file_stream->pos.fd);
1140 g_free(file_stream);
1141 error:
1142 return ret;
1143 }
1144
1145 static
1146 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags,
1147 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1148 int whence), FILE *metadata_fp)
1149 {
1150 int ret;
1151 struct dirent *dirent;
1152 struct dirent *diriter;
1153 size_t dirent_len;
1154
1155 td->flags = flags;
1156
1157 /* Open trace directory */
1158 td->dir = opendir(path);
1159 if (!td->dir) {
1160 fprintf(stdout, "[error] Unable to open trace directory.\n");
1161 ret = -ENOENT;
1162 goto error;
1163 }
1164
1165 td->dirfd = open(path, 0);
1166 if (td->dirfd < 0) {
1167 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
1168 ret = -ENOENT;
1169 goto error_dirfd;
1170 }
1171
1172 /*
1173 * Keep the metadata file separate.
1174 */
1175
1176 ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp);
1177 if (ret) {
1178 goto error_metadata;
1179 }
1180
1181 /*
1182 * Open each stream: for each file, try to open, check magic
1183 * number, and get the stream ID to add to the right location in
1184 * the stream array.
1185 */
1186
1187 dirent_len = offsetof(struct dirent, d_name) +
1188 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1189
1190 dirent = malloc(dirent_len);
1191
1192 for (;;) {
1193 ret = readdir_r(td->dir, dirent, &diriter);
1194 if (ret) {
1195 fprintf(stdout, "[error] Readdir error.\n");
1196 goto readdir_error;
1197 }
1198 if (!diriter)
1199 break;
1200 /* Ignore hidden files, ., .. and metadata. */
1201 if (!strncmp(diriter->d_name, ".", 1)
1202 || !strcmp(diriter->d_name, "..")
1203 || !strcmp(diriter->d_name, "metadata"))
1204 continue;
1205 ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow);
1206 if (ret) {
1207 fprintf(stdout, "[error] Open file stream error.\n");
1208 goto readdir_error;
1209 }
1210 }
1211
1212 free(dirent);
1213 return 0;
1214
1215 readdir_error:
1216 free(dirent);
1217 error_metadata:
1218 close(td->dirfd);
1219 error_dirfd:
1220 closedir(td->dir);
1221 error:
1222 return ret;
1223 }
1224
1225 static
1226 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1227 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1228 int whence), FILE *metadata_fp)
1229 {
1230 struct ctf_trace *td;
1231 int ret;
1232
1233 td = g_new0(struct ctf_trace, 1);
1234
1235 switch (flags & O_ACCMODE) {
1236 case O_RDONLY:
1237 if (!path) {
1238 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1239 goto error;
1240 }
1241 ret = ctf_open_trace_read(td, path, flags, move_pos_slow, metadata_fp);
1242 if (ret)
1243 goto error;
1244 break;
1245 case O_RDWR:
1246 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1247 goto error;
1248 default:
1249 fprintf(stdout, "[error] Incorrect open flags.\n");
1250 goto error;
1251 }
1252
1253 return &td->parent;
1254 error:
1255 g_free(td);
1256 return NULL;
1257 }
1258
1259 static
1260 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1261 {
1262 ctf_fini_pos(&file_stream->pos);
1263 close(file_stream->pos.fd);
1264 }
1265
1266 static
1267 void ctf_close_trace(struct trace_descriptor *tdp)
1268 {
1269 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1270 int i;
1271
1272 if (td->streams) {
1273 for (i = 0; i < td->streams->len; i++) {
1274 struct ctf_stream_class *stream;
1275 int j;
1276
1277 stream = g_ptr_array_index(td->streams, i);
1278 if (!stream)
1279 continue;
1280 for (j = 0; j < stream->streams->len; j++) {
1281 struct ctf_file_stream *file_stream;
1282 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1283 ctf_close_file_stream(file_stream);
1284 }
1285
1286 }
1287 g_ptr_array_free(td->streams, TRUE);
1288 }
1289 closedir(td->dir);
1290 g_free(td);
1291 }
1292
1293 void __attribute__((constructor)) ctf_init(void)
1294 {
1295 int ret;
1296
1297 ctf_format.name = g_quark_from_static_string("ctf");
1298 ret = bt_register_format(&ctf_format);
1299 assert(!ret);
1300 }
1301
1302 /* TODO: finalize */
This page took 0.066159 seconds and 5 git commands to generate.