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