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