Discarded event time range is between last packet event and timestamp_end
[babeltrace.git] / formats / ctf / ctf.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <inttypes.h>
26 #include <stdio.h>
27 #include <uuid/uuid.h>
28 #include <sys/mman.h>
29 #include <errno.h>
30 #include <endian.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <dirent.h>
35 #include <glib.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38
39 #include "metadata/ctf-scanner.h"
40 #include "metadata/ctf-parser.h"
41 #include "metadata/ctf-ast.h"
42
43 /*
44 * We currently simply map a page to read the packet header and packet
45 * context to get the packet length and content length. (in bits)
46 */
47 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
48 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
49 #define UUID_LEN 16 /* uuid by value len */
50
51 #ifndef min
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
53 #endif
54
55 extern int yydebug;
56
57 static
58 struct trace_descriptor *ctf_open_trace(const char *collection_path, const char *path, int flags,
59 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
60 int whence), FILE *metadata_fp);
61 static
62 struct trace_descriptor *ctf_open_mmap_trace(
63 struct mmap_stream_list *mmap_list,
64 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
65 FILE *metadata_fp);
66
67 static
68 void ctf_close_trace(struct trace_descriptor *descriptor);
69
70 static
71 rw_dispatch read_dispatch_table[] = {
72 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
73 [ CTF_TYPE_FLOAT ] = ctf_float_read,
74 [ CTF_TYPE_ENUM ] = ctf_enum_read,
75 [ CTF_TYPE_STRING ] = ctf_string_read,
76 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
77 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
78 [ CTF_TYPE_ARRAY ] = ctf_array_read,
79 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
80 };
81
82 static
83 rw_dispatch write_dispatch_table[] = {
84 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
85 [ CTF_TYPE_FLOAT ] = ctf_float_write,
86 [ CTF_TYPE_ENUM ] = ctf_enum_write,
87 [ CTF_TYPE_STRING ] = ctf_string_write,
88 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
89 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
90 [ CTF_TYPE_ARRAY ] = ctf_array_write,
91 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
92 };
93
94 static
95 struct format ctf_format = {
96 .open_trace = ctf_open_trace,
97 .open_mmap_trace = ctf_open_mmap_trace,
98 .close_trace = ctf_close_trace,
99 };
100
101 static
102 void ctf_update_timestamp(struct ctf_stream *stream,
103 struct definition_integer *integer_definition)
104 {
105 struct declaration_integer *integer_declaration =
106 integer_definition->declaration;
107 uint64_t oldval, newval, updateval;
108
109 if (unlikely(integer_declaration->len == 64)) {
110 stream->timestamp = integer_definition->value._unsigned;
111 return;
112 }
113 /* keep low bits */
114 oldval = stream->timestamp;
115 oldval &= (1ULL << integer_declaration->len) - 1;
116 newval = integer_definition->value._unsigned;
117 /* Test for overflow by comparing low bits */
118 if (newval < oldval)
119 newval += 1ULL << integer_declaration->len;
120 /* updateval contains old high bits, and new low bits (sum) */
121 updateval = stream->timestamp;
122 updateval &= ~((1ULL << integer_declaration->len) - 1);
123 updateval += newval;
124 stream->prev_timestamp = stream->timestamp;
125 stream->timestamp = updateval;
126 }
127
128 static
129 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream *stream)
130 {
131 struct ctf_stream_pos *pos =
132 container_of(ppos, struct ctf_stream_pos, parent);
133 struct ctf_stream_class *stream_class = stream->stream_class;
134 struct ctf_stream_event *event;
135 uint64_t id = 0;
136 int ret;
137
138 /* We need to check for EOF here for empty files. */
139 if (unlikely(pos->offset == EOF))
140 return EOF;
141
142 ctf_pos_get_event(pos);
143
144 /*
145 * This is the EOF check after we've advanced the position in
146 * ctf_pos_get_event.
147 */
148 if (unlikely(pos->offset == EOF))
149 return EOF;
150 assert(pos->offset < pos->content_size);
151
152 /* Read event header */
153 if (likely(stream->stream_event_header)) {
154 struct definition_integer *integer_definition;
155 struct definition *variant;
156
157 ret = generic_rw(ppos, &stream->stream_event_header->p);
158 if (unlikely(ret))
159 goto error;
160 /* lookup event id */
161 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
162 if (integer_definition) {
163 id = integer_definition->value._unsigned;
164 } else {
165 struct definition_enum *enum_definition;
166
167 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
168 if (enum_definition) {
169 id = enum_definition->integer->value._unsigned;
170 }
171 }
172
173 variant = lookup_variant(&stream->stream_event_header->p, "v");
174 if (variant) {
175 integer_definition = lookup_integer(variant, "id", FALSE);
176 if (integer_definition) {
177 id = integer_definition->value._unsigned;
178 }
179 }
180 stream->event_id = id;
181
182 /* lookup timestamp */
183 stream->has_timestamp = 0;
184 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
185 if (integer_definition) {
186 ctf_update_timestamp(stream, integer_definition);
187 stream->has_timestamp = 1;
188 } else {
189 if (variant) {
190 integer_definition = lookup_integer(variant, "timestamp", FALSE);
191 if (integer_definition) {
192 ctf_update_timestamp(stream, integer_definition);
193 stream->has_timestamp = 1;
194 }
195 }
196 }
197 }
198
199 /* Read stream-declared event context */
200 if (stream->stream_event_context) {
201 ret = generic_rw(ppos, &stream->stream_event_context->p);
202 if (ret)
203 goto error;
204 }
205
206 if (unlikely(id >= stream_class->events_by_id->len)) {
207 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
208 return -EINVAL;
209 }
210 event = g_ptr_array_index(stream->events_by_id, id);
211 if (unlikely(!event)) {
212 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
213 return -EINVAL;
214 }
215
216 /* Read event-declared event context */
217 if (event->event_context) {
218 ret = generic_rw(ppos, &event->event_context->p);
219 if (ret)
220 goto error;
221 }
222
223 /* Read event payload */
224 if (likely(event->event_fields)) {
225 ret = generic_rw(ppos, &event->event_fields->p);
226 if (ret)
227 goto error;
228 }
229
230 return 0;
231
232 error:
233 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
234 return ret;
235 }
236
237 static
238 int ctf_write_event(struct stream_pos *pos, struct ctf_stream *stream)
239 {
240 struct ctf_stream_class *stream_class = stream->stream_class;
241 struct ctf_stream_event *event;
242 uint64_t id;
243 int ret;
244
245 id = stream->event_id;
246
247 /* print event header */
248 if (likely(stream->stream_event_header)) {
249 ret = generic_rw(pos, &stream->stream_event_header->p);
250 if (ret)
251 goto error;
252 }
253
254 /* print stream-declared event context */
255 if (stream->stream_event_context) {
256 ret = generic_rw(pos, &stream->stream_event_context->p);
257 if (ret)
258 goto error;
259 }
260
261 if (unlikely(id >= stream_class->events_by_id->len)) {
262 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
263 return -EINVAL;
264 }
265 event = g_ptr_array_index(stream->events_by_id, id);
266 if (unlikely(!event)) {
267 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
268 return -EINVAL;
269 }
270
271 /* print event-declared event context */
272 if (event->event_context) {
273 ret = generic_rw(pos, &event->event_context->p);
274 if (ret)
275 goto error;
276 }
277
278 /* Read and print event payload */
279 if (likely(event->event_fields)) {
280 ret = generic_rw(pos, &event->event_fields->p);
281 if (ret)
282 goto error;
283 }
284
285 return 0;
286
287 error:
288 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
289 return ret;
290 }
291
292 void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
293 {
294 pos->fd = fd;
295 pos->mmap_offset = 0;
296 pos->packet_size = 0;
297 pos->content_size = 0;
298 pos->content_size_loc = NULL;
299 pos->base = NULL;
300 pos->offset = 0;
301 pos->dummy = false;
302 pos->cur_index = 0;
303 if (fd >= 0)
304 pos->packet_index = g_array_new(FALSE, TRUE,
305 sizeof(struct packet_index));
306 else
307 pos->packet_index = NULL;
308 switch (open_flags & O_ACCMODE) {
309 case O_RDONLY:
310 pos->prot = PROT_READ;
311 pos->flags = MAP_PRIVATE;
312 pos->parent.rw_table = read_dispatch_table;
313 pos->parent.event_cb = ctf_read_event;
314 break;
315 case O_RDWR:
316 pos->prot = PROT_WRITE; /* Write has priority */
317 pos->flags = MAP_SHARED;
318 pos->parent.rw_table = write_dispatch_table;
319 pos->parent.event_cb = ctf_write_event;
320 if (fd >= 0)
321 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
322 break;
323 default:
324 assert(0);
325 }
326 }
327
328 void ctf_fini_pos(struct ctf_stream_pos *pos)
329 {
330 int ret;
331
332 if (pos->prot == PROT_WRITE && pos->content_size_loc)
333 *pos->content_size_loc = pos->offset;
334 if (pos->base) {
335 /* unmap old base */
336 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
337 if (ret) {
338 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
339 strerror(errno));
340 assert(0);
341 }
342 }
343 (void) g_array_free(pos->packet_index, TRUE);
344 }
345
346 void ctf_move_pos_slow(struct ctf_stream_pos *pos, size_t offset, int whence)
347 {
348 struct ctf_file_stream *file_stream =
349 container_of(pos, struct ctf_file_stream, pos);
350 int ret;
351 off_t off;
352 struct packet_index *index;
353
354 if (pos->prot == PROT_WRITE && pos->content_size_loc)
355 *pos->content_size_loc = pos->offset;
356
357 if (pos->base) {
358 /* unmap old base */
359 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
360 if (ret) {
361 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
362 strerror(errno));
363 assert(0);
364 }
365 pos->base = NULL;
366 }
367
368 /*
369 * The caller should never ask for ctf_move_pos across packets,
370 * except to get exactly at the beginning of the next packet.
371 */
372 if (pos->prot == PROT_WRITE) {
373 switch (whence) {
374 case SEEK_CUR:
375 /* The writer will add padding */
376 assert(pos->offset + offset == pos->packet_size);
377 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
378 break;
379 case SEEK_SET:
380 assert(offset == 0); /* only seek supported for now */
381 pos->cur_index = 0;
382 break;
383 default:
384 assert(0);
385 }
386 pos->content_size = -1U; /* Unknown at this point */
387 pos->packet_size = WRITE_PACKET_LEN;
388 off = posix_fallocate(pos->fd, pos->mmap_offset,
389 pos->packet_size / CHAR_BIT);
390 assert(off >= 0);
391 pos->offset = 0;
392 } else {
393 read_next_packet:
394 switch (whence) {
395 case SEEK_CUR:
396 {
397 uint32_t events_discarded_diff;
398
399 /* For printing discarded event count */
400 index = &g_array_index(pos->packet_index,
401 struct packet_index, pos->cur_index);
402 events_discarded_diff = index->events_discarded;
403 file_stream->parent.prev_timestamp_end =
404 index->timestamp_end;
405 if (pos->cur_index > 0) {
406 index = &g_array_index(pos->packet_index,
407 struct packet_index,
408 pos->cur_index - 1);
409 events_discarded_diff -= index->events_discarded;
410 }
411 file_stream->parent.events_discarded = events_discarded_diff;
412 if (pos->offset == EOF)
413 return;
414 /* The reader will expect us to skip padding */
415 assert(pos->offset + offset == pos->content_size);
416 ++pos->cur_index;
417 file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
418 break;
419 }
420 case SEEK_SET:
421 assert(offset == 0); /* only seek supported for now */
422 pos->cur_index = 0;
423 file_stream->parent.prev_timestamp = 0;
424 file_stream->parent.prev_timestamp_end = 0;
425 break;
426 default:
427 assert(0);
428 }
429 if (pos->cur_index >= pos->packet_index->len) {
430 pos->offset = EOF;
431 return;
432 }
433 index = &g_array_index(pos->packet_index, struct packet_index,
434 pos->cur_index);
435 pos->mmap_offset = index->offset;
436
437 /* Lookup context/packet size in index */
438 file_stream->parent.timestamp = index->timestamp_begin;
439 pos->content_size = index->content_size;
440 pos->packet_size = index->packet_size;
441 if (index->data_offset < index->content_size) {
442 pos->offset = 0; /* will read headers */
443 } else if (index->data_offset == index->content_size) {
444 /* empty packet */
445 pos->offset = index->data_offset;
446 offset = 0;
447 whence = SEEK_CUR;
448 goto read_next_packet;
449 } else {
450 pos->offset = EOF;
451 return;
452 }
453 }
454 /* map new base. Need mapping length from header. */
455 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
456 pos->flags, pos->fd, pos->mmap_offset);
457 if (pos->base == MAP_FAILED) {
458 fprintf(stderr, "[error] mmap error %s.\n",
459 strerror(errno));
460 assert(0);
461 }
462
463 /* update trace_packet_header and stream_packet_context */
464 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
465 /* Read packet header */
466 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
467 assert(!ret);
468 }
469 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
470 /* Read packet context */
471 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
472 assert(!ret);
473 }
474 }
475
476 static
477 int packet_metadata(struct ctf_trace *td, FILE *fp)
478 {
479 uint32_t magic;
480 size_t len;
481 int ret = 0;
482
483 len = fread(&magic, sizeof(magic), 1, fp);
484 if (len != 1) {
485 goto end;
486 }
487 if (magic == TSDL_MAGIC) {
488 ret = 1;
489 td->byte_order = BYTE_ORDER;
490 CTF_TRACE_SET_FIELD(td, byte_order);
491 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
492 ret = 1;
493 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
494 LITTLE_ENDIAN : BIG_ENDIAN;
495 CTF_TRACE_SET_FIELD(td, byte_order);
496 }
497 end:
498 rewind(fp);
499 return ret;
500 }
501
502 /*
503 * Returns 0 on success, -1 on error.
504 */
505 static
506 int check_version(unsigned int major, unsigned int minor)
507 {
508 switch (major) {
509 case 1:
510 switch (minor) {
511 case 8:
512 return 0;
513 default:
514 goto warning;
515 }
516 default:
517 goto warning;
518
519 }
520
521 /* eventually return an error instead of warning */
522 warning:
523 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
524 major, minor);
525 return 0;
526 }
527
528 static
529 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
530 FILE *out)
531 {
532 struct metadata_packet_header header;
533 size_t readlen, writelen, toread;
534 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
535 int ret = 0;
536
537 readlen = fread(&header, header_sizeof(header), 1, in);
538 if (readlen < 1)
539 return -EINVAL;
540
541 if (td->byte_order != BYTE_ORDER) {
542 header.magic = GUINT32_SWAP_LE_BE(header.magic);
543 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
544 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
545 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
546 }
547 if (header.checksum)
548 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
549 if (header.compression_scheme) {
550 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
551 header.compression_scheme);
552 return -EINVAL;
553 }
554 if (header.encryption_scheme) {
555 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
556 header.encryption_scheme);
557 return -EINVAL;
558 }
559 if (header.checksum_scheme) {
560 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
561 header.checksum_scheme);
562 return -EINVAL;
563 }
564 if (check_version(header.major, header.minor) < 0)
565 return -EINVAL;
566 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
567 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
568 CTF_TRACE_SET_FIELD(td, uuid);
569 } else {
570 if (uuid_compare(header.uuid, td->uuid))
571 return -EINVAL;
572 }
573
574 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
575
576 for (;;) {
577 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
578 if (ferror(in)) {
579 ret = -EINVAL;
580 break;
581 }
582 if (babeltrace_debug) {
583 buf[readlen] = '\0';
584 fprintf(stderr, "[debug] metadata packet read: %s\n",
585 buf);
586 }
587
588 writelen = fwrite(buf, sizeof(char), readlen, out);
589 if (writelen < readlen) {
590 ret = -EIO;
591 break;
592 }
593 if (ferror(out)) {
594 ret = -EINVAL;
595 break;
596 }
597 toread -= readlen;
598 if (!toread) {
599 ret = 0; /* continue reading next packet */
600 goto read_padding;
601 }
602 }
603 return ret;
604
605 read_padding:
606 toread = (header.packet_size - header.content_size) / CHAR_BIT;
607 ret = fseek(in, toread, SEEK_CUR);
608 if (ret < 0) {
609 fprintf(stderr, "[warning] Missing padding at end of file\n");
610 ret = 0;
611 }
612 return ret;
613 }
614
615 static
616 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
617 char **buf)
618 {
619 FILE *in, *out;
620 size_t size;
621 int ret;
622
623 in = *fp;
624 /*
625 * Using strlen on *buf instead of size of open_memstream
626 * because its size includes garbage at the end (after final
627 * \0). This is the allocated size, not the actual string size.
628 */
629 out = open_memstream(buf, &size);
630 if (out == NULL) {
631 perror("Metadata open_memstream");
632 return -errno;
633 }
634 for (;;) {
635 ret = ctf_open_trace_metadata_packet_read(td, in, out);
636 if (ret) {
637 break;
638 }
639 if (feof(in)) {
640 ret = 0;
641 break;
642 }
643 }
644 fclose(out); /* flush the buffer */
645 fclose(in);
646 /* open for reading */
647 *fp = fmemopen(*buf, strlen(*buf), "rb");
648 if (!*fp) {
649 perror("Metadata fmemopen");
650 return -errno;
651 }
652 return 0;
653 }
654
655 static
656 int ctf_open_trace_metadata_read(struct ctf_trace *td,
657 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
658 int whence), FILE *metadata_fp)
659 {
660 struct ctf_scanner *scanner;
661 struct ctf_file_stream *metadata_stream;
662 FILE *fp;
663 char *buf = NULL;
664 int ret = 0;
665
666 metadata_stream = g_new0(struct ctf_file_stream, 1);
667
668 if (move_pos_slow) {
669 metadata_stream->pos.move_pos_slow = move_pos_slow;
670 } else {
671 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
672 ret = -1;
673 goto end_stream;
674 }
675
676 if (metadata_fp) {
677 fp = metadata_fp;
678 } else {
679 td->metadata = &metadata_stream->parent;
680 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
681 if (metadata_stream->pos.fd < 0) {
682 fprintf(stderr, "Unable to open metadata.\n");
683 return metadata_stream->pos.fd;
684 }
685
686 fp = fdopen(metadata_stream->pos.fd, "r");
687 if (!fp) {
688 fprintf(stderr, "[error] Unable to open metadata stream.\n");
689 perror("Metadata stream open");
690 ret = -errno;
691 goto end_stream;
692 }
693 }
694 if (babeltrace_debug)
695 yydebug = 1;
696
697 if (packet_metadata(td, fp)) {
698 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
699 if (ret)
700 goto end_packet_read;
701 } else {
702 unsigned int major, minor;
703 ssize_t nr_items;
704
705 td->byte_order = BYTE_ORDER;
706
707 /* Check text-only metadata header and version */
708 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
709 if (nr_items < 2)
710 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
711 if (check_version(major, minor) < 0) {
712 ret = -EINVAL;
713 goto end_packet_read;
714 }
715 rewind(fp);
716 }
717
718 scanner = ctf_scanner_alloc(fp);
719 if (!scanner) {
720 fprintf(stderr, "[error] Error allocating scanner\n");
721 ret = -ENOMEM;
722 goto end_scanner_alloc;
723 }
724 ret = ctf_scanner_append_ast(scanner);
725 if (ret) {
726 fprintf(stderr, "[error] Error creating AST\n");
727 goto end;
728 }
729
730 if (babeltrace_debug) {
731 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
732 if (ret) {
733 fprintf(stderr, "[error] Error visiting AST for XML output\n");
734 goto end;
735 }
736 }
737
738 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
739 if (ret) {
740 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
741 goto end;
742 }
743 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
744 td, td->byte_order);
745 if (ret) {
746 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
747 goto end;
748 }
749 end:
750 ctf_scanner_free(scanner);
751 end_scanner_alloc:
752 end_packet_read:
753 fclose(fp);
754 free(buf);
755 end_stream:
756 close(metadata_stream->pos.fd);
757 if (ret)
758 g_free(metadata_stream);
759 return ret;
760 }
761
762 static
763 struct ctf_stream_event *create_event_definitions(struct ctf_trace *td,
764 struct ctf_stream *stream,
765 struct ctf_event *event)
766 {
767 struct ctf_stream_event *stream_event = g_new0(struct ctf_stream_event, 1);
768
769 if (event->context_decl) {
770 struct definition *definition =
771 event->context_decl->p.definition_new(&event->context_decl->p,
772 stream->parent_def_scope, 0, 0, "event.context");
773 if (!definition) {
774 goto error;
775 }
776 stream_event->event_context = container_of(definition,
777 struct definition_struct, p);
778 stream->parent_def_scope = stream_event->event_context->p.scope;
779 }
780 if (event->fields_decl) {
781 struct definition *definition =
782 event->fields_decl->p.definition_new(&event->fields_decl->p,
783 stream->parent_def_scope, 0, 0, "event.fields");
784 if (!definition) {
785 goto error;
786 }
787 stream_event->event_fields = container_of(definition,
788 struct definition_struct, p);
789 stream->parent_def_scope = stream_event->event_fields->p.scope;
790 }
791 return stream_event;
792
793 error:
794 if (stream_event->event_fields)
795 definition_unref(&stream_event->event_fields->p);
796 if (stream_event->event_context)
797 definition_unref(&stream_event->event_context->p);
798 return NULL;
799 }
800
801 static
802 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
803 {
804 struct ctf_stream_class *stream_class;
805 int ret;
806 int i;
807
808 if (stream->stream_definitions_created)
809 return 0;
810
811 stream_class = stream->stream_class;
812
813 if (stream_class->packet_context_decl) {
814 struct definition *definition =
815 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
816 stream->parent_def_scope, 0, 0, "stream.packet.context");
817 if (!definition) {
818 ret = -EINVAL;
819 goto error;
820 }
821 stream->stream_packet_context = container_of(definition,
822 struct definition_struct, p);
823 stream->parent_def_scope = stream->stream_packet_context->p.scope;
824 }
825 if (stream_class->event_header_decl) {
826 struct definition *definition =
827 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
828 stream->parent_def_scope, 0, 0, "stream.event.header");
829 if (!definition) {
830 ret = -EINVAL;
831 goto error;
832 }
833 stream->stream_event_header =
834 container_of(definition, struct definition_struct, p);
835 stream->parent_def_scope = stream->stream_event_header->p.scope;
836 }
837 if (stream_class->event_context_decl) {
838 struct definition *definition =
839 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
840 stream->parent_def_scope, 0, 0, "stream.event.context");
841 if (!definition) {
842 ret = -EINVAL;
843 goto error;
844 }
845 stream->stream_event_context =
846 container_of(definition, struct definition_struct, p);
847 stream->parent_def_scope = stream->stream_event_context->p.scope;
848 }
849 stream->events_by_id = g_ptr_array_new();
850 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
851 for (i = 0; i < stream->events_by_id->len; i++) {
852 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
853 struct ctf_stream_event *stream_event;
854
855 if (!event)
856 continue;
857 stream_event = create_event_definitions(td, stream, event);
858 if (!stream_event)
859 goto error_event;
860 g_ptr_array_index(stream->events_by_id, i) = stream_event;
861 }
862 return 0;
863
864 error_event:
865 for (i = 0; i < stream->events_by_id->len; i++) {
866 struct ctf_stream_event *stream_event = g_ptr_array_index(stream->events_by_id, i);
867 if (stream_event)
868 g_free(stream_event);
869 }
870 g_ptr_array_free(stream->events_by_id, TRUE);
871 error:
872 if (stream->stream_event_context)
873 definition_unref(&stream->stream_event_context->p);
874 if (stream->stream_event_header)
875 definition_unref(&stream->stream_event_header->p);
876 if (stream->stream_packet_context)
877 definition_unref(&stream->stream_packet_context->p);
878 return ret;
879 }
880
881
882 static
883 int create_stream_packet_index(struct ctf_trace *td,
884 struct ctf_file_stream *file_stream)
885 {
886 struct ctf_stream_class *stream;
887 int len_index;
888 struct ctf_stream_pos *pos;
889 struct stat filestats;
890 struct packet_index packet_index;
891 int first_packet = 1;
892 int ret;
893
894 pos = &file_stream->pos;
895
896 ret = fstat(pos->fd, &filestats);
897 if (ret < 0)
898 return ret;
899
900 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
901 return -EINVAL;
902
903 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
904 uint64_t stream_id = 0;
905
906 if (pos->base) {
907 /* unmap old base */
908 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
909 if (ret) {
910 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
911 strerror(errno));
912 return ret;
913 }
914 pos->base = NULL;
915 }
916 /* map new base. Need mapping length from header. */
917 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
918 MAP_PRIVATE, pos->fd, pos->mmap_offset);
919 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
920 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
921 pos->offset = 0; /* Position of the packet header */
922
923 packet_index.offset = pos->mmap_offset;
924 packet_index.content_size = 0;
925 packet_index.packet_size = 0;
926 packet_index.timestamp_begin = 0;
927 packet_index.timestamp_end = 0;
928 packet_index.events_discarded = 0;
929
930 /* read and check header, set stream id (and check) */
931 if (file_stream->parent.trace_packet_header) {
932 /* Read packet header */
933 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
934 if (ret)
935 return ret;
936 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
937 if (len_index >= 0) {
938 struct definition *field;
939 uint64_t magic;
940
941 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
942 magic = get_unsigned_int(field);
943 if (magic != CTF_MAGIC) {
944 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
945 magic,
946 file_stream->pos.packet_index->len,
947 (ssize_t) pos->mmap_offset);
948 return -EINVAL;
949 }
950 }
951
952 /* check uuid */
953 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
954 if (len_index >= 0) {
955 struct definition_array *defarray;
956 struct definition *field;
957 uint64_t i;
958 uint8_t uuidval[UUID_LEN];
959
960 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
961 assert(field->declaration->id == CTF_TYPE_ARRAY);
962 defarray = container_of(field, struct definition_array, p);
963 assert(array_len(defarray) == UUID_LEN);
964
965 for (i = 0; i < UUID_LEN; i++) {
966 struct definition *elem;
967
968 elem = array_index(defarray, i);
969 uuidval[i] = get_unsigned_int(elem);
970 }
971 ret = uuid_compare(td->uuid, uuidval);
972 if (ret) {
973 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
974 return -EINVAL;
975 }
976 }
977
978
979 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
980 if (len_index >= 0) {
981 struct definition *field;
982
983 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
984 stream_id = get_unsigned_int(field);
985 }
986 }
987
988 if (!first_packet && file_stream->parent.stream_id != stream_id) {
989 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
990 return -EINVAL;
991 }
992 if (first_packet) {
993 file_stream->parent.stream_id = stream_id;
994 if (stream_id >= td->streams->len) {
995 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
996 return -EINVAL;
997 }
998 stream = g_ptr_array_index(td->streams, stream_id);
999 if (!stream) {
1000 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1001 return -EINVAL;
1002 }
1003 file_stream->parent.stream_class = stream;
1004 ret = create_stream_definitions(td, &file_stream->parent);
1005 if (ret)
1006 return ret;
1007 }
1008 first_packet = 0;
1009
1010 if (file_stream->parent.stream_packet_context) {
1011 /* Read packet context */
1012 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1013 if (ret)
1014 return ret;
1015 /* read content size from header */
1016 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1017 if (len_index >= 0) {
1018 struct definition *field;
1019
1020 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1021 packet_index.content_size = get_unsigned_int(field);
1022 } else {
1023 /* Use file size for packet size */
1024 packet_index.content_size = filestats.st_size * CHAR_BIT;
1025 }
1026
1027 /* read packet size from header */
1028 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1029 if (len_index >= 0) {
1030 struct definition *field;
1031
1032 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1033 packet_index.packet_size = get_unsigned_int(field);
1034 } else {
1035 /* Use content size if non-zero, else file size */
1036 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1037 }
1038
1039 /* read timestamp begin from header */
1040 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1041 if (len_index >= 0) {
1042 struct definition *field;
1043
1044 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1045 packet_index.timestamp_begin = get_unsigned_int(field);
1046 }
1047
1048 /* read timestamp end from header */
1049 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1050 if (len_index >= 0) {
1051 struct definition *field;
1052
1053 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1054 packet_index.timestamp_end = get_unsigned_int(field);
1055 }
1056
1057 /* read events discarded from header */
1058 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1059 if (len_index >= 0) {
1060 struct definition *field;
1061
1062 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1063 packet_index.events_discarded = get_unsigned_int(field);
1064 }
1065 } else {
1066 /* Use file size for packet size */
1067 packet_index.content_size = filestats.st_size * CHAR_BIT;
1068 /* Use content size if non-zero, else file size */
1069 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1070 }
1071
1072 /* Validate content size and packet size values */
1073 if (packet_index.content_size > packet_index.packet_size) {
1074 fprintf(stderr, "[error] Content size (%zu bits) is larger than packet size (%zu bits).\n",
1075 packet_index.content_size, packet_index.packet_size);
1076 return -EINVAL;
1077 }
1078
1079 if (packet_index.packet_size > (filestats.st_size - packet_index.offset) * CHAR_BIT) {
1080 fprintf(stderr, "[error] Packet size (%zu bits) is larger than remaining file size (%zu bits).\n",
1081 packet_index.content_size, (size_t) (filestats.st_size - packet_index.offset) * CHAR_BIT);
1082 return -EINVAL;
1083 }
1084
1085 /* Save position after header and context */
1086 packet_index.data_offset = pos->offset;
1087
1088 /* add index to packet array */
1089 g_array_append_val(file_stream->pos.packet_index, packet_index);
1090
1091 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1092 }
1093
1094 /* Move pos back to beginning of file */
1095 ctf_move_pos_slow(pos, 0, SEEK_SET); /* position for write */
1096
1097 return 0;
1098 }
1099
1100 static
1101 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1102 {
1103 int ret;
1104
1105 if (td->packet_header_decl) {
1106 struct definition *definition =
1107 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1108 stream->parent_def_scope, 0, 0, "trace.packet.header");
1109 if (!definition) {
1110 ret = -EINVAL;
1111 goto error;
1112 }
1113 stream->trace_packet_header =
1114 container_of(definition, struct definition_struct, p);
1115 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1116 }
1117
1118 return 0;
1119
1120 error:
1121 return ret;
1122 }
1123
1124 /*
1125 * Note: many file streams can inherit from the same stream class
1126 * description (metadata).
1127 */
1128 static
1129 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1130 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1131 int whence))
1132 {
1133 int ret;
1134 struct ctf_file_stream *file_stream;
1135
1136 ret = openat(td->dirfd, path, flags);
1137 if (ret < 0) {
1138 perror("File stream openat()");
1139 goto error;
1140 }
1141 file_stream = g_new0(struct ctf_file_stream, 1);
1142
1143 if (move_pos_slow) {
1144 file_stream->pos.move_pos_slow = move_pos_slow;
1145 } else {
1146 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1147 ret = -1;
1148 goto error_def;
1149 }
1150
1151 ctf_init_pos(&file_stream->pos, ret, flags);
1152 ret = create_trace_definitions(td, &file_stream->parent);
1153 if (ret)
1154 goto error_def;
1155 ret = create_stream_packet_index(td, file_stream);
1156 if (ret)
1157 goto error_index;
1158 /* Add stream file to stream class */
1159 g_ptr_array_add(file_stream->parent.stream_class->streams,
1160 &file_stream->parent);
1161 return 0;
1162
1163 error_index:
1164 if (file_stream->parent.trace_packet_header)
1165 definition_unref(&file_stream->parent.trace_packet_header->p);
1166 error_def:
1167 ctf_fini_pos(&file_stream->pos);
1168 close(file_stream->pos.fd);
1169 g_free(file_stream);
1170 error:
1171 return ret;
1172 }
1173
1174 static void
1175 init_domain_name(struct ctf_trace *td)
1176 {
1177 char *start, *end;
1178
1179 start = td->path + strlen(td->collection_path);
1180 while (start[0] == '/')
1181 start++; /* skip / */
1182 end = strchr(start, '/');
1183 if (!end)
1184 end = start + strlen(start);
1185 memcpy(td->domain, start, end - start);
1186 td->domain[end - start] = '\0';
1187 }
1188
1189 static void
1190 init_proc_name(struct ctf_trace *td)
1191 {
1192 char buf[PATH_MAX];
1193 char *start, *end;
1194
1195 if (td->domain[0] == '\0')
1196 return;
1197 memcpy(buf, td->path, PATH_MAX);
1198 start = buf + strlen(td->collection_path);
1199 while (start[0] == '/')
1200 start++; /* skip / */
1201 start = strchr(start, '/'); /* get begin of domain content */
1202 if (!start)
1203 return;
1204 while (start[0] == '/')
1205 start++; /* skip / */
1206 /* find last -, skips time */
1207 end = strrchr(start, '-');
1208 if (!end)
1209 return;
1210 *end = '\0';
1211 /* find previous -, skips date */
1212 end = strrchr(start, '-');
1213 if (!end)
1214 return;
1215 *end = '\0';
1216 /* find previous -, skips pid */
1217 end = strrchr(start, '-');
1218 if (!end)
1219 return;
1220 *end = '\0';
1221
1222 memcpy(td->procname, start, end - start);
1223 td->procname[end - start] = '\0';
1224 }
1225
1226 static void
1227 init_vpid(struct ctf_trace *td)
1228 {
1229 char buf[PATH_MAX];
1230 char *start, *end;
1231
1232 if (td->domain[0] == '\0')
1233 return;
1234 memcpy(buf, td->path, PATH_MAX);
1235 start = buf + strlen(td->collection_path);
1236 while (start[0] == '/')
1237 start++; /* skip / */
1238 start = strchr(start, '/'); /* get begin of domain content */
1239 if (!start)
1240 return;
1241 while (start[0] == '/')
1242 start++; /* skip / */
1243 /* find last -, skips time */
1244 end = strrchr(start, '-');
1245 if (!end)
1246 return;
1247 *end = '\0';
1248 /* find previous -, skips date */
1249 end = strrchr(start, '-');
1250 if (!end)
1251 return;
1252 *end = '\0';
1253 /* find previous -, skips pid */
1254 start = strrchr(start, '-');
1255 if (!start)
1256 return;
1257 start++; /* skip - */
1258
1259 memcpy(td->vpid, start, end - start);
1260 td->vpid[end - start] = '\0';
1261 }
1262
1263 static
1264 int ctf_open_trace_read(struct ctf_trace *td, const char *collection_path,
1265 const char *path, int flags,
1266 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1267 int whence), FILE *metadata_fp)
1268 {
1269 int ret;
1270 struct dirent *dirent;
1271 struct dirent *diriter;
1272 size_t dirent_len;
1273 char *respath, *rescolpath;
1274
1275 td->flags = flags;
1276
1277 /* Open trace directory */
1278 td->dir = opendir(path);
1279 if (!td->dir) {
1280 fprintf(stderr, "[error] Unable to open trace directory.\n");
1281 ret = -ENOENT;
1282 goto error;
1283 }
1284
1285 td->dirfd = open(path, 0);
1286 if (td->dirfd < 0) {
1287 fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
1288 perror("Trace directory open");
1289 ret = -errno;
1290 goto error_dirfd;
1291 }
1292 rescolpath = realpath(collection_path, td->collection_path);
1293 if (!rescolpath) {
1294 fprintf(stderr, "[error] collection path resolution failure\n");
1295 return -EINVAL;
1296 }
1297 respath = realpath(path, td->path);
1298 if (!respath) {
1299 fprintf(stderr, "[error] path resolution failure\n");
1300 return -EINVAL;
1301 }
1302 init_domain_name(td);
1303 init_proc_name(td);
1304 init_vpid(td);
1305
1306 /*
1307 * Keep the metadata file separate.
1308 */
1309
1310 ret = ctf_open_trace_metadata_read(td, move_pos_slow, metadata_fp);
1311 if (ret) {
1312 goto error_metadata;
1313 }
1314
1315 /*
1316 * Open each stream: for each file, try to open, check magic
1317 * number, and get the stream ID to add to the right location in
1318 * the stream array.
1319 */
1320
1321 dirent_len = offsetof(struct dirent, d_name) +
1322 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1323
1324 dirent = malloc(dirent_len);
1325
1326 for (;;) {
1327 ret = readdir_r(td->dir, dirent, &diriter);
1328 if (ret) {
1329 fprintf(stderr, "[error] Readdir error.\n");
1330 goto readdir_error;
1331 }
1332 if (!diriter)
1333 break;
1334 /* Ignore hidden files, ., .. and metadata. */
1335 if (!strncmp(diriter->d_name, ".", 1)
1336 || !strcmp(diriter->d_name, "..")
1337 || !strcmp(diriter->d_name, "metadata"))
1338 continue;
1339 ret = ctf_open_file_stream_read(td, diriter->d_name, flags, move_pos_slow);
1340 if (ret) {
1341 fprintf(stderr, "[error] Open file stream error.\n");
1342 goto readdir_error;
1343 }
1344 }
1345
1346 free(dirent);
1347 return 0;
1348
1349 readdir_error:
1350 free(dirent);
1351 error_metadata:
1352 close(td->dirfd);
1353 error_dirfd:
1354 closedir(td->dir);
1355 error:
1356 return ret;
1357 }
1358
1359 static
1360 struct trace_descriptor *ctf_open_trace(const char *collection_path, const char *path, int flags,
1361 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1362 int whence), FILE *metadata_fp)
1363 {
1364 struct ctf_trace *td;
1365 int ret;
1366
1367 td = g_new0(struct ctf_trace, 1);
1368
1369 switch (flags & O_ACCMODE) {
1370 case O_RDONLY:
1371 if (!path) {
1372 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1373 goto error;
1374 }
1375 ret = ctf_open_trace_read(td, collection_path, path, flags, move_pos_slow, metadata_fp);
1376 if (ret)
1377 goto error;
1378 break;
1379 case O_RDWR:
1380 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1381 goto error;
1382 default:
1383 fprintf(stderr, "[error] Incorrect open flags.\n");
1384 goto error;
1385 }
1386
1387 return &td->parent;
1388 error:
1389 g_free(td);
1390 return NULL;
1391 }
1392
1393
1394 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1395 struct mmap_stream *mmap_info)
1396 {
1397 pos->mmap_offset = 0;
1398 pos->packet_size = 0;
1399 pos->content_size = 0;
1400 pos->content_size_loc = NULL;
1401 pos->fd = mmap_info->fd;
1402 pos->base = 0;
1403 pos->offset = 0;
1404 pos->dummy = false;
1405 pos->cur_index = 0;
1406 pos->packet_index = NULL;
1407 pos->prot = PROT_READ;
1408 pos->flags = MAP_PRIVATE;
1409 pos->parent.rw_table = read_dispatch_table;
1410 pos->parent.event_cb = ctf_read_event;
1411 }
1412
1413 static
1414 int prepare_mmap_stream_definition(struct ctf_trace *td,
1415 struct ctf_file_stream *file_stream)
1416 {
1417 struct ctf_stream_class *stream;
1418 uint64_t stream_id = 0;
1419 int ret;
1420
1421 file_stream->parent.stream_id = stream_id;
1422 if (stream_id >= td->streams->len) {
1423 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1424 "in metadata.\n", stream_id);
1425 ret = -EINVAL;
1426 goto end;
1427 }
1428 stream = g_ptr_array_index(td->streams, stream_id);
1429 if (!stream) {
1430 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1431 "in metadata.\n", stream_id);
1432 ret = -EINVAL;
1433 goto end;
1434 }
1435 file_stream->parent.stream_class = stream;
1436 ret = create_stream_definitions(td, &file_stream->parent);
1437 end:
1438 return ret;
1439 }
1440
1441 static
1442 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1443 struct mmap_stream *mmap_info,
1444 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1445 int whence))
1446 {
1447 int ret;
1448 struct ctf_file_stream *file_stream;
1449
1450 file_stream = g_new0(struct ctf_file_stream, 1);
1451 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1452
1453 file_stream->pos.move_pos_slow = move_pos_slow;
1454
1455 ret = create_trace_definitions(td, &file_stream->parent);
1456 if (ret) {
1457 goto error_def;
1458 }
1459
1460 ret = prepare_mmap_stream_definition(td, file_stream);
1461 if (ret)
1462 goto error_index;
1463
1464 /* Add stream file to stream class */
1465 g_ptr_array_add(file_stream->parent.stream_class->streams,
1466 &file_stream->parent);
1467 return 0;
1468
1469 error_index:
1470 if (file_stream->parent.trace_packet_header)
1471 definition_unref(&file_stream->parent.trace_packet_header->p);
1472 error_def:
1473 g_free(file_stream);
1474 return ret;
1475 }
1476
1477 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1478 struct mmap_stream_list *mmap_list,
1479 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
1480 int whence),
1481 FILE *metadata_fp)
1482 {
1483 int ret;
1484 struct mmap_stream *mmap_info;
1485
1486 ret = ctf_open_trace_metadata_read(td, ctf_move_pos_slow, metadata_fp);
1487 if (ret) {
1488 goto error;
1489 }
1490
1491 /*
1492 * for each stream, try to open, check magic number, and get the
1493 * stream ID to add to the right location in the stream array.
1494 */
1495 cds_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1496 ret = ctf_open_mmap_stream_read(td, mmap_info, move_pos_slow);
1497 if (ret) {
1498 fprintf(stderr, "[error] Open file mmap stream error.\n");
1499 goto error;
1500 }
1501 }
1502
1503 return 0;
1504
1505 error:
1506 return ret;
1507 }
1508
1509 static
1510 struct trace_descriptor *ctf_open_mmap_trace(
1511 struct mmap_stream_list *mmap_list,
1512 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, int whence),
1513 FILE *metadata_fp)
1514 {
1515 struct ctf_trace *td;
1516 int ret;
1517
1518 if (!metadata_fp) {
1519 fprintf(stderr, "[error] No metadata file pointer associated, "
1520 "required for mmap parsing\n");
1521 goto error;
1522 }
1523 if (!move_pos_slow) {
1524 fprintf(stderr, "[error] move_pos_slow function undefined.\n");
1525 goto error;
1526 }
1527 td = g_new0(struct ctf_trace, 1);
1528 ret = ctf_open_mmap_trace_read(td, mmap_list, move_pos_slow, metadata_fp);
1529 if (ret)
1530 goto error_free;
1531
1532 return &td->parent;
1533
1534 error_free:
1535 g_free(td);
1536 error:
1537 return NULL;
1538 }
1539
1540 static
1541 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1542 {
1543 ctf_fini_pos(&file_stream->pos);
1544 close(file_stream->pos.fd);
1545 }
1546
1547 static
1548 void ctf_close_trace(struct trace_descriptor *tdp)
1549 {
1550 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1551 int i;
1552
1553 if (td->streams) {
1554 for (i = 0; i < td->streams->len; i++) {
1555 struct ctf_stream_class *stream;
1556 int j;
1557
1558 stream = g_ptr_array_index(td->streams, i);
1559 if (!stream)
1560 continue;
1561 for (j = 0; j < stream->streams->len; j++) {
1562 struct ctf_file_stream *file_stream;
1563 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1564 ctf_close_file_stream(file_stream);
1565 }
1566
1567 }
1568 g_ptr_array_free(td->streams, TRUE);
1569 }
1570 closedir(td->dir);
1571 g_free(td);
1572 }
1573
1574 void __attribute__((constructor)) ctf_init(void)
1575 {
1576 int ret;
1577
1578 ctf_format.name = g_quark_from_static_string("ctf");
1579 ret = bt_register_format(&ctf_format);
1580 assert(!ret);
1581 }
1582
1583 /* TODO: finalize */
This page took 0.119661 seconds and 4 git commands to generate.