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