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