Enum printing fix, enum / variant header handling
[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 struct trace_descriptor *ctf_open_trace(const char *path, int flags);
56 void ctf_close_trace(struct trace_descriptor *descriptor);
57
58 static
59 rw_dispatch read_dispatch_table[] = {
60 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
61 [ CTF_TYPE_FLOAT ] = ctf_float_read,
62 [ CTF_TYPE_ENUM ] = ctf_enum_read,
63 [ CTF_TYPE_STRING ] = ctf_string_read,
64 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
65 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
66 [ CTF_TYPE_ARRAY ] = ctf_array_read,
67 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
68 };
69
70 static
71 rw_dispatch write_dispatch_table[] = {
72 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
73 [ CTF_TYPE_FLOAT ] = ctf_float_write,
74 [ CTF_TYPE_ENUM ] = ctf_enum_write,
75 [ CTF_TYPE_STRING ] = ctf_string_write,
76 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
77 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
78 [ CTF_TYPE_ARRAY ] = ctf_array_write,
79 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
80 };
81
82 static
83 struct format ctf_format = {
84 .open_trace = ctf_open_trace,
85 .close_trace = ctf_close_trace,
86 };
87
88 static
89 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
90 {
91 struct ctf_stream_pos *pos =
92 container_of(ppos, struct ctf_stream_pos, parent);
93 struct ctf_stream_class *stream_class = stream->stream_class;
94 struct ctf_event *event_class;
95 uint64_t id = 0;
96 int ret;
97
98 if (pos->offset == EOF)
99 return EOF;
100
101 /* Read event header */
102 if (stream_class->event_header) {
103 struct definition_integer *integer_definition;
104 struct definition *variant;
105
106 ret = generic_rw(ppos, &stream_class->event_header->p);
107 if (ret)
108 goto error;
109 /* lookup event id */
110 integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE);
111 if (integer_definition) {
112 id = integer_definition->value._unsigned;
113 } else {
114 struct definition_enum *enum_definition;
115
116 enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE);
117 if (enum_definition) {
118 id = enum_definition->integer->value._unsigned;
119 }
120 }
121
122 variant = lookup_variant(&stream_class->event_header->p, "v");
123 if (variant) {
124 integer_definition = lookup_integer(variant, "id", FALSE);
125 if (integer_definition) {
126 id = integer_definition->value._unsigned;
127 }
128 }
129
130 /* lookup timestamp */
131 integer_definition = lookup_integer(&stream_class->event_header->p, "timestamp", FALSE);
132 if (integer_definition) {
133 stream->timestamp = integer_definition->value._unsigned;
134 } else {
135 if (variant) {
136 integer_definition = lookup_integer(variant, "timestamp", FALSE);
137 if (integer_definition) {
138 stream->timestamp = integer_definition->value._unsigned;
139 }
140 }
141 }
142 }
143
144 /* Read stream-declared event context */
145 if (stream_class->event_context) {
146 ret = generic_rw(ppos, &stream_class->event_context->p);
147 if (ret)
148 goto error;
149 }
150
151 if (id >= stream_class->events_by_id->len) {
152 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
153 return -EINVAL;
154 }
155 event_class = g_ptr_array_index(stream_class->events_by_id, id);
156 if (!event_class) {
157 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
158 return -EINVAL;
159 }
160
161 /* Read event-declared event context */
162 if (event_class->context) {
163 ret = generic_rw(ppos, &event_class->context->p);
164 if (ret)
165 goto error;
166 }
167
168 /* Read event payload */
169 if (event_class->fields) {
170 ret = generic_rw(ppos, &event_class->fields->p);
171 if (ret)
172 goto error;
173 }
174
175 return 0;
176
177 error:
178 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
179 return ret;
180 }
181
182 static
183 int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
184 {
185 struct ctf_stream_class *stream_class = stream->stream_class;
186 struct ctf_event *event_class;
187 uint64_t id = 0;
188 int ret;
189
190 /* print event header */
191 if (stream_class->event_header) {
192 struct definition_integer *integer_definition;
193 struct definition *variant;
194
195 /* lookup event id */
196 integer_definition = lookup_integer(&stream_class->event_header->p, "id", FALSE);
197 if (integer_definition) {
198 id = integer_definition->value._unsigned;
199 } else {
200 struct definition_enum *enum_definition;
201
202 enum_definition = lookup_enum(&stream_class->event_header->p, "id", FALSE);
203 if (enum_definition) {
204 id = enum_definition->integer->value._unsigned;
205 }
206 }
207
208 variant = lookup_variant(&stream_class->event_header->p, "v");
209 if (variant) {
210 integer_definition = lookup_integer(variant, "id", FALSE);
211 if (integer_definition) {
212 id = integer_definition->value._unsigned;
213 }
214 }
215
216 ret = generic_rw(pos, &stream_class->event_header->p);
217 if (ret)
218 goto error;
219 }
220
221 /* print stream-declared event context */
222 if (stream_class->event_context) {
223 ret = generic_rw(pos, &stream_class->event_context->p);
224 if (ret)
225 goto error;
226 }
227
228 if (id >= stream_class->events_by_id->len) {
229 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
230 return -EINVAL;
231 }
232 event_class = g_ptr_array_index(stream_class->events_by_id, id);
233 if (!event_class) {
234 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
235 return -EINVAL;
236 }
237
238 /* print event-declared event context */
239 if (event_class->context) {
240 ret = generic_rw(pos, &event_class->context->p);
241 if (ret)
242 goto error;
243 }
244
245 /* Read and print event payload */
246 if (event_class->fields) {
247 ret = generic_rw(pos, &event_class->fields->p);
248 if (ret)
249 goto error;
250 }
251
252 return 0;
253
254 error:
255 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
256 return ret;
257 }
258
259 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
260 {
261 pos->fd = fd;
262 pos->mmap_offset = 0;
263 pos->packet_size = 0;
264 pos->content_size = 0;
265 pos->content_size_loc = NULL;
266 pos->base = NULL;
267 pos->offset = 0;
268 pos->dummy = false;
269 pos->cur_index = 0;
270 if (fd >= 0)
271 pos->packet_index = g_array_new(FALSE, TRUE,
272 sizeof(struct packet_index));
273 else
274 pos->packet_index = NULL;
275 switch (open_flags & O_ACCMODE) {
276 case O_RDONLY:
277 pos->prot = PROT_READ;
278 pos->flags = MAP_PRIVATE;
279 pos->parent.rw_table = read_dispatch_table;
280 pos->parent.event_cb = ctf_read_event;
281 break;
282 case O_RDWR:
283 pos->prot = PROT_WRITE; /* Write has priority */
284 pos->flags = MAP_SHARED;
285 pos->parent.rw_table = write_dispatch_table;
286 pos->parent.event_cb = ctf_write_event;
287 if (fd >= 0)
288 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
289 break;
290 default:
291 assert(0);
292 }
293 }
294
295 void ctf_fini_pos(struct ctf_stream_pos *pos)
296 {
297 int ret;
298
299 if (pos->prot == PROT_WRITE && pos->content_size_loc)
300 *pos->content_size_loc = pos->offset;
301 if (pos->base) {
302 /* unmap old base */
303 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
304 if (ret) {
305 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
306 strerror(errno));
307 assert(0);
308 }
309 }
310 (void) g_array_free(pos->packet_index, TRUE);
311 }
312
313 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
314 {
315 int ret;
316 off_t off;
317 struct packet_index *index;
318
319 if (pos->prot == PROT_WRITE && pos->content_size_loc)
320 *pos->content_size_loc = pos->offset;
321
322 if (pos->base) {
323 /* unmap old base */
324 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
325 if (ret) {
326 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
327 strerror(errno));
328 assert(0);
329 }
330 pos->base = NULL;
331 }
332
333 /*
334 * The caller should never ask for ctf_move_pos across packets,
335 * except to get exactly at the beginning of the next packet.
336 */
337 if (pos->prot == PROT_WRITE) {
338 switch (whence) {
339 case SEEK_CUR:
340 /* The writer will add padding */
341 assert(pos->offset + offset == pos->packet_size);
342 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
343 break;
344 case SEEK_SET:
345 assert(offset == 0); /* only seek supported for now */
346 pos->cur_index = 0;
347 break;
348 default:
349 assert(0);
350 }
351 pos->content_size = -1U; /* Unknown at this point */
352 pos->packet_size = WRITE_PACKET_LEN;
353 off = posix_fallocate(pos->fd, pos->mmap_offset,
354 pos->packet_size / CHAR_BIT);
355 assert(off >= 0);
356 pos->offset = 0;
357 } else {
358 switch (whence) {
359 case SEEK_CUR:
360 /* The reader will expect us to skip padding */
361 assert(pos->offset + offset == pos->content_size);
362 ++pos->cur_index;
363 break;
364 case SEEK_SET:
365 assert(offset == 0); /* only seek supported for now */
366 pos->cur_index = 0;
367 break;
368 default:
369 assert(0);
370 }
371 if (pos->cur_index >= pos->packet_index->len) {
372 pos->offset = EOF;
373 return;
374 }
375 index = &g_array_index(pos->packet_index, struct packet_index,
376 pos->cur_index);
377 pos->mmap_offset = index->offset;
378
379 /* Lookup context/packet size in index */
380 pos->content_size = index->content_size;
381 pos->packet_size = index->packet_size;
382 pos->offset = index->data_offset;
383 }
384 /* map new base. Need mapping length from header. */
385 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
386 pos->flags, pos->fd, pos->mmap_offset);
387 if (pos->base == MAP_FAILED) {
388 fprintf(stdout, "[error] mmap error %s.\n",
389 strerror(errno));
390 assert(0);
391 }
392 }
393
394 static
395 int packet_metadata(struct ctf_trace *td, FILE *fp)
396 {
397 uint32_t magic;
398 size_t len;
399 int ret = 0;
400
401 len = fread(&magic, sizeof(magic), 1, fp);
402 if (len != 1) {
403 goto end;
404 }
405 if (magic == TSDL_MAGIC) {
406 ret = 1;
407 td->byte_order = BYTE_ORDER;
408 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
409 ret = 1;
410 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
411 LITTLE_ENDIAN : BIG_ENDIAN;
412 }
413 CTF_TRACE_SET_FIELD(td, byte_order);
414 end:
415 rewind(fp);
416 return ret;
417 }
418
419 static
420 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
421 FILE *out)
422 {
423 struct metadata_packet_header header;
424 size_t readlen, writelen, toread;
425 char buf[4096];
426 int ret = 0;
427
428 readlen = fread(&header, header_sizeof(header), 1, in);
429 if (readlen < 1)
430 return -EINVAL;
431
432 if (td->byte_order != BYTE_ORDER) {
433 header.magic = GUINT32_SWAP_LE_BE(header.magic);
434 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
435 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
436 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
437 }
438 if (header.checksum)
439 fprintf(stdout, "[warning] checksum verification not supported yet.\n");
440 if (header.compression_scheme) {
441 fprintf(stdout, "[error] compression (%u) not supported yet.\n",
442 header.compression_scheme);
443 return -EINVAL;
444 }
445 if (header.encryption_scheme) {
446 fprintf(stdout, "[error] encryption (%u) not supported yet.\n",
447 header.encryption_scheme);
448 return -EINVAL;
449 }
450 if (header.checksum_scheme) {
451 fprintf(stdout, "[error] checksum (%u) not supported yet.\n",
452 header.checksum_scheme);
453 return -EINVAL;
454 }
455 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
456 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
457 CTF_TRACE_SET_FIELD(td, uuid);
458 } else {
459 if (uuid_compare(header.uuid, td->uuid))
460 return -EINVAL;
461 }
462
463 toread = header.content_size / CHAR_BIT;
464
465 for (;;) {
466 readlen = fread(buf, sizeof(char), min(sizeof(buf), toread), in);
467 if (ferror(in)) {
468 ret = -EINVAL;
469 break;
470 }
471 if (babeltrace_debug) {
472 fprintf(stdout, "[debug] metadata packet read: %s\n",
473 buf);
474 }
475
476 writelen = fwrite(buf, sizeof(char), readlen, out);
477 if (writelen < readlen) {
478 ret = -EIO;
479 break;
480 }
481 if (ferror(out)) {
482 ret = -EINVAL;
483 break;
484 }
485 toread -= readlen;
486 if (!toread) {
487 ret = 0; /* continue reading next packet */
488 break;
489 }
490 }
491 return ret;
492 }
493
494 static
495 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
496 char **buf)
497 {
498 FILE *in, *out;
499 size_t size;
500 int ret;
501
502 in = *fp;
503 /*
504 * Using strlen on *buf instead of size of open_memstream
505 * because its size includes garbage at the end (after final
506 * \0). This is the allocated size, not the actual string size.
507 */
508 out = open_memstream(buf, &size);
509 if (out == NULL)
510 return -errno;
511
512 for (;;) {
513 ret = ctf_open_trace_metadata_packet_read(td, in, out);
514 if (ret) {
515 break;
516 }
517 if (feof(in)) {
518 ret = 0;
519 break;
520 }
521 }
522 fclose(out); /* flush the buffer */
523 fclose(in);
524 /* open for reading */
525 *fp = fmemopen(*buf, strlen(*buf), "rb");
526 return 0;
527 }
528
529 static
530 int ctf_open_trace_metadata_read(struct ctf_trace *td)
531 {
532 struct ctf_scanner *scanner;
533 FILE *fp;
534 char *buf = NULL;
535 int ret = 0;
536
537 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
538 if (td->metadata.pos.fd < 0) {
539 fprintf(stdout, "Unable to open metadata.\n");
540 return td->metadata.pos.fd;
541 }
542
543 if (babeltrace_debug)
544 yydebug = 1;
545
546 fp = fdopen(td->metadata.pos.fd, "r");
547 if (!fp) {
548 fprintf(stdout, "[error] Unable to open metadata stream.\n");
549 ret = -errno;
550 goto end_stream;
551 }
552
553 if (packet_metadata(td, fp)) {
554 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
555 if (ret)
556 goto end_packet_read;
557 }
558
559 scanner = ctf_scanner_alloc(fp);
560 if (!scanner) {
561 fprintf(stdout, "[error] Error allocating scanner\n");
562 ret = -ENOMEM;
563 goto end_scanner_alloc;
564 }
565 ret = ctf_scanner_append_ast(scanner);
566 if (ret) {
567 fprintf(stdout, "[error] Error creating AST\n");
568 goto end;
569 }
570
571 if (babeltrace_debug) {
572 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
573 if (ret) {
574 fprintf(stdout, "[error] Error visiting AST for XML output\n");
575 goto end;
576 }
577 }
578
579 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
580 if (ret) {
581 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
582 goto end;
583 }
584 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
585 td, BYTE_ORDER);
586 if (ret) {
587 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
588 goto end;
589 }
590 end:
591 ctf_scanner_free(scanner);
592 end_scanner_alloc:
593 end_packet_read:
594 fclose(fp);
595 free(buf);
596 end_stream:
597 close(td->metadata.pos.fd);
598 return ret;
599 }
600
601
602 static
603 int create_stream_packet_index(struct ctf_trace *td,
604 struct ctf_file_stream *file_stream)
605 {
606 struct ctf_stream_class *stream;
607 int len_index;
608 struct ctf_stream_pos *pos;
609 struct stat filestats;
610 struct packet_index packet_index;
611 int first_packet = 1;
612 int ret;
613
614 pos = &file_stream->pos;
615
616 ret = fstat(pos->fd, &filestats);
617 if (ret < 0)
618 return ret;
619
620 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
621 uint64_t stream_id = 0;
622
623 if (pos->base) {
624 /* unmap old base */
625 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
626 if (ret) {
627 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
628 strerror(errno));
629 return ret;
630 }
631 pos->base = NULL;
632 }
633 /* map new base. Need mapping length from header. */
634 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
635 MAP_PRIVATE, pos->fd, pos->mmap_offset);
636 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
637 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
638 pos->offset = 0; /* Position of the packet header */
639
640 packet_index.offset = pos->mmap_offset;
641 packet_index.content_size = 0;
642 packet_index.packet_size = 0;
643
644 /* read and check header, set stream id (and check) */
645 if (td->packet_header) {
646 /* Read packet header */
647 ret = generic_rw(&pos->parent, &td->packet_header->p);
648 if (ret)
649 return ret;
650 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
651 if (len_index >= 0) {
652 struct definition_integer *defint;
653 struct definition *field;
654
655 field = struct_definition_get_field_from_index(td->packet_header, len_index);
656 assert(field->declaration->id == CTF_TYPE_INTEGER);
657 defint = container_of(field, struct definition_integer, p);
658 assert(defint->declaration->signedness == FALSE);
659 if (defint->value._unsigned != CTF_MAGIC) {
660 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
661 defint->value._unsigned,
662 file_stream->pos.packet_index->len,
663 (ssize_t) pos->mmap_offset);
664 return -EINVAL;
665 }
666 }
667
668 /* check uuid */
669 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("uuid"));
670 if (len_index >= 0) {
671 struct definition_array *defarray;
672 struct definition *field;
673 uint64_t i;
674 uint8_t uuidval[UUID_LEN];
675
676 field = struct_definition_get_field_from_index(td->packet_header, len_index);
677 assert(field->declaration->id == CTF_TYPE_ARRAY);
678 defarray = container_of(field, struct definition_array, p);
679 assert(array_len(defarray) == UUID_LEN);
680 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
681
682 for (i = 0; i < UUID_LEN; i++) {
683 struct definition *elem;
684 struct definition_integer *defint;
685
686 elem = array_index(defarray, i);
687 assert(elem);
688 defint = container_of(elem, struct definition_integer, p);
689 uuidval[i] = defint->value._unsigned;
690 }
691 ret = uuid_compare(td->uuid, uuidval);
692 if (ret) {
693 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
694 return -EINVAL;
695 }
696 }
697
698
699 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
700 if (len_index >= 0) {
701 struct definition_integer *defint;
702 struct definition *field;
703
704 field = struct_definition_get_field_from_index(td->packet_header, len_index);
705 assert(field->declaration->id == CTF_TYPE_INTEGER);
706 defint = container_of(field, struct definition_integer, p);
707 assert(defint->declaration->signedness == FALSE);
708 stream_id = defint->value._unsigned;
709 }
710 }
711
712 if (!first_packet && file_stream->stream_id != stream_id) {
713 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
714 return -EINVAL;
715 }
716 if (first_packet) {
717 file_stream->stream_id = stream_id;
718 if (stream_id >= td->streams->len) {
719 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
720 return -EINVAL;
721 }
722 stream = g_ptr_array_index(td->streams, stream_id);
723 if (!stream) {
724 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
725 return -EINVAL;
726 }
727 file_stream->stream.stream_class = stream;
728 }
729 first_packet = 0;
730
731 if (stream->packet_context) {
732 /* Read packet context */
733 ret = generic_rw(&pos->parent, &stream->packet_context->p);
734 if (ret)
735 return ret;
736 /* read content size from header */
737 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
738 if (len_index >= 0) {
739 struct definition_integer *defint;
740 struct definition *field;
741
742 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
743 assert(field->declaration->id == CTF_TYPE_INTEGER);
744 defint = container_of(field, struct definition_integer, p);
745 assert(defint->declaration->signedness == FALSE);
746 packet_index.content_size = defint->value._unsigned;
747 } else {
748 /* Use file size for packet size */
749 packet_index.content_size = filestats.st_size * CHAR_BIT;
750 }
751
752 /* read packet size from header */
753 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
754 if (len_index >= 0) {
755 struct definition_integer *defint;
756 struct definition *field;
757
758 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
759 assert(field->declaration->id == CTF_TYPE_INTEGER);
760 defint = container_of(field, struct definition_integer, p);
761 assert(defint->declaration->signedness == FALSE);
762 packet_index.packet_size = defint->value._unsigned;
763 } else {
764 /* Use content size if non-zero, else file size */
765 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
766 }
767 } else {
768 /* Use file size for packet size */
769 packet_index.content_size = filestats.st_size * CHAR_BIT;
770 /* Use content size if non-zero, else file size */
771 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
772 }
773
774 /* Validate content size and packet size values */
775 if (packet_index.content_size > packet_index.packet_size) {
776 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
777 packet_index.content_size, packet_index.packet_size);
778 return -EINVAL;
779 }
780
781 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
782 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
783 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
784 return -EINVAL;
785 }
786
787 /* Save position after header and context */
788 packet_index.data_offset = pos->offset;
789
790 /* add index to packet array */
791 g_array_append_val(file_stream->pos.packet_index, packet_index);
792
793 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
794 }
795
796 /* Move pos back to beginning of file */
797 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
798
799 return 0;
800 }
801
802 /*
803 * Note: many file streams can inherit from the same stream class
804 * description (metadata).
805 */
806 static
807 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
808 {
809 int ret;
810 struct ctf_file_stream *file_stream;
811
812 ret = openat(td->dirfd, path, flags);
813 if (ret < 0)
814 goto error;
815 file_stream = g_new0(struct ctf_file_stream, 1);
816 ctf_init_pos(&file_stream->pos, ret, flags);
817 ret = create_stream_packet_index(td, file_stream);
818 if (ret)
819 goto error_index;
820 /* Add stream file to stream class */
821 g_ptr_array_add(file_stream->stream.stream_class->files, file_stream);
822 return 0;
823
824 error_index:
825 ctf_fini_pos(&file_stream->pos);
826 close(file_stream->pos.fd);
827 g_free(file_stream);
828 error:
829 return ret;
830 }
831
832 static
833 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
834 {
835 int ret;
836 struct dirent *dirent;
837 struct dirent *diriter;
838 size_t dirent_len;
839
840 td->flags = flags;
841
842 /* Open trace directory */
843 td->dir = opendir(path);
844 if (!td->dir) {
845 fprintf(stdout, "[error] Unable to open trace directory.\n");
846 ret = -ENOENT;
847 goto error;
848 }
849
850 td->dirfd = open(path, 0);
851 if (td->dirfd < 0) {
852 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
853 ret = -ENOENT;
854 goto error_dirfd;
855 }
856
857 /*
858 * Keep the metadata file separate.
859 */
860
861 ret = ctf_open_trace_metadata_read(td);
862 if (ret) {
863 goto error_metadata;
864 }
865
866 /*
867 * Open each stream: for each file, try to open, check magic
868 * number, and get the stream ID to add to the right location in
869 * the stream array.
870 */
871
872 dirent_len = offsetof(struct dirent, d_name) +
873 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
874
875 dirent = malloc(dirent_len);
876
877 for (;;) {
878 ret = readdir_r(td->dir, dirent, &diriter);
879 if (ret) {
880 fprintf(stdout, "[error] Readdir error.\n");
881 goto readdir_error;
882 }
883 if (!diriter)
884 break;
885 /* Ignore hidden files, ., .. and metadata. */
886 if (!strncmp(diriter->d_name, ".", 1)
887 || !strcmp(diriter->d_name, "..")
888 || !strcmp(diriter->d_name, "metadata"))
889 continue;
890 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
891 if (ret) {
892 fprintf(stdout, "[error] Open file stream error.\n");
893 goto readdir_error;
894 }
895 }
896
897 free(dirent);
898 return 0;
899
900 readdir_error:
901 free(dirent);
902 error_metadata:
903 close(td->dirfd);
904 error_dirfd:
905 closedir(td->dir);
906 error:
907 return ret;
908 }
909
910 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
911 {
912 struct ctf_trace *td;
913 int ret;
914
915 td = g_new0(struct ctf_trace, 1);
916
917 switch (flags & O_ACCMODE) {
918 case O_RDONLY:
919 if (!path) {
920 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
921 goto error;
922 }
923 ret = ctf_open_trace_read(td, path, flags);
924 if (ret)
925 goto error;
926 break;
927 case O_RDWR:
928 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
929 goto error;
930 default:
931 fprintf(stdout, "[error] Incorrect open flags.\n");
932 goto error;
933 }
934
935 return &td->parent;
936 error:
937 g_free(td);
938 return NULL;
939 }
940
941 static
942 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
943 {
944 ctf_fini_pos(&file_stream->pos);
945 close(file_stream->pos.fd);
946 }
947
948 void ctf_close_trace(struct trace_descriptor *tdp)
949 {
950 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
951 int i;
952
953 if (td->streams) {
954 for (i = 0; i < td->streams->len; i++) {
955 struct ctf_stream_class *stream;
956 int j;
957 stream = g_ptr_array_index(td->streams, i);
958 for (j = 0; j < stream->files->len; j++) {
959 struct ctf_file_stream *file_stream;
960 file_stream = g_ptr_array_index(stream->files, j);
961 ctf_close_file_stream(file_stream);
962 }
963
964 }
965 g_ptr_array_free(td->streams, TRUE);
966 }
967 closedir(td->dir);
968 g_free(td);
969 }
970
971 void __attribute__((constructor)) ctf_init(void)
972 {
973 int ret;
974
975 ctf_format.name = g_quark_from_static_string("ctf");
976 ret = bt_register_format(&ctf_format);
977 assert(!ret);
978 }
979
980 /* TODO: finalize */
This page took 0.055992 seconds and 4 git commands to generate.