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