Fix: report appropriate field in error message
[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 /* 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
171 error:
172 return -1ULL;
173 }
174
175 static
176 uint64_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
215 error:
216 return -1ULL;
217 }
218
219 /*
220 * Update stream current timestamp, keep at clock frequency.
221 */
222 static
223 void 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 */
253 void 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 }
310 seconds:
311 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
312 ts_sec, ts_nsec);
313
314 end:
315 return;
316 }
317
318 static
319 int 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
427 error:
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
432 static
433 int 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
482 error:
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
487 void 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
523 void 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 */
545 void 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 uint64_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 * Deal with 32-bit wrap-around if the
615 * tracer provided a 32-bit field.
616 */
617 if (packet_index->events_discarded_len == 32) {
618 events_discarded_diff = (uint32_t) events_discarded_diff;
619 }
620 }
621 file_stream->parent.events_discarded = events_discarded_diff;
622 file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
623 /* The reader will expect us to skip padding */
624 ++pos->cur_index;
625 break;
626 }
627 case SEEK_SET:
628 pos->cur_index = index;
629 file_stream->parent.prev_timestamp = 0;
630 file_stream->parent.prev_timestamp_end = 0;
631 break;
632 default:
633 assert(0);
634 }
635 if (pos->cur_index >= pos->packet_index->len) {
636 /*
637 * When a stream reaches the end of the
638 * file, we need to show the number of
639 * events discarded ourselves, because
640 * there is no next event scheduled to
641 * be printed in the output.
642 */
643 if (file_stream->parent.events_discarded) {
644 /*
645 * We need to check if we are in trace
646 * read or called from packet indexing.
647 * In this last case, the collection is
648 * not there, so we cannot print the
649 * timestamps.
650 */
651 if ((&file_stream->parent)->stream_class->trace->collection) {
652 fflush(stdout);
653 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events at end of stream between [",
654 file_stream->parent.events_discarded);
655 ctf_print_timestamp(stderr, &file_stream->parent,
656 file_stream->parent.prev_timestamp);
657 fprintf(stderr, "] and [");
658 ctf_print_timestamp(stderr, &file_stream->parent,
659 file_stream->parent.prev_timestamp_end);
660 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
661 fflush(stderr);
662 }
663 file_stream->parent.events_discarded = 0;
664 }
665 pos->offset = EOF;
666 return;
667 }
668 packet_index = &g_array_index(pos->packet_index, struct packet_index,
669 pos->cur_index);
670 pos->mmap_offset = packet_index->offset;
671
672 /* Lookup context/packet size in index */
673 file_stream->parent.timestamp = packet_index->timestamp_begin;
674 pos->content_size = packet_index->content_size;
675 pos->packet_size = packet_index->packet_size;
676 if (packet_index->data_offset < packet_index->content_size) {
677 pos->offset = 0; /* will read headers */
678 } else if (packet_index->data_offset == packet_index->content_size) {
679 /* empty packet */
680 pos->offset = packet_index->data_offset;
681 whence = SEEK_CUR;
682 goto read_next_packet;
683 } else {
684 pos->offset = EOF;
685 return;
686 }
687 }
688 /* map new base. Need mapping length from header. */
689 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
690 pos->flags, pos->fd, pos->mmap_offset);
691 if (pos->base == MAP_FAILED) {
692 fprintf(stderr, "[error] mmap error %s.\n",
693 strerror(errno));
694 assert(0);
695 }
696
697 /* update trace_packet_header and stream_packet_context */
698 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
699 /* Read packet header */
700 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
701 assert(!ret);
702 }
703 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
704 /* Read packet context */
705 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
706 assert(!ret);
707 }
708 }
709
710 static
711 int packet_metadata(struct ctf_trace *td, FILE *fp)
712 {
713 uint32_t magic;
714 size_t len;
715 int ret = 0;
716
717 len = fread(&magic, sizeof(magic), 1, fp);
718 if (len != 1) {
719 goto end;
720 }
721 if (magic == TSDL_MAGIC) {
722 ret = 1;
723 td->byte_order = BYTE_ORDER;
724 CTF_TRACE_SET_FIELD(td, byte_order);
725 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
726 ret = 1;
727 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
728 LITTLE_ENDIAN : BIG_ENDIAN;
729 CTF_TRACE_SET_FIELD(td, byte_order);
730 }
731 end:
732 rewind(fp);
733 return ret;
734 }
735
736 /*
737 * Returns 0 on success, -1 on error.
738 */
739 static
740 int check_version(unsigned int major, unsigned int minor)
741 {
742 switch (major) {
743 case 1:
744 switch (minor) {
745 case 8:
746 return 0;
747 default:
748 goto warning;
749 }
750 default:
751 goto warning;
752
753 }
754
755 /* eventually return an error instead of warning */
756 warning:
757 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
758 major, minor);
759 return 0;
760 }
761
762 static
763 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
764 FILE *out)
765 {
766 struct metadata_packet_header header;
767 size_t readlen, writelen, toread;
768 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
769 int ret = 0;
770
771 readlen = fread(&header, header_sizeof(header), 1, in);
772 if (readlen < 1)
773 return -EINVAL;
774
775 if (td->byte_order != BYTE_ORDER) {
776 header.magic = GUINT32_SWAP_LE_BE(header.magic);
777 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
778 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
779 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
780 }
781 if (header.checksum)
782 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
783 if (header.compression_scheme) {
784 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
785 header.compression_scheme);
786 return -EINVAL;
787 }
788 if (header.encryption_scheme) {
789 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
790 header.encryption_scheme);
791 return -EINVAL;
792 }
793 if (header.checksum_scheme) {
794 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
795 header.checksum_scheme);
796 return -EINVAL;
797 }
798 if (check_version(header.major, header.minor) < 0)
799 return -EINVAL;
800 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
801 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
802 CTF_TRACE_SET_FIELD(td, uuid);
803 } else {
804 if (babeltrace_uuid_compare(header.uuid, td->uuid))
805 return -EINVAL;
806 }
807
808 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
809
810 for (;;) {
811 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
812 if (ferror(in)) {
813 ret = -EINVAL;
814 break;
815 }
816 if (babeltrace_debug) {
817 buf[readlen] = '\0';
818 fprintf(stderr, "[debug] metadata packet read: %s\n",
819 buf);
820 }
821
822 writelen = fwrite(buf, sizeof(char), readlen, out);
823 if (writelen < readlen) {
824 ret = -EIO;
825 break;
826 }
827 if (ferror(out)) {
828 ret = -EINVAL;
829 break;
830 }
831 toread -= readlen;
832 if (!toread) {
833 ret = 0; /* continue reading next packet */
834 goto read_padding;
835 }
836 }
837 return ret;
838
839 read_padding:
840 toread = (header.packet_size - header.content_size) / CHAR_BIT;
841 ret = fseek(in, toread, SEEK_CUR);
842 if (ret < 0) {
843 fprintf(stderr, "[warning] Missing padding at end of file\n");
844 ret = 0;
845 }
846 return ret;
847 }
848
849 static
850 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
851 char **buf)
852 {
853 FILE *in, *out;
854 size_t size;
855 int ret;
856
857 in = *fp;
858 /*
859 * Using strlen on *buf instead of size of open_memstream
860 * because its size includes garbage at the end (after final
861 * \0). This is the allocated size, not the actual string size.
862 */
863 out = babeltrace_open_memstream(buf, &size);
864 if (out == NULL) {
865 perror("Metadata open_memstream");
866 return -errno;
867 }
868 for (;;) {
869 ret = ctf_open_trace_metadata_packet_read(td, in, out);
870 if (ret) {
871 break;
872 }
873 if (feof(in)) {
874 ret = 0;
875 break;
876 }
877 }
878 /* close to flush the buffer */
879 ret = babeltrace_close_memstream(buf, &size, out);
880 if (ret < 0) {
881 perror("babeltrace_flush_memstream");
882 fclose(in);
883 return -errno;
884 }
885 fclose(in);
886 /* open for reading */
887 *fp = babeltrace_fmemopen(*buf, strlen(*buf), "rb");
888 if (!*fp) {
889 perror("Metadata fmemopen");
890 return -errno;
891 }
892 return 0;
893 }
894
895 static
896 int ctf_open_trace_metadata_read(struct ctf_trace *td,
897 void (*packet_seek)(struct stream_pos *pos, size_t index,
898 int whence), FILE *metadata_fp)
899 {
900 struct ctf_scanner *scanner;
901 struct ctf_file_stream *metadata_stream;
902 FILE *fp;
903 char *buf = NULL;
904 int ret = 0;
905
906 metadata_stream = g_new0(struct ctf_file_stream, 1);
907
908 if (packet_seek) {
909 metadata_stream->pos.packet_seek = packet_seek;
910 } else {
911 fprintf(stderr, "[error] packet_seek function undefined.\n");
912 ret = -1;
913 goto end_stream;
914 }
915
916 if (metadata_fp) {
917 fp = metadata_fp;
918 } else {
919 td->metadata = &metadata_stream->parent;
920 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
921 if (metadata_stream->pos.fd < 0) {
922 fprintf(stderr, "Unable to open metadata.\n");
923 return metadata_stream->pos.fd;
924 }
925
926 fp = fdopen(metadata_stream->pos.fd, "r");
927 if (!fp) {
928 fprintf(stderr, "[error] Unable to open metadata stream.\n");
929 perror("Metadata stream open");
930 ret = -errno;
931 goto end_stream;
932 }
933 }
934 if (babeltrace_debug)
935 yydebug = 1;
936
937 if (packet_metadata(td, fp)) {
938 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
939 if (ret)
940 goto end_packet_read;
941 } else {
942 unsigned int major, minor;
943 ssize_t nr_items;
944
945 td->byte_order = BYTE_ORDER;
946
947 /* Check text-only metadata header and version */
948 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
949 if (nr_items < 2)
950 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
951 if (check_version(major, minor) < 0) {
952 ret = -EINVAL;
953 goto end_packet_read;
954 }
955 rewind(fp);
956 }
957
958 scanner = ctf_scanner_alloc(fp);
959 if (!scanner) {
960 fprintf(stderr, "[error] Error allocating scanner\n");
961 ret = -ENOMEM;
962 goto end_scanner_alloc;
963 }
964 ret = ctf_scanner_append_ast(scanner);
965 if (ret) {
966 fprintf(stderr, "[error] Error creating AST\n");
967 goto end;
968 }
969
970 if (babeltrace_debug) {
971 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
972 if (ret) {
973 fprintf(stderr, "[error] Error visiting AST for XML output\n");
974 goto end;
975 }
976 }
977
978 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
979 if (ret) {
980 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
981 goto end;
982 }
983 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
984 td, td->byte_order);
985 if (ret) {
986 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
987 goto end;
988 }
989 end:
990 ctf_scanner_free(scanner);
991 end_scanner_alloc:
992 end_packet_read:
993 if (fp)
994 fclose(fp);
995 free(buf);
996 end_stream:
997 close(metadata_stream->pos.fd);
998 if (ret)
999 g_free(metadata_stream);
1000 return ret;
1001 }
1002
1003 static
1004 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1005 struct ctf_stream_definition *stream,
1006 struct ctf_event_declaration *event)
1007 {
1008 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1009
1010 if (event->context_decl) {
1011 struct definition *definition =
1012 event->context_decl->p.definition_new(&event->context_decl->p,
1013 stream->parent_def_scope, 0, 0, "event.context");
1014 if (!definition) {
1015 goto error;
1016 }
1017 stream_event->event_context = container_of(definition,
1018 struct definition_struct, p);
1019 stream->parent_def_scope = stream_event->event_context->p.scope;
1020 }
1021 if (event->fields_decl) {
1022 struct definition *definition =
1023 event->fields_decl->p.definition_new(&event->fields_decl->p,
1024 stream->parent_def_scope, 0, 0, "event.fields");
1025 if (!definition) {
1026 goto error;
1027 }
1028 stream_event->event_fields = container_of(definition,
1029 struct definition_struct, p);
1030 stream->parent_def_scope = stream_event->event_fields->p.scope;
1031 }
1032 stream_event->stream = stream;
1033 return stream_event;
1034
1035 error:
1036 if (stream_event->event_fields)
1037 definition_unref(&stream_event->event_fields->p);
1038 if (stream_event->event_context)
1039 definition_unref(&stream_event->event_context->p);
1040 return NULL;
1041 }
1042
1043 static
1044 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1045 {
1046 struct ctf_stream_declaration *stream_class;
1047 int ret;
1048 int i;
1049
1050 if (stream->stream_definitions_created)
1051 return 0;
1052
1053 stream_class = stream->stream_class;
1054
1055 if (stream_class->packet_context_decl) {
1056 struct definition *definition =
1057 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1058 stream->parent_def_scope, 0, 0, "stream.packet.context");
1059 if (!definition) {
1060 ret = -EINVAL;
1061 goto error;
1062 }
1063 stream->stream_packet_context = container_of(definition,
1064 struct definition_struct, p);
1065 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1066 }
1067 if (stream_class->event_header_decl) {
1068 struct definition *definition =
1069 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1070 stream->parent_def_scope, 0, 0, "stream.event.header");
1071 if (!definition) {
1072 ret = -EINVAL;
1073 goto error;
1074 }
1075 stream->stream_event_header =
1076 container_of(definition, struct definition_struct, p);
1077 stream->parent_def_scope = stream->stream_event_header->p.scope;
1078 }
1079 if (stream_class->event_context_decl) {
1080 struct definition *definition =
1081 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1082 stream->parent_def_scope, 0, 0, "stream.event.context");
1083 if (!definition) {
1084 ret = -EINVAL;
1085 goto error;
1086 }
1087 stream->stream_event_context =
1088 container_of(definition, struct definition_struct, p);
1089 stream->parent_def_scope = stream->stream_event_context->p.scope;
1090 }
1091 stream->events_by_id = g_ptr_array_new();
1092 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1093 for (i = 0; i < stream->events_by_id->len; i++) {
1094 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1095 struct ctf_event_definition *stream_event;
1096
1097 if (!event)
1098 continue;
1099 stream_event = create_event_definitions(td, stream, event);
1100 if (!stream_event)
1101 goto error_event;
1102 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1103 }
1104 return 0;
1105
1106 error_event:
1107 for (i = 0; i < stream->events_by_id->len; i++) {
1108 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1109 if (stream_event)
1110 g_free(stream_event);
1111 }
1112 g_ptr_array_free(stream->events_by_id, TRUE);
1113 error:
1114 if (stream->stream_event_context)
1115 definition_unref(&stream->stream_event_context->p);
1116 if (stream->stream_event_header)
1117 definition_unref(&stream->stream_event_header->p);
1118 if (stream->stream_packet_context)
1119 definition_unref(&stream->stream_packet_context->p);
1120 return ret;
1121 }
1122
1123
1124 static
1125 int create_stream_packet_index(struct ctf_trace *td,
1126 struct ctf_file_stream *file_stream)
1127 {
1128 struct ctf_stream_declaration *stream;
1129 int len_index;
1130 struct ctf_stream_pos *pos;
1131 struct stat filestats;
1132 struct packet_index packet_index;
1133 int first_packet = 1;
1134 int ret;
1135
1136 pos = &file_stream->pos;
1137
1138 ret = fstat(pos->fd, &filestats);
1139 if (ret < 0)
1140 return ret;
1141
1142 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1143 return -EINVAL;
1144
1145 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1146 uint64_t stream_id = 0;
1147
1148 if (pos->base) {
1149 /* unmap old base */
1150 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
1151 if (ret) {
1152 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1153 strerror(errno));
1154 return ret;
1155 }
1156 pos->base = NULL;
1157 }
1158 /* map new base. Need mapping length from header. */
1159 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
1160 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1161 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1162 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1163 pos->offset = 0; /* Position of the packet header */
1164
1165 packet_index.offset = pos->mmap_offset;
1166 packet_index.content_size = 0;
1167 packet_index.packet_size = 0;
1168 packet_index.timestamp_begin = 0;
1169 packet_index.timestamp_end = 0;
1170 packet_index.events_discarded = 0;
1171
1172 /* read and check header, set stream id (and check) */
1173 if (file_stream->parent.trace_packet_header) {
1174 /* Read packet header */
1175 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1176 if (ret)
1177 return ret;
1178 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1179 if (len_index >= 0) {
1180 struct definition *field;
1181 uint64_t magic;
1182
1183 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1184 magic = get_unsigned_int(field);
1185 if (magic != CTF_MAGIC) {
1186 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1187 magic,
1188 file_stream->pos.packet_index->len,
1189 (ssize_t) pos->mmap_offset);
1190 return -EINVAL;
1191 }
1192 }
1193
1194 /* check uuid */
1195 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1196 if (len_index >= 0) {
1197 struct definition_array *defarray;
1198 struct definition *field;
1199 uint64_t i;
1200 uint8_t uuidval[BABELTRACE_UUID_LEN];
1201
1202 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1203 assert(field->declaration->id == CTF_TYPE_ARRAY);
1204 defarray = container_of(field, struct definition_array, p);
1205 assert(array_len(defarray) == BABELTRACE_UUID_LEN);
1206
1207 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1208 struct definition *elem;
1209
1210 elem = array_index(defarray, i);
1211 uuidval[i] = get_unsigned_int(elem);
1212 }
1213 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1214 if (ret) {
1215 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1216 return -EINVAL;
1217 }
1218 }
1219
1220
1221 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1222 if (len_index >= 0) {
1223 struct definition *field;
1224
1225 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1226 stream_id = get_unsigned_int(field);
1227 }
1228 }
1229
1230 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1231 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
1232 return -EINVAL;
1233 }
1234 if (first_packet) {
1235 file_stream->parent.stream_id = stream_id;
1236 if (stream_id >= td->streams->len) {
1237 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1238 return -EINVAL;
1239 }
1240 stream = g_ptr_array_index(td->streams, stream_id);
1241 if (!stream) {
1242 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1243 return -EINVAL;
1244 }
1245 file_stream->parent.stream_class = stream;
1246 ret = create_stream_definitions(td, &file_stream->parent);
1247 if (ret)
1248 return ret;
1249 }
1250 first_packet = 0;
1251
1252 if (file_stream->parent.stream_packet_context) {
1253 /* Read packet context */
1254 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1255 if (ret)
1256 return ret;
1257 /* read content size from header */
1258 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1259 if (len_index >= 0) {
1260 struct definition *field;
1261
1262 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1263 packet_index.content_size = get_unsigned_int(field);
1264 } else {
1265 /* Use file size for packet size */
1266 packet_index.content_size = filestats.st_size * CHAR_BIT;
1267 }
1268
1269 /* read packet size from header */
1270 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1271 if (len_index >= 0) {
1272 struct definition *field;
1273
1274 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1275 packet_index.packet_size = get_unsigned_int(field);
1276 } else {
1277 /* Use content size if non-zero, else file size */
1278 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1279 }
1280
1281 /* read timestamp begin from header */
1282 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1283 if (len_index >= 0) {
1284 struct definition *field;
1285
1286 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1287 packet_index.timestamp_begin = get_unsigned_int(field);
1288 }
1289
1290 /* read timestamp end from header */
1291 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1292 if (len_index >= 0) {
1293 struct definition *field;
1294
1295 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1296 packet_index.timestamp_end = get_unsigned_int(field);
1297 }
1298
1299 /* read events discarded from header */
1300 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1301 if (len_index >= 0) {
1302 struct definition *field;
1303
1304 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1305 packet_index.events_discarded = get_unsigned_int(field);
1306 packet_index.events_discarded_len = get_int_len(field);
1307 }
1308 } else {
1309 /* Use file size for packet size */
1310 packet_index.content_size = filestats.st_size * CHAR_BIT;
1311 /* Use content size if non-zero, else file size */
1312 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1313 }
1314
1315 /* Validate content size and packet size values */
1316 if (packet_index.content_size > packet_index.packet_size) {
1317 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1318 packet_index.content_size, packet_index.packet_size);
1319 return -EINVAL;
1320 }
1321
1322 if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
1323 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1324 packet_index.packet_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
1325 return -EINVAL;
1326 }
1327
1328 /* Save position after header and context */
1329 packet_index.data_offset = pos->offset;
1330
1331 /* add index to packet array */
1332 g_array_append_val(file_stream->pos.packet_index, packet_index);
1333
1334 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1335 }
1336
1337 /* Move pos back to beginning of file */
1338 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
1339
1340 return 0;
1341 }
1342
1343 static
1344 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1345 {
1346 int ret;
1347
1348 if (td->packet_header_decl) {
1349 struct definition *definition =
1350 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1351 stream->parent_def_scope, 0, 0, "trace.packet.header");
1352 if (!definition) {
1353 ret = -EINVAL;
1354 goto error;
1355 }
1356 stream->trace_packet_header =
1357 container_of(definition, struct definition_struct, p);
1358 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1359 }
1360
1361 return 0;
1362
1363 error:
1364 return ret;
1365 }
1366
1367 /*
1368 * Note: many file streams can inherit from the same stream class
1369 * description (metadata).
1370 */
1371 static
1372 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1373 void (*packet_seek)(struct stream_pos *pos, size_t index,
1374 int whence))
1375 {
1376 int ret;
1377 struct ctf_file_stream *file_stream;
1378
1379 ret = openat(td->dirfd, path, flags);
1380 if (ret < 0) {
1381 perror("File stream openat()");
1382 goto error;
1383 }
1384 file_stream = g_new0(struct ctf_file_stream, 1);
1385
1386 if (packet_seek) {
1387 file_stream->pos.packet_seek = packet_seek;
1388 } else {
1389 fprintf(stderr, "[error] packet_seek function undefined.\n");
1390 ret = -1;
1391 goto error_def;
1392 }
1393
1394 ctf_init_pos(&file_stream->pos, ret, flags);
1395 ret = create_trace_definitions(td, &file_stream->parent);
1396 if (ret)
1397 goto error_def;
1398 /*
1399 * For now, only a single slock is supported.
1400 */
1401 file_stream->parent.current_clock = td->single_clock;
1402 ret = create_stream_packet_index(td, file_stream);
1403 if (ret)
1404 goto error_index;
1405 /* Add stream file to stream class */
1406 g_ptr_array_add(file_stream->parent.stream_class->streams,
1407 &file_stream->parent);
1408 return 0;
1409
1410 error_index:
1411 if (file_stream->parent.trace_packet_header)
1412 definition_unref(&file_stream->parent.trace_packet_header->p);
1413 error_def:
1414 ctf_fini_pos(&file_stream->pos);
1415 close(file_stream->pos.fd);
1416 g_free(file_stream);
1417 error:
1418 return ret;
1419 }
1420
1421 static
1422 int ctf_open_trace_read(struct ctf_trace *td,
1423 const char *path, int flags,
1424 void (*packet_seek)(struct stream_pos *pos, size_t index,
1425 int whence), FILE *metadata_fp)
1426 {
1427 int ret;
1428 struct dirent *dirent;
1429 struct dirent *diriter;
1430 size_t dirent_len;
1431
1432 td->flags = flags;
1433
1434 /* Open trace directory */
1435 td->dir = opendir(path);
1436 if (!td->dir) {
1437 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1438 ret = -ENOENT;
1439 goto error;
1440 }
1441
1442 td->dirfd = open(path, 0);
1443 if (td->dirfd < 0) {
1444 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1445 perror("Trace directory open");
1446 ret = -errno;
1447 goto error_dirfd;
1448 }
1449 strncpy(td->path, path, sizeof(td->path));
1450 td->path[sizeof(td->path) - 1] = '\0';
1451
1452 /*
1453 * Keep the metadata file separate.
1454 */
1455
1456 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1457 if (ret) {
1458 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1459 goto error_metadata;
1460 }
1461
1462 /*
1463 * Open each stream: for each file, try to open, check magic
1464 * number, and get the stream ID to add to the right location in
1465 * the stream array.
1466 */
1467
1468 dirent_len = offsetof(struct dirent, d_name) +
1469 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1470
1471 dirent = malloc(dirent_len);
1472
1473 for (;;) {
1474 ret = readdir_r(td->dir, dirent, &diriter);
1475 if (ret) {
1476 fprintf(stderr, "[error] Readdir error.\n");
1477 goto readdir_error;
1478 }
1479 if (!diriter)
1480 break;
1481 /* Ignore hidden files, ., .. and metadata. */
1482 if (!strncmp(diriter->d_name, ".", 1)
1483 || !strcmp(diriter->d_name, "..")
1484 || !strcmp(diriter->d_name, "metadata"))
1485 continue;
1486 ret = ctf_open_file_stream_read(td, diriter->d_name,
1487 flags, packet_seek);
1488 if (ret) {
1489 fprintf(stderr, "[error] Open file stream error.\n");
1490 goto readdir_error;
1491 }
1492 }
1493
1494 free(dirent);
1495 return 0;
1496
1497 readdir_error:
1498 free(dirent);
1499 error_metadata:
1500 close(td->dirfd);
1501 error_dirfd:
1502 closedir(td->dir);
1503 error:
1504 return ret;
1505 }
1506
1507 static
1508 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1509 void (*packet_seek)(struct stream_pos *pos, size_t index,
1510 int whence), FILE *metadata_fp)
1511 {
1512 struct ctf_trace *td;
1513 int ret;
1514
1515 /*
1516 * If packet_seek is NULL, we provide our default version.
1517 */
1518 if (!packet_seek)
1519 packet_seek = ctf_packet_seek;
1520
1521 td = g_new0(struct ctf_trace, 1);
1522
1523 switch (flags & O_ACCMODE) {
1524 case O_RDONLY:
1525 if (!path) {
1526 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1527 goto error;
1528 }
1529 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1530 if (ret)
1531 goto error;
1532 break;
1533 case O_RDWR:
1534 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1535 goto error;
1536 default:
1537 fprintf(stderr, "[error] Incorrect open flags.\n");
1538 goto error;
1539 }
1540
1541 return &td->parent;
1542 error:
1543 g_free(td);
1544 return NULL;
1545 }
1546
1547
1548 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1549 struct mmap_stream *mmap_info)
1550 {
1551 pos->mmap_offset = 0;
1552 pos->packet_size = 0;
1553 pos->content_size = 0;
1554 pos->content_size_loc = NULL;
1555 pos->fd = mmap_info->fd;
1556 pos->base = 0;
1557 pos->offset = 0;
1558 pos->dummy = false;
1559 pos->cur_index = 0;
1560 pos->packet_index = NULL;
1561 pos->prot = PROT_READ;
1562 pos->flags = MAP_PRIVATE;
1563 pos->parent.rw_table = read_dispatch_table;
1564 pos->parent.event_cb = ctf_read_event;
1565 }
1566
1567 static
1568 int prepare_mmap_stream_definition(struct ctf_trace *td,
1569 struct ctf_file_stream *file_stream)
1570 {
1571 struct ctf_stream_declaration *stream;
1572 uint64_t stream_id = 0;
1573 int ret;
1574
1575 file_stream->parent.stream_id = stream_id;
1576 if (stream_id >= td->streams->len) {
1577 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1578 "in metadata.\n", stream_id);
1579 ret = -EINVAL;
1580 goto end;
1581 }
1582 stream = g_ptr_array_index(td->streams, stream_id);
1583 if (!stream) {
1584 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1585 "in metadata.\n", stream_id);
1586 ret = -EINVAL;
1587 goto end;
1588 }
1589 file_stream->parent.stream_class = stream;
1590 ret = create_stream_definitions(td, &file_stream->parent);
1591 end:
1592 return ret;
1593 }
1594
1595 static
1596 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1597 struct mmap_stream *mmap_info,
1598 void (*packet_seek)(struct stream_pos *pos, size_t index,
1599 int whence))
1600 {
1601 int ret;
1602 struct ctf_file_stream *file_stream;
1603
1604 file_stream = g_new0(struct ctf_file_stream, 1);
1605 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1606
1607 file_stream->pos.packet_seek = packet_seek;
1608
1609 ret = create_trace_definitions(td, &file_stream->parent);
1610 if (ret) {
1611 goto error_def;
1612 }
1613
1614 ret = prepare_mmap_stream_definition(td, file_stream);
1615 if (ret)
1616 goto error_index;
1617
1618 /* Add stream file to stream class */
1619 g_ptr_array_add(file_stream->parent.stream_class->streams,
1620 &file_stream->parent);
1621 return 0;
1622
1623 error_index:
1624 if (file_stream->parent.trace_packet_header)
1625 definition_unref(&file_stream->parent.trace_packet_header->p);
1626 error_def:
1627 g_free(file_stream);
1628 return ret;
1629 }
1630
1631 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1632 struct mmap_stream_list *mmap_list,
1633 void (*packet_seek)(struct stream_pos *pos, size_t index,
1634 int whence),
1635 FILE *metadata_fp)
1636 {
1637 int ret;
1638 struct mmap_stream *mmap_info;
1639
1640 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1641 if (ret) {
1642 goto error;
1643 }
1644
1645 /*
1646 * for each stream, try to open, check magic number, and get the
1647 * stream ID to add to the right location in the stream array.
1648 */
1649 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1650 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
1651 if (ret) {
1652 fprintf(stderr, "[error] Open file mmap stream error.\n");
1653 goto error;
1654 }
1655 }
1656
1657 return 0;
1658
1659 error:
1660 return ret;
1661 }
1662
1663 static
1664 struct trace_descriptor *ctf_open_mmap_trace(
1665 struct mmap_stream_list *mmap_list,
1666 void (*packet_seek)(struct stream_pos *pos, size_t index,
1667 int whence),
1668 FILE *metadata_fp)
1669 {
1670 struct ctf_trace *td;
1671 int ret;
1672
1673 if (!metadata_fp) {
1674 fprintf(stderr, "[error] No metadata file pointer associated, "
1675 "required for mmap parsing\n");
1676 goto error;
1677 }
1678 if (!packet_seek) {
1679 fprintf(stderr, "[error] packet_seek function undefined.\n");
1680 goto error;
1681 }
1682 td = g_new0(struct ctf_trace, 1);
1683 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
1684 if (ret)
1685 goto error_free;
1686
1687 return &td->parent;
1688
1689 error_free:
1690 g_free(td);
1691 error:
1692 return NULL;
1693 }
1694
1695 static
1696 void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1697 {
1698 ctf_fini_pos(&file_stream->pos);
1699 close(file_stream->pos.fd);
1700 }
1701
1702 static
1703 void ctf_close_trace(struct trace_descriptor *tdp)
1704 {
1705 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1706 int i;
1707
1708 if (td->streams) {
1709 for (i = 0; i < td->streams->len; i++) {
1710 struct ctf_stream_declaration *stream;
1711 int j;
1712
1713 stream = g_ptr_array_index(td->streams, i);
1714 if (!stream)
1715 continue;
1716 for (j = 0; j < stream->streams->len; j++) {
1717 struct ctf_file_stream *file_stream;
1718 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
1719 ctf_close_file_stream(file_stream);
1720 }
1721
1722 }
1723 g_ptr_array_free(td->streams, TRUE);
1724 }
1725
1726 if (td->event_declarations) {
1727 for (i = 0; i < td->event_declarations->len; i++) {
1728 struct bt_ctf_event_decl *event;
1729
1730 event = g_ptr_array_index(td->event_declarations, i);
1731 if (event->context_decl)
1732 g_ptr_array_free(event->context_decl, TRUE);
1733 if (event->fields_decl)
1734 g_ptr_array_free(event->fields_decl, TRUE);
1735 if (event->packet_header_decl)
1736 g_ptr_array_free(event->packet_header_decl, TRUE);
1737 if (event->event_context_decl)
1738 g_ptr_array_free(event->event_context_decl, TRUE);
1739 if (event->event_header_decl)
1740 g_ptr_array_free(event->event_header_decl, TRUE);
1741 if (event->packet_context_decl)
1742 g_ptr_array_free(event->packet_context_decl, TRUE);
1743 g_free(event);
1744 }
1745 g_ptr_array_free(td->event_declarations, TRUE);
1746 }
1747 closedir(td->dir);
1748 g_free(td);
1749 }
1750
1751 static
1752 void ctf_set_context(struct trace_descriptor *descriptor,
1753 struct bt_context *ctx)
1754 {
1755 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1756 parent);
1757
1758 td->ctx = ctx;
1759 }
1760
1761 static
1762 void ctf_set_handle(struct trace_descriptor *descriptor,
1763 struct bt_trace_handle *handle)
1764 {
1765 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1766 parent);
1767
1768 td->handle = handle;
1769 }
1770
1771 void __attribute__((constructor)) ctf_init(void)
1772 {
1773 int ret;
1774
1775 ctf_format.name = g_quark_from_static_string("ctf");
1776 ret = bt_register_format(&ctf_format);
1777 assert(!ret);
1778 }
1779
1780 /* TODO: finalize */
This page took 0.126158 seconds and 4 git commands to generate.