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