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