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