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