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