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