22a55e64b50c813fa02827a31cf5c322075cd784
[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 *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 *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 *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 *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 *stream,
903 struct ctf_event *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 return stream_event;
930
931 error:
932 if (stream_event->event_fields)
933 definition_unref(&stream_event->event_fields->p);
934 if (stream_event->event_context)
935 definition_unref(&stream_event->event_context->p);
936 return NULL;
937 }
938
939 static
940 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream *stream)
941 {
942 struct ctf_stream_declaration *stream_class;
943 int ret;
944 int i;
945
946 if (stream->stream_definitions_created)
947 return 0;
948
949 stream_class = stream->stream_class;
950
951 if (stream_class->packet_context_decl) {
952 struct definition *definition =
953 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
954 stream->parent_def_scope, 0, 0, "stream.packet.context");
955 if (!definition) {
956 ret = -EINVAL;
957 goto error;
958 }
959 stream->stream_packet_context = container_of(definition,
960 struct definition_struct, p);
961 stream->parent_def_scope = stream->stream_packet_context->p.scope;
962 }
963 if (stream_class->event_header_decl) {
964 struct definition *definition =
965 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
966 stream->parent_def_scope, 0, 0, "stream.event.header");
967 if (!definition) {
968 ret = -EINVAL;
969 goto error;
970 }
971 stream->stream_event_header =
972 container_of(definition, struct definition_struct, p);
973 stream->parent_def_scope = stream->stream_event_header->p.scope;
974 }
975 if (stream_class->event_context_decl) {
976 struct definition *definition =
977 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
978 stream->parent_def_scope, 0, 0, "stream.event.context");
979 if (!definition) {
980 ret = -EINVAL;
981 goto error;
982 }
983 stream->stream_event_context =
984 container_of(definition, struct definition_struct, p);
985 stream->parent_def_scope = stream->stream_event_context->p.scope;
986 }
987 stream->events_by_id = g_ptr_array_new();
988 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
989 for (i = 0; i < stream->events_by_id->len; i++) {
990 struct ctf_event *event = g_ptr_array_index(stream_class->events_by_id, i);
991 struct ctf_event_definition *stream_event;
992
993 if (!event)
994 continue;
995 stream_event = create_event_definitions(td, stream, event);
996 if (!stream_event)
997 goto error_event;
998 g_ptr_array_index(stream->events_by_id, i) = stream_event;
999 }
1000 return 0;
1001
1002 error_event:
1003 for (i = 0; i < stream->events_by_id->len; i++) {
1004 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1005 if (stream_event)
1006 g_free(stream_event);
1007 }
1008 g_ptr_array_free(stream->events_by_id, TRUE);
1009 error:
1010 if (stream->stream_event_context)
1011 definition_unref(&stream->stream_event_context->p);
1012 if (stream->stream_event_header)
1013 definition_unref(&stream->stream_event_header->p);
1014 if (stream->stream_packet_context)
1015 definition_unref(&stream->stream_packet_context->p);
1016 return ret;
1017 }
1018
1019
1020 static
1021 int create_stream_packet_index(struct ctf_trace *td,
1022 struct ctf_file_stream *file_stream)
1023 {
1024 struct ctf_stream_declaration *stream;
1025 int len_index;
1026 struct ctf_stream_pos *pos;
1027 struct stat filestats;
1028 struct packet_index packet_index;
1029 int first_packet = 1;
1030 int ret;
1031
1032 pos = &file_stream->pos;
1033
1034 ret = fstat(pos->fd, &filestats);
1035 if (ret < 0)
1036 return ret;
1037
1038 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1039 return -EINVAL;
1040
1041 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1042 uint64_t stream_id = 0;
1043
1044 if (pos->base) {
1045 /* unmap old base */
1046 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
1047 if (ret) {
1048 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1049 strerror(errno));
1050 return ret;
1051 }
1052 pos->base = NULL;
1053 }
1054 /* map new base. Need mapping length from header. */
1055 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
1056 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1057 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1058 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1059 pos->offset = 0; /* Position of the packet header */
1060
1061 packet_index.offset = pos->mmap_offset;
1062 packet_index.content_size = 0;
1063 packet_index.packet_size = 0;
1064 packet_index.timestamp_begin = 0;
1065 packet_index.timestamp_end = 0;
1066 packet_index.events_discarded = 0;
1067
1068 /* read and check header, set stream id (and check) */
1069 if (file_stream->parent.trace_packet_header) {
1070 /* Read packet header */
1071 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1072 if (ret)
1073 return ret;
1074 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1075 if (len_index >= 0) {
1076 struct definition *field;
1077 uint64_t magic;
1078
1079 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1080 magic = get_unsigned_int(field);
1081 if (magic != CTF_MAGIC) {
1082 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1083 magic,
1084 file_stream->pos.packet_index->len,
1085 (ssize_t) pos->mmap_offset);
1086 return -EINVAL;
1087 }
1088 }
1089
1090 /* check uuid */
1091 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1092 if (len_index >= 0) {
1093 struct definition_array *defarray;
1094 struct definition *field;
1095 uint64_t i;
1096 uint8_t uuidval[BABELTRACE_UUID_LEN];
1097
1098 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1099 assert(field->declaration->id == CTF_TYPE_ARRAY);
1100 defarray = container_of(field, struct definition_array, p);
1101 assert(array_len(defarray) == BABELTRACE_UUID_LEN);
1102
1103 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1104 struct definition *elem;
1105
1106 elem = array_index(defarray, i);
1107 uuidval[i] = get_unsigned_int(elem);
1108 }
1109 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1110 if (ret) {
1111 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1112 return -EINVAL;
1113 }
1114 }
1115
1116
1117 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1118 if (len_index >= 0) {
1119 struct definition *field;
1120
1121 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1122 stream_id = get_unsigned_int(field);
1123 }
1124 }
1125
1126 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1127 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
1128 return -EINVAL;
1129 }
1130 if (first_packet) {
1131 file_stream->parent.stream_id = stream_id;
1132 if (stream_id >= td->streams->len) {
1133 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1134 return -EINVAL;
1135 }
1136 stream = g_ptr_array_index(td->streams, stream_id);
1137 if (!stream) {
1138 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1139 return -EINVAL;
1140 }
1141 file_stream->parent.stream_class = stream;
1142 ret = create_stream_definitions(td, &file_stream->parent);
1143 if (ret)
1144 return ret;
1145 }
1146 first_packet = 0;
1147
1148 if (file_stream->parent.stream_packet_context) {
1149 /* Read packet context */
1150 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1151 if (ret)
1152 return ret;
1153 /* read content size from header */
1154 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1155 if (len_index >= 0) {
1156 struct definition *field;
1157
1158 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1159 packet_index.content_size = get_unsigned_int(field);
1160 } else {
1161 /* Use file size for packet size */
1162 packet_index.content_size = filestats.st_size * CHAR_BIT;
1163 }
1164
1165 /* read packet size from header */
1166 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1167 if (len_index >= 0) {
1168 struct definition *field;
1169
1170 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1171 packet_index.packet_size = get_unsigned_int(field);
1172 } else {
1173 /* Use content size if non-zero, else file size */
1174 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1175 }
1176
1177 /* read timestamp begin from header */
1178 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1179 if (len_index >= 0) {
1180 struct definition *field;
1181
1182 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1183 packet_index.timestamp_begin = get_unsigned_int(field);
1184 }
1185
1186 /* read timestamp end from header */
1187 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1188 if (len_index >= 0) {
1189 struct definition *field;
1190
1191 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1192 packet_index.timestamp_end = get_unsigned_int(field);
1193 }
1194
1195 /* read events discarded from header */
1196 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1197 if (len_index >= 0) {
1198 struct definition *field;
1199
1200 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1201 packet_index.events_discarded = get_unsigned_int(field);
1202 }
1203 } else {
1204 /* Use file size for packet size */
1205 packet_index.content_size = filestats.st_size * CHAR_BIT;
1206 /* Use content size if non-zero, else file size */
1207 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1208 }
1209
1210 /* Validate content size and packet size values */
1211 if (packet_index.content_size > packet_index.packet_size) {
1212 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1213 packet_index.content_size, packet_index.packet_size);
1214 return -EINVAL;
1215 }
1216
1217 if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
1218 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1219 packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
1220 return -EINVAL;
1221 }
1222
1223 /* Save position after header and context */
1224 packet_index.data_offset = pos->offset;
1225
1226 /* add index to packet array */
1227 g_array_append_val(file_stream->pos.packet_index, packet_index);
1228
1229 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1230 }
1231
1232 /* Move pos back to beginning of file */
1233 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
1234
1235 return 0;
1236 }
1237
1238 static
1239 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream *stream)
1240 {
1241 int ret;
1242
1243 if (td->packet_header_decl) {
1244 struct definition *definition =
1245 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1246 stream->parent_def_scope, 0, 0, "trace.packet.header");
1247 if (!definition) {
1248 ret = -EINVAL;
1249 goto error;
1250 }
1251 stream->trace_packet_header =
1252 container_of(definition, struct definition_struct, p);
1253 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1254 }
1255
1256 return 0;
1257
1258 error:
1259 return ret;
1260 }
1261
1262 /*
1263 * Note: many file streams can inherit from the same stream class
1264 * description (metadata).
1265 */
1266 static
1267 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1268 void (*packet_seek)(struct stream_pos *pos, size_t index,
1269 int whence))
1270 {
1271 int ret;
1272 struct ctf_file_stream *file_stream;
1273
1274 ret = openat(td->dirfd, path, flags);
1275 if (ret < 0) {
1276 perror("File stream openat()");
1277 goto error;
1278 }
1279 file_stream = g_new0(struct ctf_file_stream, 1);
1280
1281 if (packet_seek) {
1282 file_stream->pos.packet_seek = packet_seek;
1283 } else {
1284 fprintf(stderr, "[error] packet_seek function undefined.\n");
1285 ret = -1;
1286 goto error_def;
1287 }
1288
1289 ctf_init_pos(&file_stream->pos, ret, flags);
1290 ret = create_trace_definitions(td, &file_stream->parent);
1291 if (ret)
1292 goto error_def;
1293 /*
1294 * For now, only a single slock is supported.
1295 */
1296 file_stream->parent.current_clock = td->single_clock;
1297 ret = create_stream_packet_index(td, file_stream);
1298 if (ret)
1299 goto error_index;
1300 /* Add stream file to stream class */
1301 g_ptr_array_add(file_stream->parent.stream_class->streams,
1302 &file_stream->parent);
1303 return 0;
1304
1305 error_index:
1306 if (file_stream->parent.trace_packet_header)
1307 definition_unref(&file_stream->parent.trace_packet_header->p);
1308 error_def:
1309 ctf_fini_pos(&file_stream->pos);
1310 close(file_stream->pos.fd);
1311 g_free(file_stream);
1312 error:
1313 return ret;
1314 }
1315
1316 static
1317 int ctf_open_trace_read(struct ctf_trace *td,
1318 const char *path, int flags,
1319 void (*packet_seek)(struct stream_pos *pos, size_t index,
1320 int whence), FILE *metadata_fp)
1321 {
1322 int ret;
1323 struct dirent *dirent;
1324 struct dirent *diriter;
1325 size_t dirent_len;
1326
1327 td->flags = flags;
1328
1329 /* Open trace directory */
1330 td->dir = opendir(path);
1331 if (!td->dir) {
1332 fprintf(stderr, "[error] Unable to open trace directory.\n");
1333 ret = -ENOENT;
1334 goto error;
1335 }
1336
1337 td->dirfd = open(path, 0);
1338 if (td->dirfd < 0) {
1339 fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
1340 perror("Trace directory open");
1341 ret = -errno;
1342 goto error_dirfd;
1343 }
1344 strncpy(td->path, path, sizeof(td->path));
1345 td->path[sizeof(td->path) - 1] = '\0';
1346
1347 /*
1348 * Keep the metadata file separate.
1349 */
1350
1351 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1352 if (ret) {
1353 goto error_metadata;
1354 }
1355
1356 /*
1357 * Open each stream: for each file, try to open, check magic
1358 * number, and get the stream ID to add to the right location in
1359 * the stream array.
1360 */
1361
1362 dirent_len = offsetof(struct dirent, d_name) +
1363 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1364
1365 dirent = malloc(dirent_len);
1366
1367 for (;;) {
1368 ret = readdir_r(td->dir, dirent, &diriter);
1369 if (ret) {
1370 fprintf(stderr, "[error] Readdir error.\n");
1371 goto readdir_error;
1372 }
1373 if (!diriter)
1374 break;
1375 /* Ignore hidden files, ., .. and metadata. */
1376 if (!strncmp(diriter->d_name, ".", 1)
1377 || !strcmp(diriter->d_name, "..")
1378 || !strcmp(diriter->d_name, "metadata"))
1379 continue;
1380 ret = ctf_open_file_stream_read(td, diriter->d_name,
1381 flags, packet_seek);
1382 if (ret) {
1383 fprintf(stderr, "[error] Open file stream error.\n");
1384 goto readdir_error;
1385 }
1386 }
1387
1388 free(dirent);
1389 return 0;
1390
1391 readdir_error:
1392 free(dirent);
1393 error_metadata:
1394 close(td->dirfd);
1395 error_dirfd:
1396 closedir(td->dir);
1397 error:
1398 return ret;
1399 }
1400
1401 static
1402 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1403 void (*packet_seek)(struct stream_pos *pos, size_t index,
1404 int whence), FILE *metadata_fp)
1405 {
1406 struct ctf_trace *td;
1407 int ret;
1408
1409 /*
1410 * If packet_seek is NULL, we provide our default version.
1411 */
1412 if (!packet_seek)
1413 packet_seek = ctf_packet_seek;
1414
1415 td = g_new0(struct ctf_trace, 1);
1416
1417 switch (flags & O_ACCMODE) {
1418 case O_RDONLY:
1419 if (!path) {
1420 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1421 goto error;
1422 }
1423 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1424 if (ret)
1425 goto error;
1426 break;
1427 case O_RDWR:
1428 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1429 goto error;
1430 default:
1431 fprintf(stderr, "[error] Incorrect open flags.\n");
1432 goto error;
1433 }
1434
1435 return &td->parent;
1436 error:
1437 g_free(td);
1438 return NULL;
1439 }
1440
1441
1442 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1443 struct mmap_stream *mmap_info)
1444 {
1445 pos->mmap_offset = 0;
1446 pos->packet_size = 0;
1447 pos->content_size = 0;
1448 pos->content_size_loc = NULL;
1449 pos->fd = mmap_info->fd;
1450 pos->base = 0;
1451 pos->offset = 0;
1452 pos->dummy = false;
1453 pos->cur_index = 0;
1454 pos->packet_index = NULL;
1455 pos->prot = PROT_READ;
1456 pos->flags = MAP_PRIVATE;
1457 pos->parent.rw_table = read_dispatch_table;
1458 pos->parent.event_cb = ctf_read_event;
1459 }
1460
1461 static
1462 int prepare_mmap_stream_definition(struct ctf_trace *td,
1463 struct ctf_file_stream *file_stream)
1464 {
1465 struct ctf_stream_declaration *stream;
1466 uint64_t stream_id = 0;
1467 int ret;
1468
1469 file_stream->parent.stream_id = stream_id;
1470 if (stream_id >= td->streams->len) {
1471 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1472 "in metadata.\n", stream_id);
1473 ret = -EINVAL;
1474 goto end;
1475 }
1476 stream = g_ptr_array_index(td->streams, stream_id);
1477 if (!stream) {
1478 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1479 "in metadata.\n", stream_id);
1480 ret = -EINVAL;
1481 goto end;
1482 }
1483 file_stream->parent.stream_class = stream;
1484 ret = create_stream_definitions(td, &file_stream->parent);
1485 end:
1486 return ret;
1487 }
1488
1489 static
1490 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1491 struct mmap_stream *mmap_info,
1492 void (*packet_seek)(struct stream_pos *pos, size_t index,
1493 int whence))
1494 {
1495 int ret;
1496 struct ctf_file_stream *file_stream;
1497
1498 file_stream = g_new0(struct ctf_file_stream, 1);
1499 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1500
1501 file_stream->pos.packet_seek = packet_seek;
1502
1503 ret = create_trace_definitions(td, &file_stream->parent);
1504 if (ret) {
1505 goto error_def;
1506 }
1507
1508 ret = prepare_mmap_stream_definition(td, file_stream);
1509 if (ret)
1510 goto error_index;
1511
1512 /* Add stream file to stream class */
1513 g_ptr_array_add(file_stream->parent.stream_class->streams,
1514 &file_stream->parent);
1515 return 0;
1516
1517 error_index:
1518 if (file_stream->parent.trace_packet_header)
1519 definition_unref(&file_stream->parent.trace_packet_header->p);
1520 error_def:
1521 g_free(file_stream);
1522 return ret;
1523 }
1524
1525 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1526 struct mmap_stream_list *mmap_list,
1527 void (*packet_seek)(struct stream_pos *pos, size_t index,
1528 int whence),
1529 FILE *metadata_fp)
1530 {
1531 int ret;
1532 struct mmap_stream *mmap_info;
1533
1534 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1535 if (ret) {
1536 goto error;
1537 }
1538
1539 /*
1540 * for each stream, try to open, check magic number, and get the
1541 * stream ID to add to the right location in the stream array.
1542 */
1543 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1544 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
1545 if (ret) {
1546 fprintf(stderr, "[error] Open file mmap stream error.\n");
1547 goto error;
1548 }
1549 }
1550
1551 return 0;
1552
1553 error:
1554 return ret;
1555 }
1556
1557 static
1558 struct trace_descriptor *ctf_open_mmap_trace(
1559 struct mmap_stream_list *mmap_list,
1560 void (*packet_seek)(struct stream_pos *pos, size_t index,
1561 int whence),
1562 FILE *metadata_fp)
1563 {
1564 struct ctf_trace *td;
1565 int ret;
1566
1567 if (!metadata_fp) {
1568 fprintf(stderr, "[error] No metadata file pointer associated, "
1569 "required for mmap parsing\n");
1570 goto error;
1571 }
1572 if (!packet_seek) {
1573 fprintf(stderr, "[error] packet_seek function undefined.\n");
1574 goto error;
1575 }
1576 td = g_new0(struct ctf_trace, 1);
1577 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
1578 if (ret)
1579 goto error_free;
1580
1581 return &td->parent;
1582
1583 error_free:
1584 g_free(td);
1585 error:
1586 return NULL;
1587 }
1588
1589 static
1590 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1591 {
1592 ctf_fini_pos(&file_stream->pos);
1593 close(file_stream->pos.fd);
1594 }
1595
1596 static
1597 void ctf_close_trace(struct trace_descriptor *tdp)
1598 {
1599 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1600 int i;
1601
1602 if (td->streams) {
1603 for (i = 0; i < td->streams->len; i++) {
1604 struct ctf_stream_declaration *stream;
1605 int j;
1606
1607 stream = g_ptr_array_index(td->streams, i);
1608 if (!stream)
1609 continue;
1610 for (j = 0; j < stream->streams->len; j++) {
1611 struct ctf_file_stream *file_stream;
1612 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1613 ctf_close_file_stream(file_stream);
1614 }
1615
1616 }
1617 g_ptr_array_free(td->streams, TRUE);
1618 }
1619 closedir(td->dir);
1620 g_free(td);
1621 }
1622
1623 static
1624 void ctf_set_context(struct trace_descriptor *descriptor,
1625 struct bt_context *ctx)
1626 {
1627 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1628 parent);
1629
1630 td->ctx = ctx;
1631 }
1632
1633 static
1634 void ctf_set_handle(struct trace_descriptor *descriptor,
1635 struct bt_trace_handle *handle)
1636 {
1637 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1638 parent);
1639
1640 td->handle = handle;
1641 }
1642
1643 void __attribute__((constructor)) ctf_init(void)
1644 {
1645 int ret;
1646
1647 ctf_format.name = g_quark_from_static_string("ctf");
1648 ret = bt_register_format(&ctf_format);
1649 assert(!ret);
1650 }
1651
1652 /* TODO: finalize */
This page took 0.086701 seconds and 4 git commands to generate.