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