Fix read of text-based metadata traces
[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 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
462 ret = 1;
463 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
464 LITTLE_ENDIAN : BIG_ENDIAN;
465 }
466 CTF_TRACE_SET_FIELD(td, byte_order);
467 end:
468 rewind(fp);
469 return ret;
470 }
471
472 static
473 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
474 FILE *out)
475 {
476 struct metadata_packet_header header;
477 size_t readlen, writelen, toread;
478 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
479 int ret = 0;
480
481 readlen = fread(&header, header_sizeof(header), 1, in);
482 if (readlen < 1)
483 return -EINVAL;
484
485 if (td->byte_order != BYTE_ORDER) {
486 header.magic = GUINT32_SWAP_LE_BE(header.magic);
487 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
488 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
489 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
490 }
491 if (header.checksum)
492 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
493 if (header.compression_scheme) {
494 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
495 header.compression_scheme);
496 return -EINVAL;
497 }
498 if (header.encryption_scheme) {
499 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
500 header.encryption_scheme);
501 return -EINVAL;
502 }
503 if (header.checksum_scheme) {
504 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
505 header.checksum_scheme);
506 return -EINVAL;
507 }
508 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
509 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
510 CTF_TRACE_SET_FIELD(td, uuid);
511 } else {
512 if (uuid_compare(header.uuid, td->uuid))
513 return -EINVAL;
514 }
515
516 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
517
518 for (;;) {
519 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
520 if (ferror(in)) {
521 ret = -EINVAL;
522 break;
523 }
524 if (babeltrace_debug) {
525 buf[readlen] = '\0';
526 fprintf(stdout, "[debug] metadata packet read: %s\n",
527 buf);
528 }
529
530 writelen = fwrite(buf, sizeof(char), readlen, out);
531 if (writelen < readlen) {
532 ret = -EIO;
533 break;
534 }
535 if (ferror(out)) {
536 ret = -EINVAL;
537 break;
538 }
539 toread -= readlen;
540 if (!toread) {
541 ret = 0; /* continue reading next packet */
542 break;
543 }
544 }
545 return ret;
546 }
547
548 static
549 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
550 char **buf)
551 {
552 FILE *in, *out;
553 size_t size;
554 int ret;
555
556 in = *fp;
557 /*
558 * Using strlen on *buf instead of size of open_memstream
559 * because its size includes garbage at the end (after final
560 * \0). This is the allocated size, not the actual string size.
561 */
562 out = open_memstream(buf, &size);
563 if (out == NULL)
564 return -errno;
565
566 for (;;) {
567 ret = ctf_open_trace_metadata_packet_read(td, in, out);
568 if (ret) {
569 break;
570 }
571 if (feof(in)) {
572 ret = 0;
573 break;
574 }
575 }
576 fclose(out); /* flush the buffer */
577 fclose(in);
578 /* open for reading */
579 *fp = fmemopen(*buf, strlen(*buf), "rb");
580 return 0;
581 }
582
583 static
584 int ctf_open_trace_metadata_read(struct ctf_trace *td)
585 {
586 struct ctf_scanner *scanner;
587 struct ctf_file_stream *metadata_stream;
588 FILE *fp;
589 char *buf = NULL;
590 int ret = 0;
591
592 metadata_stream = g_new0(struct ctf_file_stream, 1);
593 td->metadata = &metadata_stream->parent;
594 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
595 if (metadata_stream->pos.fd < 0) {
596 fprintf(stdout, "Unable to open metadata.\n");
597 return metadata_stream->pos.fd;
598 }
599
600 if (babeltrace_debug)
601 yydebug = 1;
602
603 fp = fdopen(metadata_stream->pos.fd, "r");
604 if (!fp) {
605 fprintf(stdout, "[error] Unable to open metadata stream.\n");
606 ret = -errno;
607 goto end_stream;
608 }
609
610 td->byte_order = BYTE_ORDER;
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 }
616
617 scanner = ctf_scanner_alloc(fp);
618 if (!scanner) {
619 fprintf(stdout, "[error] Error allocating scanner\n");
620 ret = -ENOMEM;
621 goto end_scanner_alloc;
622 }
623 ret = ctf_scanner_append_ast(scanner);
624 if (ret) {
625 fprintf(stdout, "[error] Error creating AST\n");
626 goto end;
627 }
628
629 if (babeltrace_debug) {
630 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
631 if (ret) {
632 fprintf(stdout, "[error] Error visiting AST for XML output\n");
633 goto end;
634 }
635 }
636
637 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
638 if (ret) {
639 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
640 goto end;
641 }
642 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
643 td, td->byte_order);
644 if (ret) {
645 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
646 goto end;
647 }
648 end:
649 ctf_scanner_free(scanner);
650 end_scanner_alloc:
651 end_packet_read:
652 fclose(fp);
653 free(buf);
654 end_stream:
655 close(metadata_stream->pos.fd);
656 if (ret)
657 g_free(metadata_stream);
658 return ret;
659 }
660
661 static
662 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
663 struct ctf_stream *stream,
664 struct ctf_event *event)
665 {
666 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
667
668 if (event->context_decl) {
669 struct definition *definition =
670 event->context_decl->p.definition_new(&event->context_decl->p,
671 stream->parent_def_scope, 0, 0, "event.context");
672 if (!definition) {
673 goto error;
674 }
675 stream_event->event_context = container_of(definition,
676 struct definition_struct, p);
677 stream->parent_def_scope = stream_event->event_context->p.scope;
678 }
679 if (event->fields_decl) {
680 struct definition *definition =
681 event->fields_decl->p.definition_new(&event->fields_decl->p,
682 stream->parent_def_scope, 0, 0, "event.fields");
683 if (!definition) {
684 goto error;
685 }
686 stream_event->event_fields = container_of(definition,
687 struct definition_struct, p);
688 stream->parent_def_scope = stream_event->event_fields->p.scope;
689 }
690 return stream_event;
691
692 error:
693 if (stream_event->event_fields)
694 definition_unref(&stream_event->event_fields->p);
695 if (stream_event->event_context)
696 definition_unref(&stream_event->event_context->p);
697 return NULL;
698 }
699
700 static
701 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
702 {
703 struct ctf_stream_class *stream_class;
704 int ret;
705 int i;
706
707 if (stream->stream_definitions_created)
708 return 0;
709
710 stream_class = stream->stream_class;
711
712 if (stream_class->packet_context_decl) {
713 struct definition *definition =
714 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
715 stream->parent_def_scope, 0, 0, "stream.packet.context");
716 if (!definition) {
717 ret = -EINVAL;
718 goto error;
719 }
720 stream->stream_packet_context = container_of(definition,
721 struct definition_struct, p);
722 stream->parent_def_scope = stream->stream_packet_context->p.scope;
723 }
724 if (stream_class->event_header_decl) {
725 struct definition *definition =
726 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
727 stream->parent_def_scope, 0, 0, "stream.event.header");
728 if (!definition) {
729 ret = -EINVAL;
730 goto error;
731 }
732 stream->stream_event_header =
733 container_of(definition, struct definition_struct, p);
734 stream->parent_def_scope = stream->stream_event_header->p.scope;
735 }
736 if (stream_class->event_context_decl) {
737 struct definition *definition =
738 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
739 stream->parent_def_scope, 0, 0, "stream.event.context");
740 if (!definition) {
741 ret = -EINVAL;
742 goto error;
743 }
744 stream->stream_event_context =
745 container_of(definition, struct definition_struct, p);
746 stream->parent_def_scope = stream->stream_event_context->p.scope;
747 }
748 stream->events_by_id = g_ptr_array_new();
749 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
750 for (i = 0; i < stream->events_by_id->len; i++) {
751 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
752 struct ctf_stream_event *stream_event;
753
754 if (!event)
755 continue;
756 stream_event = create_event_definitions(td, stream, event);
757 if (!stream_event)
758 goto error_event;
759 g_ptr_array_index(stream->events_by_id, i) = stream_event;
760 }
761 return 0;
762
763 error_event:
764 for (i = 0; i < stream->events_by_id->len; i++) {
765 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
766 if (stream_event)
767 g_free(stream_event);
768 }
769 g_ptr_array_free(stream->events_by_id, TRUE);
770 error:
771 if (stream->stream_event_context)
772 definition_unref(&stream->stream_event_context->p);
773 if (stream->stream_event_header)
774 definition_unref(&stream->stream_event_header->p);
775 if (stream->stream_packet_context)
776 definition_unref(&stream->stream_packet_context->p);
777 return ret;
778 }
779
780
781 static
782 int create_stream_packet_index(struct ctf_trace *td,
783 struct ctf_file_stream *file_stream)
784 {
785 struct ctf_stream_class *stream;
786 int len_index;
787 struct ctf_stream_pos *pos;
788 struct stat filestats;
789 struct packet_index packet_index;
790 int first_packet = 1;
791 int ret;
792
793 pos = &file_stream->pos;
794
795 ret = fstat(pos->fd, &filestats);
796 if (ret < 0)
797 return ret;
798
799 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
800 return -EINVAL;
801
802 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
803 uint64_t stream_id = 0;
804
805 if (pos->base) {
806 /* unmap old base */
807 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
808 if (ret) {
809 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
810 strerror(errno));
811 return ret;
812 }
813 pos->base = NULL;
814 }
815 /* map new base. Need mapping length from header. */
816 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
817 MAP_PRIVATE, pos->fd, pos->mmap_offset);
818 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
819 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
820 pos->offset = 0; /* Position of the packet header */
821
822 packet_index.offset = pos->mmap_offset;
823 packet_index.content_size = 0;
824 packet_index.packet_size = 0;
825 packet_index.timestamp_begin = 0;
826 packet_index.timestamp_end = 0;
827
828 /* read and check header, set stream id (and check) */
829 if (file_stream->parent.trace_packet_header) {
830 /* Read packet header */
831 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
832 if (ret)
833 return ret;
834 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
835 if (len_index >= 0) {
836 struct definition_integer *defint;
837 struct definition *field;
838
839 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
840 assert(field->declaration->id == CTF_TYPE_INTEGER);
841 defint = container_of(field, struct definition_integer, p);
842 assert(defint->declaration->signedness == FALSE);
843 if (defint->value._unsigned != CTF_MAGIC) {
844 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
845 defint->value._unsigned,
846 file_stream->pos.packet_index->len,
847 (ssize_t) pos->mmap_offset);
848 return -EINVAL;
849 }
850 }
851
852 /* check uuid */
853 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
854 if (len_index >= 0) {
855 struct definition_array *defarray;
856 struct definition *field;
857 uint64_t i;
858 uint8_t uuidval[UUID_LEN];
859
860 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
861 assert(field->declaration->id == CTF_TYPE_ARRAY);
862 defarray = container_of(field, struct definition_array, p);
863 assert(array_len(defarray) == UUID_LEN);
864 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
865
866 for (i = 0; i < UUID_LEN; i++) {
867 struct definition *elem;
868 struct definition_integer *defint;
869
870 elem = array_index(defarray, i);
871 assert(elem);
872 defint = container_of(elem, struct definition_integer, p);
873 uuidval[i] = defint->value._unsigned;
874 }
875 ret = uuid_compare(td->uuid, uuidval);
876 if (ret) {
877 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
878 return -EINVAL;
879 }
880 }
881
882
883 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
884 if (len_index >= 0) {
885 struct definition_integer *defint;
886 struct definition *field;
887
888 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
889 assert(field->declaration->id == CTF_TYPE_INTEGER);
890 defint = container_of(field, struct definition_integer, p);
891 assert(defint->declaration->signedness == FALSE);
892 stream_id = defint->value._unsigned;
893 }
894 }
895
896 if (!first_packet && file_stream->parent.stream_id != stream_id) {
897 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
898 return -EINVAL;
899 }
900 if (first_packet) {
901 file_stream->parent.stream_id = stream_id;
902 if (stream_id >= td->streams->len) {
903 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
904 return -EINVAL;
905 }
906 stream = g_ptr_array_index(td->streams, stream_id);
907 if (!stream) {
908 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
909 return -EINVAL;
910 }
911 file_stream->parent.stream_class = stream;
912 ret = create_stream_definitions(td, &file_stream->parent);
913 if (ret)
914 return ret;
915 }
916 first_packet = 0;
917
918 if (file_stream->parent.stream_packet_context) {
919 /* Read packet context */
920 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
921 if (ret)
922 return ret;
923 /* read content size from header */
924 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
925 if (len_index >= 0) {
926 struct definition_integer *defint;
927 struct definition *field;
928
929 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
930 assert(field->declaration->id == CTF_TYPE_INTEGER);
931 defint = container_of(field, struct definition_integer, p);
932 assert(defint->declaration->signedness == FALSE);
933 packet_index.content_size = defint->value._unsigned;
934 } else {
935 /* Use file size for packet size */
936 packet_index.content_size = filestats.st_size * CHAR_BIT;
937 }
938
939 /* read packet size from header */
940 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
941 if (len_index >= 0) {
942 struct definition_integer *defint;
943 struct definition *field;
944
945 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
946 assert(field->declaration->id == CTF_TYPE_INTEGER);
947 defint = container_of(field, struct definition_integer, p);
948 assert(defint->declaration->signedness == FALSE);
949 packet_index.packet_size = defint->value._unsigned;
950 } else {
951 /* Use content size if non-zero, else file size */
952 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
953 }
954
955 /* read timestamp begin from header */
956 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
957 if (len_index >= 0) {
958 struct definition_integer *defint;
959 struct definition *field;
960
961 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
962 assert(field->declaration->id == CTF_TYPE_INTEGER);
963 defint = container_of(field, struct definition_integer, p);
964 assert(defint->declaration->signedness == FALSE);
965 packet_index.timestamp_begin = defint->value._unsigned;
966 }
967
968 /* read timestamp end from header */
969 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
970 if (len_index >= 0) {
971 struct definition_integer *defint;
972 struct definition *field;
973
974 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
975 assert(field->declaration->id == CTF_TYPE_INTEGER);
976 defint = container_of(field, struct definition_integer, p);
977 assert(defint->declaration->signedness == FALSE);
978 packet_index.timestamp_end = defint->value._unsigned;
979 }
980 } else {
981 /* Use file size for packet size */
982 packet_index.content_size = filestats.st_size * CHAR_BIT;
983 /* Use content size if non-zero, else file size */
984 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
985 }
986
987 /* Validate content size and packet size values */
988 if (packet_index.content_size > packet_index.packet_size) {
989 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
990 packet_index.content_size, packet_index.packet_size);
991 return -EINVAL;
992 }
993
994 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
995 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
996 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
997 return -EINVAL;
998 }
999
1000 /* Save position after header and context */
1001 packet_index.data_offset = pos->offset;
1002
1003 /* add index to packet array */
1004 g_array_append_val(file_stream->pos.packet_index, packet_index);
1005
1006 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1007 }
1008
1009 /* Move pos back to beginning of file */
1010 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1011
1012 return 0;
1013 }
1014
1015 static
1016 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1017 {
1018 int ret;
1019
1020 if (td->packet_header_decl) {
1021 struct definition *definition =
1022 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1023 stream->parent_def_scope, 0, 0, "trace.packet.header");
1024 if (!definition) {
1025 ret = -EINVAL;
1026 goto error;
1027 }
1028 stream->trace_packet_header =
1029 container_of(definition, struct definition_struct, p);
1030 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1031 }
1032
1033 return 0;
1034
1035 error:
1036 return ret;
1037 }
1038
1039 /*
1040 * Note: many file streams can inherit from the same stream class
1041 * description (metadata).
1042 */
1043 static
1044 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
1045 {
1046 int ret;
1047 struct ctf_file_stream *file_stream;
1048
1049 ret = openat(td->dirfd, path, flags);
1050 if (ret < 0)
1051 goto error;
1052 file_stream = g_new0(struct ctf_file_stream, 1);
1053 ctf_init_pos(&file_stream->pos, ret, flags);
1054 ret = create_trace_definitions(td, &file_stream->parent);
1055 if (ret)
1056 goto error_def;
1057 ret = create_stream_packet_index(td, file_stream);
1058 if (ret)
1059 goto error_index;
1060 /* Add stream file to stream class */
1061 g_ptr_array_add(file_stream->parent.stream_class->streams,
1062 &file_stream->parent);
1063 return 0;
1064
1065 error_index:
1066 if (file_stream->parent.trace_packet_header)
1067 definition_unref(&file_stream->parent.trace_packet_header->p);
1068 error_def:
1069 ctf_fini_pos(&file_stream->pos);
1070 close(file_stream->pos.fd);
1071 g_free(file_stream);
1072 error:
1073 return ret;
1074 }
1075
1076 static
1077 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
1078 {
1079 int ret;
1080 struct dirent *dirent;
1081 struct dirent *diriter;
1082 size_t dirent_len;
1083
1084 td->flags = flags;
1085
1086 /* Open trace directory */
1087 td->dir = opendir(path);
1088 if (!td->dir) {
1089 fprintf(stdout, "[error] Unable to open trace directory.\n");
1090 ret = -ENOENT;
1091 goto error;
1092 }
1093
1094 td->dirfd = open(path, 0);
1095 if (td->dirfd < 0) {
1096 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
1097 ret = -ENOENT;
1098 goto error_dirfd;
1099 }
1100
1101 /*
1102 * Keep the metadata file separate.
1103 */
1104
1105 ret = ctf_open_trace_metadata_read(td);
1106 if (ret) {
1107 goto error_metadata;
1108 }
1109
1110 /*
1111 * Open each stream: for each file, try to open, check magic
1112 * number, and get the stream ID to add to the right location in
1113 * the stream array.
1114 */
1115
1116 dirent_len = offsetof(struct dirent, d_name) +
1117 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1118
1119 dirent = malloc(dirent_len);
1120
1121 for (;;) {
1122 ret = readdir_r(td->dir, dirent, &diriter);
1123 if (ret) {
1124 fprintf(stdout, "[error] Readdir error.\n");
1125 goto readdir_error;
1126 }
1127 if (!diriter)
1128 break;
1129 /* Ignore hidden files, ., .. and metadata. */
1130 if (!strncmp(diriter->d_name, ".", 1)
1131 || !strcmp(diriter->d_name, "..")
1132 || !strcmp(diriter->d_name, "metadata"))
1133 continue;
1134 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
1135 if (ret) {
1136 fprintf(stdout, "[error] Open file stream error.\n");
1137 goto readdir_error;
1138 }
1139 }
1140
1141 free(dirent);
1142 return 0;
1143
1144 readdir_error:
1145 free(dirent);
1146 error_metadata:
1147 close(td->dirfd);
1148 error_dirfd:
1149 closedir(td->dir);
1150 error:
1151 return ret;
1152 }
1153
1154 static
1155 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
1156 {
1157 struct ctf_trace *td;
1158 int ret;
1159
1160 td = g_new0(struct ctf_trace, 1);
1161
1162 switch (flags & O_ACCMODE) {
1163 case O_RDONLY:
1164 if (!path) {
1165 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
1166 goto error;
1167 }
1168 ret = ctf_open_trace_read(td, path, flags);
1169 if (ret)
1170 goto error;
1171 break;
1172 case O_RDWR:
1173 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
1174 goto error;
1175 default:
1176 fprintf(stdout, "[error] Incorrect open flags.\n");
1177 goto error;
1178 }
1179
1180 return &td->parent;
1181 error:
1182 g_free(td);
1183 return NULL;
1184 }
1185
1186 static
1187 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1188 {
1189 ctf_fini_pos(&file_stream->pos);
1190 close(file_stream->pos.fd);
1191 }
1192
1193 static
1194 void ctf_close_trace(struct trace_descriptor *tdp)
1195 {
1196 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1197 int i;
1198
1199 if (td->streams) {
1200 for (i = 0; i < td->streams->len; i++) {
1201 struct ctf_stream_class *stream;
1202 int j;
1203
1204 stream = g_ptr_array_index(td->streams, i);
1205 if (!stream)
1206 continue;
1207 for (j = 0; j < stream->streams->len; j++) {
1208 struct ctf_file_stream *file_stream;
1209 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1210 ctf_close_file_stream(file_stream);
1211 }
1212
1213 }
1214 g_ptr_array_free(td->streams, TRUE);
1215 }
1216 closedir(td->dir);
1217 g_free(td);
1218 }
1219
1220 void __attribute__((constructor)) ctf_init(void)
1221 {
1222 int ret;
1223
1224 ctf_format.name = g_quark_from_static_string("ctf");
1225 ret = bt_register_format(&ctf_format);
1226 assert(!ret);
1227 }
1228
1229 /* TODO: finalize */
This page took 0.060291 seconds and 4 git commands to generate.