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