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