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