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