1ba00a67232d59f5fb76bac2a07d8daea2e01984
[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 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, 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 = 0; /* continue reading next packet */
469 break;
470 }
471 }
472 return ret;
473 }
474
475 static
476 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
477 char **buf)
478 {
479 FILE *in, *out;
480 size_t size;
481 int ret;
482
483 in = *fp;
484 out = open_memstream(buf, &size);
485 if (out == NULL)
486 return -errno;
487
488 for (;;) {
489 ret = ctf_open_trace_metadata_packet_read(td, in, out);
490 if (ret) {
491 break;
492 }
493 if (feof(in)) {
494 ret = 0;
495 break;
496 }
497 }
498 fclose(out); /* flush the buffer */
499 fclose(in);
500 /* open for reading */
501 *fp = fmemopen(*buf, size, "rb");
502 return 0;
503 }
504
505 static
506 int ctf_open_trace_metadata_read(struct ctf_trace *td)
507 {
508 struct ctf_scanner *scanner;
509 FILE *fp;
510 char *buf = NULL;
511 int ret = 0;
512
513 td->metadata.pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
514 if (td->metadata.pos.fd < 0) {
515 fprintf(stdout, "Unable to open metadata.\n");
516 return td->metadata.pos.fd;
517 }
518
519 if (babeltrace_debug)
520 yydebug = 1;
521
522 fp = fdopen(td->metadata.pos.fd, "r");
523 if (!fp) {
524 fprintf(stdout, "[error] Unable to open metadata stream.\n");
525 ret = -errno;
526 goto end_stream;
527 }
528
529 if (packet_metadata(td, fp)) {
530 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
531 if (ret)
532 goto end_packet_read;
533 }
534
535 scanner = ctf_scanner_alloc(fp);
536 if (!scanner) {
537 fprintf(stdout, "[error] Error allocating scanner\n");
538 ret = -ENOMEM;
539 goto end_scanner_alloc;
540 }
541 ret = ctf_scanner_append_ast(scanner);
542 if (ret) {
543 fprintf(stdout, "[error] Error creating AST\n");
544 goto end;
545 }
546
547 if (babeltrace_debug) {
548 ret = ctf_visitor_print_xml(stdout, 0, &scanner->ast->root);
549 if (ret) {
550 fprintf(stdout, "[error] Error visiting AST for XML output\n");
551 goto end;
552 }
553 }
554
555 ret = ctf_visitor_semantic_check(stdout, 0, &scanner->ast->root);
556 if (ret) {
557 fprintf(stdout, "[error] Error in CTF semantic validation %d\n", ret);
558 goto end;
559 }
560 ret = ctf_visitor_construct_metadata(stdout, 0, &scanner->ast->root,
561 td, BYTE_ORDER);
562 if (ret) {
563 fprintf(stdout, "[error] Error in CTF metadata constructor %d\n", ret);
564 goto end;
565 }
566 end:
567 ctf_scanner_free(scanner);
568 end_scanner_alloc:
569 end_packet_read:
570 fclose(fp);
571 free(buf);
572 end_stream:
573 close(td->metadata.pos.fd);
574 return ret;
575 }
576
577
578 static
579 int create_stream_packet_index(struct ctf_trace *td,
580 struct ctf_file_stream *file_stream)
581 {
582 struct ctf_stream_class *stream;
583 int len_index;
584 struct ctf_stream_pos *pos;
585 struct stat filestats;
586 struct packet_index packet_index;
587 int first_packet = 1;
588 int ret;
589
590 pos = &file_stream->pos;
591
592 ret = fstat(pos->fd, &filestats);
593 if (ret < 0)
594 return ret;
595
596 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
597 uint64_t stream_id = 0;
598
599 if (pos->base) {
600 /* unmap old base */
601 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
602 if (ret) {
603 fprintf(stdout, "[error] Unable to unmap old base: %s.\n",
604 strerror(errno));
605 return ret;
606 }
607 pos->base = NULL;
608 }
609 /* map new base. Need mapping length from header. */
610 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
611 MAP_PRIVATE, pos->fd, pos->mmap_offset);
612 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
613 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
614 pos->offset = 0; /* Position of the packet header */
615
616 packet_index.offset = pos->mmap_offset;
617 packet_index.content_size = 0;
618 packet_index.packet_size = 0;
619
620 /* read and check header, set stream id (and check) */
621 if (td->packet_header) {
622 /* Read packet header */
623 ret = generic_rw(&pos->parent, &td->packet_header->p);
624 if (ret)
625 return ret;
626 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("magic"));
627 if (len_index >= 0) {
628 struct definition_integer *defint;
629 struct definition *field;
630
631 field = struct_definition_get_field_from_index(td->packet_header, len_index);
632 assert(field->declaration->id == CTF_TYPE_INTEGER);
633 defint = container_of(field, struct definition_integer, p);
634 assert(defint->declaration->signedness == FALSE);
635 if (defint->value._unsigned != CTF_MAGIC) {
636 fprintf(stdout, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
637 defint->value._unsigned,
638 file_stream->pos.packet_index->len,
639 (ssize_t) pos->mmap_offset);
640 return -EINVAL;
641 }
642 }
643
644 /* check uuid */
645 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("uuid"));
646 if (len_index >= 0) {
647 struct definition_array *defarray;
648 struct definition *field;
649 uint64_t i;
650 uint8_t uuidval[UUID_LEN];
651
652 field = struct_definition_get_field_from_index(td->packet_header, len_index);
653 assert(field->declaration->id == CTF_TYPE_ARRAY);
654 defarray = container_of(field, struct definition_array, p);
655 assert(array_len(defarray) == UUID_LEN);
656 assert(defarray->declaration->elem->id == CTF_TYPE_INTEGER);
657
658 for (i = 0; i < UUID_LEN; i++) {
659 struct definition *elem;
660 struct definition_integer *defint;
661
662 elem = array_index(defarray, i);
663 assert(elem);
664 defint = container_of(elem, struct definition_integer, p);
665 uuidval[i] = defint->value._unsigned;
666 }
667 ret = uuid_compare(td->uuid, uuidval);
668 if (ret) {
669 fprintf(stdout, "[error] Unique Universal Identifiers do not match.\n");
670 return -EINVAL;
671 }
672 }
673
674
675 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration, g_quark_from_static_string("stream_id"));
676 if (len_index >= 0) {
677 struct definition_integer *defint;
678 struct definition *field;
679
680 field = struct_definition_get_field_from_index(td->packet_header, len_index);
681 assert(field->declaration->id == CTF_TYPE_INTEGER);
682 defint = container_of(field, struct definition_integer, p);
683 assert(defint->declaration->signedness == FALSE);
684 stream_id = defint->value._unsigned;
685 }
686 }
687
688 if (!first_packet && file_stream->stream_id != stream_id) {
689 fprintf(stdout, "[error] Stream ID is changing within a stream.\n");
690 return -EINVAL;
691 }
692 if (first_packet) {
693 file_stream->stream_id = stream_id;
694 if (stream_id >= td->streams->len) {
695 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
696 return -EINVAL;
697 }
698 stream = g_ptr_array_index(td->streams, stream_id);
699 if (!stream) {
700 fprintf(stdout, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
701 return -EINVAL;
702 }
703 file_stream->stream.stream_class = stream;
704 }
705 first_packet = 0;
706
707 if (stream->packet_context) {
708 /* Read packet context */
709 ret = generic_rw(&pos->parent, &stream->packet_context->p);
710 if (ret)
711 return ret;
712 /* read content size from header */
713 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("content_size"));
714 if (len_index >= 0) {
715 struct definition_integer *defint;
716 struct definition *field;
717
718 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
719 assert(field->declaration->id == CTF_TYPE_INTEGER);
720 defint = container_of(field, struct definition_integer, p);
721 assert(defint->declaration->signedness == FALSE);
722 packet_index.content_size = defint->value._unsigned;
723 } else {
724 /* Use file size for packet size */
725 packet_index.content_size = filestats.st_size * CHAR_BIT;
726 }
727
728 /* read packet size from header */
729 len_index = struct_declaration_lookup_field_index(stream->packet_context->declaration, g_quark_from_static_string("packet_size"));
730 if (len_index >= 0) {
731 struct definition_integer *defint;
732 struct definition *field;
733
734 field = struct_definition_get_field_from_index(stream->packet_context, len_index);
735 assert(field->declaration->id == CTF_TYPE_INTEGER);
736 defint = container_of(field, struct definition_integer, p);
737 assert(defint->declaration->signedness == FALSE);
738 packet_index.packet_size = defint->value._unsigned;
739 } else {
740 /* Use content size if non-zero, else file size */
741 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
742 }
743 } else {
744 /* Use file size for packet size */
745 packet_index.content_size = filestats.st_size * CHAR_BIT;
746 /* Use content size if non-zero, else file size */
747 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
748 }
749
750 /* Validate content size and packet size values */
751 if (packet_index.content_size > packet_index.packet_size) {
752 fprintf(stdout, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
753 packet_index.content_size, packet_index.packet_size);
754 return -EINVAL;
755 }
756
757 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
758 fprintf(stdout, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
759 packet_index.content_size, (filestats.st_size - packet_index.offset) * CHAR_BIT);
760 return -EINVAL;
761 }
762
763 /* Save position after header and context */
764 packet_index.data_offset = pos->offset;
765
766 /* add index to packet array */
767 g_array_append_val(file_stream->pos.packet_index, packet_index);
768
769 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
770 }
771
772 /* Move pos back to beginning of file */
773 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
774
775 return 0;
776 }
777
778 /*
779 * Note: many file streams can inherit from the same stream class
780 * description (metadata).
781 */
782 static
783 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags)
784 {
785 int ret;
786 struct ctf_file_stream *file_stream;
787
788 ret = openat(td->dirfd, path, flags);
789 if (ret < 0)
790 goto error;
791 file_stream = g_new0(struct ctf_file_stream, 1);
792 ctf_init_pos(&file_stream->pos, ret, flags);
793 ret = create_stream_packet_index(td, file_stream);
794 if (ret)
795 goto error_index;
796 /* Add stream file to stream class */
797 g_ptr_array_add(file_stream->stream.stream_class->files, file_stream);
798 return 0;
799
800 error_index:
801 ctf_fini_pos(&file_stream->pos);
802 close(file_stream->pos.fd);
803 g_free(file_stream);
804 error:
805 return ret;
806 }
807
808 static
809 int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags)
810 {
811 int ret;
812 struct dirent *dirent;
813 struct dirent *diriter;
814 size_t dirent_len;
815
816 td->flags = flags;
817
818 /* Open trace directory */
819 td->dir = opendir(path);
820 if (!td->dir) {
821 fprintf(stdout, "[error] Unable to open trace directory.\n");
822 ret = -ENOENT;
823 goto error;
824 }
825
826 td->dirfd = open(path, 0);
827 if (td->dirfd < 0) {
828 fprintf(stdout, "[error] Unable to open trace directory file descriptor.\n");
829 ret = -ENOENT;
830 goto error_dirfd;
831 }
832
833 /*
834 * Keep the metadata file separate.
835 */
836
837 ret = ctf_open_trace_metadata_read(td);
838 if (ret) {
839 goto error_metadata;
840 }
841
842 /*
843 * Open each stream: for each file, try to open, check magic
844 * number, and get the stream ID to add to the right location in
845 * the stream array.
846 */
847
848 dirent_len = offsetof(struct dirent, d_name) +
849 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
850
851 dirent = malloc(dirent_len);
852
853 for (;;) {
854 ret = readdir_r(td->dir, dirent, &diriter);
855 if (ret) {
856 fprintf(stdout, "[error] Readdir error.\n");
857 goto readdir_error;
858 }
859 if (!diriter)
860 break;
861 /* Ignore hidden files, ., .. and metadata. */
862 if (!strncmp(diriter->d_name, ".", 1)
863 || !strcmp(diriter->d_name, "..")
864 || !strcmp(diriter->d_name, "metadata"))
865 continue;
866 ret = ctf_open_file_stream_read(td, diriter->d_name, flags);
867 if (ret) {
868 fprintf(stdout, "[error] Open file stream error.\n");
869 goto readdir_error;
870 }
871 }
872
873 free(dirent);
874 return 0;
875
876 readdir_error:
877 free(dirent);
878 error_metadata:
879 close(td->dirfd);
880 error_dirfd:
881 closedir(td->dir);
882 error:
883 return ret;
884 }
885
886 struct trace_descriptor *ctf_open_trace(const char *path, int flags)
887 {
888 struct ctf_trace *td;
889 int ret;
890
891 td = g_new0(struct ctf_trace, 1);
892
893 switch (flags & O_ACCMODE) {
894 case O_RDONLY:
895 if (!path) {
896 fprintf(stdout, "[error] Path missing for input CTF trace.\n");
897 goto error;
898 }
899 ret = ctf_open_trace_read(td, path, flags);
900 if (ret)
901 goto error;
902 break;
903 case O_RDWR:
904 fprintf(stdout, "[error] Opening CTF traces for output is not supported yet.\n");
905 goto error;
906 default:
907 fprintf(stdout, "[error] Incorrect open flags.\n");
908 goto error;
909 }
910
911 return &td->parent;
912 error:
913 g_free(td);
914 return NULL;
915 }
916
917 static
918 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
919 {
920 ctf_fini_pos(&file_stream->pos);
921 close(file_stream->pos.fd);
922 }
923
924 void ctf_close_trace(struct trace_descriptor *tdp)
925 {
926 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
927 int i;
928
929 if (td->streams) {
930 for (i = 0; i < td->streams->len; i++) {
931 struct ctf_stream_class *stream;
932 int j;
933 stream = g_ptr_array_index(td->streams, i);
934 for (j = 0; j < stream->files->len; j++) {
935 struct ctf_file_stream *file_stream;
936 file_stream = g_ptr_array_index(stream->files, j);
937 ctf_close_file_stream(file_stream);
938 }
939
940 }
941 g_ptr_array_free(td->streams, TRUE);
942 }
943 closedir(td->dir);
944 g_free(td);
945 }
946
947 void __attribute__((constructor)) ctf_init(void)
948 {
949 int ret;
950
951 ctf_format.name = g_quark_from_static_string("ctf");
952 ret = bt_register_format(&ctf_format);
953 assert(!ret);
954 }
955
956 /* TODO: finalize */
This page took 0.046685 seconds and 3 git commands to generate.