Handle packets containing only a header
[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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <babeltrace/trace-handle-internal.h>
35 #include <babeltrace/context-internal.h>
36 #include <babeltrace/compat/uuid.h>
37 #include <babeltrace/endian.h>
38 #include <babeltrace/ctf/ctf-index.h>
39 #include <inttypes.h>
40 #include <stdio.h>
41 #include <sys/mman.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <dirent.h>
47 #include <glib.h>
48 #include <unistd.h>
49 #include <stdlib.h>
50
51 #include "metadata/ctf-scanner.h"
52 #include "metadata/ctf-parser.h"
53 #include "metadata/ctf-ast.h"
54 #include "events-private.h"
55 #include <babeltrace/compat/memstream.h>
56
57 #define LOG2_CHAR_BIT 3
58
59 /*
60 * Length of first attempt at mapping a packet header, in bits.
61 */
62 #define DEFAULT_HEADER_LEN (getpagesize() * CHAR_BIT)
63
64 /*
65 * Lenght of packet to write, in bits.
66 */
67 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
68
69 #ifndef min
70 #define min(a, b) (((a) < (b)) ? (a) : (b))
71 #endif
72
73 #define NSEC_PER_SEC 1000000000ULL
74
75 #define INDEX_PATH "./index/%s.idx"
76
77 int opt_clock_cycles,
78 opt_clock_seconds,
79 opt_clock_date,
80 opt_clock_gmt;
81
82 uint64_t opt_clock_offset;
83 uint64_t opt_clock_offset_ns;
84
85 extern int yydebug;
86
87 static
88 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
89 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
90 int whence),
91 FILE *metadata_fp);
92 static
93 struct bt_trace_descriptor *ctf_open_mmap_trace(
94 struct bt_mmap_stream_list *mmap_list,
95 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
96 int whence),
97 FILE *metadata_fp);
98 static
99 void ctf_set_context(struct bt_trace_descriptor *descriptor,
100 struct bt_context *ctx);
101 static
102 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
103 struct bt_trace_handle *handle);
104
105 static
106 int ctf_close_trace(struct bt_trace_descriptor *descriptor);
107 static
108 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
109 struct bt_trace_handle *handle, enum bt_clock_type type);
110 static
111 uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
112 struct bt_trace_handle *handle, enum bt_clock_type type);
113 static
114 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp);
115
116 static
117 rw_dispatch read_dispatch_table[] = {
118 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
119 [ CTF_TYPE_FLOAT ] = ctf_float_read,
120 [ CTF_TYPE_ENUM ] = ctf_enum_read,
121 [ CTF_TYPE_STRING ] = ctf_string_read,
122 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
123 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
124 [ CTF_TYPE_ARRAY ] = ctf_array_read,
125 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
126 };
127
128 static
129 rw_dispatch write_dispatch_table[] = {
130 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
131 [ CTF_TYPE_FLOAT ] = ctf_float_write,
132 [ CTF_TYPE_ENUM ] = ctf_enum_write,
133 [ CTF_TYPE_STRING ] = ctf_string_write,
134 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
135 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
136 [ CTF_TYPE_ARRAY ] = ctf_array_write,
137 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
138 };
139
140 static
141 struct bt_format ctf_format = {
142 .open_trace = ctf_open_trace,
143 .open_mmap_trace = ctf_open_mmap_trace,
144 .close_trace = ctf_close_trace,
145 .set_context = ctf_set_context,
146 .set_handle = ctf_set_handle,
147 .timestamp_begin = ctf_timestamp_begin,
148 .timestamp_end = ctf_timestamp_end,
149 .convert_index_timestamp = ctf_convert_index_timestamp,
150 };
151
152 static
153 uint64_t ctf_timestamp_begin(struct bt_trace_descriptor *descriptor,
154 struct bt_trace_handle *handle, enum bt_clock_type type)
155 {
156 struct ctf_trace *tin;
157 uint64_t begin = ULLONG_MAX;
158 int i, j;
159
160 tin = container_of(descriptor, struct ctf_trace, parent);
161
162 if (!tin)
163 goto error;
164
165 /* for each stream_class */
166 for (i = 0; i < tin->streams->len; i++) {
167 struct ctf_stream_declaration *stream_class;
168
169 stream_class = g_ptr_array_index(tin->streams, i);
170 if (!stream_class)
171 continue;
172 /* for each file_stream */
173 for (j = 0; j < stream_class->streams->len; j++) {
174 struct ctf_stream_definition *stream;
175 struct ctf_file_stream *cfs;
176 struct ctf_stream_pos *stream_pos;
177 struct packet_index *index;
178
179 stream = g_ptr_array_index(stream_class->streams, j);
180 cfs = container_of(stream, struct ctf_file_stream,
181 parent);
182 stream_pos = &cfs->pos;
183
184 if (!stream_pos->packet_real_index)
185 goto error;
186
187 if (stream_pos->packet_real_index->len <= 0)
188 continue;
189
190 if (type == BT_CLOCK_REAL) {
191 index = &g_array_index(stream_pos->packet_real_index,
192 struct packet_index,
193 stream_pos->packet_real_index->len - 1);
194 } else if (type == BT_CLOCK_CYCLES) {
195 index = &g_array_index(stream_pos->packet_cycles_index,
196 struct packet_index,
197 stream_pos->packet_real_index->len - 1);
198
199 } else {
200 goto error;
201 }
202 if (index->timestamp_begin < begin)
203 begin = index->timestamp_begin;
204 }
205 }
206
207 return begin;
208
209 error:
210 return -1ULL;
211 }
212
213 static
214 uint64_t ctf_timestamp_end(struct bt_trace_descriptor *descriptor,
215 struct bt_trace_handle *handle, enum bt_clock_type type)
216 {
217 struct ctf_trace *tin;
218 uint64_t end = 0;
219 int i, j;
220
221 tin = container_of(descriptor, struct ctf_trace, parent);
222
223 if (!tin)
224 goto error;
225
226 /* for each stream_class */
227 for (i = 0; i < tin->streams->len; i++) {
228 struct ctf_stream_declaration *stream_class;
229
230 stream_class = g_ptr_array_index(tin->streams, i);
231 if (!stream_class)
232 continue;
233 /* for each file_stream */
234 for (j = 0; j < stream_class->streams->len; j++) {
235 struct ctf_stream_definition *stream;
236 struct ctf_file_stream *cfs;
237 struct ctf_stream_pos *stream_pos;
238 struct packet_index *index;
239
240 stream = g_ptr_array_index(stream_class->streams, j);
241 cfs = container_of(stream, struct ctf_file_stream,
242 parent);
243 stream_pos = &cfs->pos;
244
245 if (!stream_pos->packet_real_index)
246 goto error;
247
248 if (stream_pos->packet_real_index->len <= 0)
249 continue;
250
251 if (type == BT_CLOCK_REAL) {
252 index = &g_array_index(stream_pos->packet_real_index,
253 struct packet_index,
254 stream_pos->packet_real_index->len - 1);
255 } else if (type == BT_CLOCK_CYCLES) {
256 index = &g_array_index(stream_pos->packet_cycles_index,
257 struct packet_index,
258 stream_pos->packet_real_index->len - 1);
259
260 } else {
261 goto error;
262 }
263 if (index->timestamp_end > end)
264 end = index->timestamp_end;
265 }
266 }
267
268 return end;
269
270 error:
271 return -1ULL;
272 }
273
274 /*
275 * Update stream current timestamp
276 */
277 static
278 void ctf_update_timestamp(struct ctf_stream_definition *stream,
279 struct definition_integer *integer_definition)
280 {
281 struct declaration_integer *integer_declaration =
282 integer_definition->declaration;
283 uint64_t oldval, newval, updateval;
284
285 if (unlikely(integer_declaration->len == 64)) {
286 stream->prev_cycles_timestamp = stream->cycles_timestamp;
287 stream->cycles_timestamp = integer_definition->value._unsigned;
288 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
289 stream->prev_cycles_timestamp);
290 stream->real_timestamp = ctf_get_real_timestamp(stream,
291 stream->cycles_timestamp);
292 return;
293 }
294 /* keep low bits */
295 oldval = stream->cycles_timestamp;
296 oldval &= (1ULL << integer_declaration->len) - 1;
297 newval = integer_definition->value._unsigned;
298 /* Test for overflow by comparing low bits */
299 if (newval < oldval)
300 newval += 1ULL << integer_declaration->len;
301 /* updateval contains old high bits, and new low bits (sum) */
302 updateval = stream->cycles_timestamp;
303 updateval &= ~((1ULL << integer_declaration->len) - 1);
304 updateval += newval;
305 stream->prev_cycles_timestamp = stream->cycles_timestamp;
306 stream->cycles_timestamp = updateval;
307
308 /* convert to real timestamp */
309 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
310 stream->prev_cycles_timestamp);
311 stream->real_timestamp = ctf_get_real_timestamp(stream,
312 stream->cycles_timestamp);
313 }
314
315 /*
316 * Print timestamp, rescaling clock frequency to nanoseconds and
317 * applying offsets as needed (unix time).
318 */
319 static
320 void ctf_print_timestamp_real(FILE *fp,
321 struct ctf_stream_definition *stream,
322 uint64_t timestamp)
323 {
324 uint64_t ts_sec = 0, ts_nsec;
325
326 ts_nsec = timestamp;
327
328 /* Add command-line offset in ns*/
329 ts_nsec += opt_clock_offset_ns;
330
331 /* Add command-line offset */
332 ts_sec += opt_clock_offset;
333
334 ts_sec += ts_nsec / NSEC_PER_SEC;
335 ts_nsec = ts_nsec % NSEC_PER_SEC;
336
337 if (!opt_clock_seconds) {
338 struct tm tm;
339 time_t time_s = (time_t) ts_sec;
340
341 if (!opt_clock_gmt) {
342 struct tm *res;
343
344 res = localtime_r(&time_s, &tm);
345 if (!res) {
346 fprintf(stderr, "[warning] Unable to get localtime.\n");
347 goto seconds;
348 }
349 } else {
350 struct tm *res;
351
352 res = gmtime_r(&time_s, &tm);
353 if (!res) {
354 fprintf(stderr, "[warning] Unable to get gmtime.\n");
355 goto seconds;
356 }
357 }
358 if (opt_clock_date) {
359 char timestr[26];
360 size_t res;
361
362 /* Print date and time */
363 res = strftime(timestr, sizeof(timestr),
364 "%F ", &tm);
365 if (!res) {
366 fprintf(stderr, "[warning] Unable to print ascii time.\n");
367 goto seconds;
368 }
369 fprintf(fp, "%s", timestr);
370 }
371 /* Print time in HH:MM:SS.ns */
372 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
373 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
374 goto end;
375 }
376 seconds:
377 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
378 ts_sec, ts_nsec);
379
380 end:
381 return;
382 }
383
384 /*
385 * Print timestamp, in cycles
386 */
387 static
388 void ctf_print_timestamp_cycles(FILE *fp,
389 struct ctf_stream_definition *stream,
390 uint64_t timestamp)
391 {
392 fprintf(fp, "%020" PRIu64, timestamp);
393 }
394
395 void ctf_print_timestamp(FILE *fp,
396 struct ctf_stream_definition *stream,
397 uint64_t timestamp)
398 {
399 if (opt_clock_cycles) {
400 ctf_print_timestamp_cycles(fp, stream, timestamp);
401 } else {
402 ctf_print_timestamp_real(fp, stream, timestamp);
403 }
404 }
405
406 static
407 void print_uuid(FILE *fp, unsigned char *uuid)
408 {
409 int i;
410
411 for (i = 0; i < BABELTRACE_UUID_LEN; i++)
412 fprintf(fp, "%x", (unsigned int) uuid[i]);
413 }
414
415 void ctf_print_discarded(FILE *fp, struct ctf_stream_definition *stream,
416 int end_stream)
417 {
418 fprintf(fp, "[warning] Tracer discarded %" PRIu64 " events %sbetween [",
419 stream->events_discarded,
420 end_stream ? "at end of stream " : "");
421 if (opt_clock_cycles) {
422 ctf_print_timestamp(fp, stream,
423 stream->prev_cycles_timestamp);
424 fprintf(fp, "] and [");
425 ctf_print_timestamp(fp, stream,
426 stream->prev_cycles_timestamp_end);
427 } else {
428 ctf_print_timestamp(fp, stream,
429 stream->prev_real_timestamp);
430 fprintf(fp, "] and [");
431 ctf_print_timestamp(fp, stream,
432 stream->prev_real_timestamp_end);
433 }
434 fprintf(fp, "] in trace UUID ");
435 print_uuid(fp, stream->stream_class->trace->uuid);
436 if (stream->stream_class->trace->parent.path[0])
437 fprintf(fp, ", at path: \"%s\"",
438 stream->stream_class->trace->parent.path);
439
440 fprintf(fp, ", within stream id %" PRIu64, stream->stream_id);
441 if (stream->path[0])
442 fprintf(fp, ", at relative path: \"%s\"", stream->path);
443 fprintf(fp, ". ");
444 fprintf(fp, "You should consider recording a new trace with larger "
445 "buffers or with fewer events enabled.\n");
446 fflush(fp);
447 }
448
449 static
450 int ctf_read_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
451 {
452 struct ctf_stream_pos *pos =
453 container_of(ppos, struct ctf_stream_pos, parent);
454 struct ctf_stream_declaration *stream_class = stream->stream_class;
455 struct ctf_event_definition *event;
456 uint64_t id = 0;
457 int ret;
458
459 /* We need to check for EOF here for empty files. */
460 if (unlikely(pos->offset == EOF))
461 return EOF;
462
463 ctf_pos_get_event(pos);
464
465 /* save the current position as a restore point */
466 pos->last_offset = pos->offset;
467
468 /*
469 * This is the EOF check after we've advanced the position in
470 * ctf_pos_get_event.
471 */
472 if (unlikely(pos->offset == EOF))
473 return EOF;
474
475 if (pos->content_size == 0) {
476 /* Stream is inactive for now (live reading). */
477 return EAGAIN;
478 }
479 /* Packet only contains headers */
480 if (pos->offset == pos->content_size)
481 return EAGAIN;
482
483 assert(pos->offset < pos->content_size);
484
485 /* Read event header */
486 if (likely(stream->stream_event_header)) {
487 struct definition_integer *integer_definition;
488 struct bt_definition *variant;
489
490 ret = generic_rw(ppos, &stream->stream_event_header->p);
491 if (unlikely(ret))
492 goto error;
493 /* lookup event id */
494 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
495 if (integer_definition) {
496 id = integer_definition->value._unsigned;
497 } else {
498 struct definition_enum *enum_definition;
499
500 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
501 if (enum_definition) {
502 id = enum_definition->integer->value._unsigned;
503 }
504 }
505
506 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
507 if (variant) {
508 integer_definition = bt_lookup_integer(variant, "id", FALSE);
509 if (integer_definition) {
510 id = integer_definition->value._unsigned;
511 }
512 }
513 stream->event_id = id;
514
515 /* lookup timestamp */
516 stream->has_timestamp = 0;
517 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
518 if (integer_definition) {
519 ctf_update_timestamp(stream, integer_definition);
520 stream->has_timestamp = 1;
521 } else {
522 if (variant) {
523 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
524 if (integer_definition) {
525 ctf_update_timestamp(stream, integer_definition);
526 stream->has_timestamp = 1;
527 }
528 }
529 }
530 }
531
532 /* Read stream-declared event context */
533 if (stream->stream_event_context) {
534 ret = generic_rw(ppos, &stream->stream_event_context->p);
535 if (ret)
536 goto error;
537 }
538
539 if (unlikely(id >= stream_class->events_by_id->len)) {
540 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
541 return -EINVAL;
542 }
543 event = g_ptr_array_index(stream->events_by_id, id);
544 if (unlikely(!event)) {
545 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
546 return -EINVAL;
547 }
548
549 /* Read event-declared event context */
550 if (event->event_context) {
551 ret = generic_rw(ppos, &event->event_context->p);
552 if (ret)
553 goto error;
554 }
555
556 /* Read event payload */
557 if (likely(event->event_fields)) {
558 ret = generic_rw(ppos, &event->event_fields->p);
559 if (ret)
560 goto error;
561 }
562
563 if (pos->last_offset == pos->offset) {
564 fprintf(stderr, "[error] Invalid 0 byte event encountered.\n");
565 return -EINVAL;
566 }
567
568 return 0;
569
570 error:
571 fprintf(stderr, "[error] Unexpected end of packet. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
572 return ret;
573 }
574
575 static
576 int ctf_write_event(struct bt_stream_pos *pos, struct ctf_stream_definition *stream)
577 {
578 struct ctf_stream_declaration *stream_class = stream->stream_class;
579 struct ctf_event_definition *event;
580 uint64_t id;
581 int ret;
582
583 id = stream->event_id;
584
585 /* print event header */
586 if (likely(stream->stream_event_header)) {
587 ret = generic_rw(pos, &stream->stream_event_header->p);
588 if (ret)
589 goto error;
590 }
591
592 /* print stream-declared event context */
593 if (stream->stream_event_context) {
594 ret = generic_rw(pos, &stream->stream_event_context->p);
595 if (ret)
596 goto error;
597 }
598
599 if (unlikely(id >= stream_class->events_by_id->len)) {
600 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
601 return -EINVAL;
602 }
603 event = g_ptr_array_index(stream->events_by_id, id);
604 if (unlikely(!event)) {
605 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
606 return -EINVAL;
607 }
608
609 /* print event-declared event context */
610 if (event->event_context) {
611 ret = generic_rw(pos, &event->event_context->p);
612 if (ret)
613 goto error;
614 }
615
616 /* Read and print event payload */
617 if (likely(event->event_fields)) {
618 ret = generic_rw(pos, &event->event_fields->p);
619 if (ret)
620 goto error;
621 }
622
623 return 0;
624
625 error:
626 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
627 return ret;
628 }
629
630 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
631 int fd, int open_flags)
632 {
633 pos->fd = fd;
634 if (fd >= 0) {
635 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
636 sizeof(struct packet_index));
637 pos->packet_real_index = g_array_new(FALSE, TRUE,
638 sizeof(struct packet_index));
639 } else {
640 pos->packet_cycles_index = NULL;
641 pos->packet_real_index = NULL;
642 }
643 switch (open_flags & O_ACCMODE) {
644 case O_RDONLY:
645 pos->prot = PROT_READ;
646 pos->flags = MAP_PRIVATE;
647 pos->parent.rw_table = read_dispatch_table;
648 pos->parent.event_cb = ctf_read_event;
649 pos->parent.trace = trace;
650 break;
651 case O_RDWR:
652 pos->prot = PROT_WRITE; /* Write has priority */
653 pos->flags = MAP_SHARED;
654 pos->parent.rw_table = write_dispatch_table;
655 pos->parent.event_cb = ctf_write_event;
656 pos->parent.trace = trace;
657 if (fd >= 0)
658 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
659 break;
660 default:
661 assert(0);
662 }
663 return 0;
664 }
665
666 int ctf_fini_pos(struct ctf_stream_pos *pos)
667 {
668 if (pos->prot == PROT_WRITE && pos->content_size_loc)
669 *pos->content_size_loc = pos->offset;
670 if (pos->base_mma) {
671 int ret;
672
673 /* unmap old base */
674 ret = munmap_align(pos->base_mma);
675 if (ret) {
676 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
677 strerror(errno));
678 return -1;
679 }
680 }
681 if (pos->packet_cycles_index)
682 (void) g_array_free(pos->packet_cycles_index, TRUE);
683 if (pos->packet_real_index)
684 (void) g_array_free(pos->packet_real_index, TRUE);
685 return 0;
686 }
687
688 /*
689 * for SEEK_CUR: go to next packet.
690 * for SEEK_SET: go to packet numer (index).
691 */
692 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
693 {
694 struct ctf_stream_pos *pos =
695 container_of(stream_pos, struct ctf_stream_pos, parent);
696 struct ctf_file_stream *file_stream =
697 container_of(pos, struct ctf_file_stream, pos);
698 int ret;
699 off_t off;
700 struct packet_index *packet_index;
701
702 switch (whence) {
703 case SEEK_CUR:
704 case SEEK_SET: /* Fall-through */
705 break; /* OK */
706 default:
707 assert(0);
708 }
709
710 if (pos->prot == PROT_WRITE && pos->content_size_loc)
711 *pos->content_size_loc = pos->offset;
712
713 if (pos->base_mma) {
714 /* unmap old base */
715 ret = munmap_align(pos->base_mma);
716 if (ret) {
717 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
718 strerror(errno));
719 assert(0);
720 }
721 pos->base_mma = NULL;
722 }
723
724 /*
725 * The caller should never ask for ctf_move_pos across packets,
726 * except to get exactly at the beginning of the next packet.
727 */
728 if (pos->prot == PROT_WRITE) {
729 switch (whence) {
730 case SEEK_CUR:
731 /* The writer will add padding */
732 pos->mmap_offset += pos->packet_size / CHAR_BIT;
733 break;
734 case SEEK_SET:
735 assert(index == 0); /* only seek supported for now */
736 pos->cur_index = 0;
737 break;
738 default:
739 assert(0);
740 }
741 pos->content_size = -1U; /* Unknown at this point */
742 pos->packet_size = WRITE_PACKET_LEN;
743 off = posix_fallocate(pos->fd, pos->mmap_offset,
744 pos->packet_size / CHAR_BIT);
745 assert(off >= 0);
746 pos->offset = 0;
747 } else {
748 read_next_packet:
749 switch (whence) {
750 case SEEK_CUR:
751 {
752 uint64_t events_discarded_diff;
753
754 if (pos->offset == EOF) {
755 return;
756 }
757 assert(pos->cur_index < pos->packet_cycles_index->len);
758 assert(pos->cur_index < pos->packet_real_index->len);
759
760 /* For printing discarded event count */
761 packet_index = &g_array_index(pos->packet_cycles_index,
762 struct packet_index, pos->cur_index);
763 file_stream->parent.prev_cycles_timestamp_end =
764 packet_index->timestamp_end;
765 file_stream->parent.prev_cycles_timestamp =
766 packet_index->timestamp_begin;
767
768 packet_index = &g_array_index(pos->packet_real_index,
769 struct packet_index, pos->cur_index);
770 file_stream->parent.prev_real_timestamp_end =
771 packet_index->timestamp_end;
772 file_stream->parent.prev_real_timestamp =
773 packet_index->timestamp_begin;
774
775 events_discarded_diff = packet_index->events_discarded;
776 if (pos->cur_index > 0) {
777 packet_index = &g_array_index(pos->packet_real_index,
778 struct packet_index,
779 pos->cur_index - 1);
780 events_discarded_diff -= packet_index->events_discarded;
781 /*
782 * Deal with 32-bit wrap-around if the
783 * tracer provided a 32-bit field.
784 */
785 if (packet_index->events_discarded_len == 32) {
786 events_discarded_diff = (uint32_t) events_discarded_diff;
787 }
788 }
789 file_stream->parent.events_discarded = events_discarded_diff;
790 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
791 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
792 /* The reader will expect us to skip padding */
793 ++pos->cur_index;
794 break;
795 }
796 case SEEK_SET:
797 if (index >= pos->packet_cycles_index->len) {
798 pos->offset = EOF;
799 return;
800 }
801 packet_index = &g_array_index(pos->packet_cycles_index,
802 struct packet_index, index);
803 pos->last_events_discarded = packet_index->events_discarded;
804 pos->cur_index = index;
805 file_stream->parent.prev_real_timestamp = 0;
806 file_stream->parent.prev_real_timestamp_end = 0;
807 file_stream->parent.prev_cycles_timestamp = 0;
808 file_stream->parent.prev_cycles_timestamp_end = 0;
809 break;
810 default:
811 assert(0);
812 }
813 if (pos->cur_index >= pos->packet_real_index->len) {
814 /*
815 * We need to check if we are in trace read or
816 * called from packet indexing. In this last
817 * case, the collection is not there, so we
818 * cannot print the timestamps.
819 */
820 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
821 /*
822 * When a stream reaches the end of the
823 * file, we need to show the number of
824 * events discarded ourselves, because
825 * there is no next event scheduled to
826 * be printed in the output.
827 */
828 if (file_stream->parent.events_discarded) {
829 fflush(stdout);
830 ctf_print_discarded(stderr,
831 &file_stream->parent,
832 1);
833 file_stream->parent.events_discarded = 0;
834 }
835 }
836 pos->offset = EOF;
837 return;
838 }
839 packet_index = &g_array_index(pos->packet_cycles_index,
840 struct packet_index,
841 pos->cur_index);
842 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
843
844 packet_index = &g_array_index(pos->packet_real_index,
845 struct packet_index,
846 pos->cur_index);
847 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
848 pos->mmap_offset = packet_index->offset;
849
850 /* Lookup context/packet size in index */
851 pos->content_size = packet_index->content_size;
852 pos->packet_size = packet_index->packet_size;
853 if (packet_index->data_offset < packet_index->content_size) {
854 pos->offset = 0; /* will read headers */
855 } else if (packet_index->data_offset == packet_index->content_size) {
856 /* empty packet */
857 pos->offset = packet_index->data_offset;
858 whence = SEEK_CUR;
859 goto read_next_packet;
860 } else {
861 pos->offset = EOF;
862 return;
863 }
864 }
865 /* map new base. Need mapping length from header. */
866 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
867 pos->flags, pos->fd, pos->mmap_offset);
868 if (pos->base_mma == MAP_FAILED) {
869 fprintf(stderr, "[error] mmap error %s.\n",
870 strerror(errno));
871 assert(0);
872 }
873
874 /* update trace_packet_header and stream_packet_context */
875 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
876 /* Read packet header */
877 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
878 assert(!ret);
879 }
880 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
881 /* Read packet context */
882 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
883 assert(!ret);
884 }
885 }
886
887 static
888 int packet_metadata(struct ctf_trace *td, FILE *fp)
889 {
890 uint32_t magic;
891 size_t len;
892 int ret = 0;
893
894 len = fread(&magic, sizeof(magic), 1, fp);
895 if (len != 1) {
896 goto end;
897 }
898 if (magic == TSDL_MAGIC) {
899 ret = 1;
900 td->byte_order = BYTE_ORDER;
901 CTF_TRACE_SET_FIELD(td, byte_order);
902 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
903 ret = 1;
904 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
905 LITTLE_ENDIAN : BIG_ENDIAN;
906 CTF_TRACE_SET_FIELD(td, byte_order);
907 }
908 end:
909 rewind(fp);
910 return ret;
911 }
912
913 /*
914 * Returns 0 on success, -1 on error.
915 */
916 static
917 int check_version(unsigned int major, unsigned int minor)
918 {
919 switch (major) {
920 case 1:
921 switch (minor) {
922 case 8:
923 return 0;
924 default:
925 goto warning;
926 }
927 default:
928 goto warning;
929
930 }
931
932 /* eventually return an error instead of warning */
933 warning:
934 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
935 major, minor);
936 return 0;
937 }
938
939 static
940 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
941 FILE *out)
942 {
943 struct metadata_packet_header header;
944 size_t readlen, writelen, toread;
945 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
946 int ret = 0;
947
948 readlen = fread(&header, header_sizeof(header), 1, in);
949 if (readlen < 1)
950 return -EINVAL;
951
952 if (td->byte_order != BYTE_ORDER) {
953 header.magic = GUINT32_SWAP_LE_BE(header.magic);
954 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
955 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
956 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
957 }
958 if (header.checksum)
959 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
960 if (header.compression_scheme) {
961 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
962 header.compression_scheme);
963 return -EINVAL;
964 }
965 if (header.encryption_scheme) {
966 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
967 header.encryption_scheme);
968 return -EINVAL;
969 }
970 if (header.checksum_scheme) {
971 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
972 header.checksum_scheme);
973 return -EINVAL;
974 }
975 if (check_version(header.major, header.minor) < 0)
976 return -EINVAL;
977 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
978 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
979 CTF_TRACE_SET_FIELD(td, uuid);
980 } else {
981 if (babeltrace_uuid_compare(header.uuid, td->uuid))
982 return -EINVAL;
983 }
984
985 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
986 return -EINVAL;
987
988 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
989
990 for (;;) {
991 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
992 if (ferror(in)) {
993 ret = -EINVAL;
994 break;
995 }
996 if (babeltrace_debug) {
997 buf[readlen] = '\0';
998 fprintf(stderr, "[debug] metadata packet read: %s\n",
999 buf);
1000 }
1001
1002 writelen = fwrite(buf, sizeof(char), readlen, out);
1003 if (writelen < readlen) {
1004 ret = -EIO;
1005 break;
1006 }
1007 if (ferror(out)) {
1008 ret = -EINVAL;
1009 break;
1010 }
1011 toread -= readlen;
1012 if (!toread) {
1013 ret = 0; /* continue reading next packet */
1014 goto read_padding;
1015 }
1016 }
1017 return ret;
1018
1019 read_padding:
1020 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1021 ret = fseek(in, toread, SEEK_CUR);
1022 if (ret < 0) {
1023 fprintf(stderr, "[warning] Missing padding at end of file\n");
1024 ret = 0;
1025 }
1026 return ret;
1027 }
1028
1029 static
1030 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1031 char **buf)
1032 {
1033 FILE *in, *out;
1034 size_t size, buflen;
1035 int ret;
1036
1037 in = *fp;
1038 /*
1039 * Using strlen on *buf instead of size of open_memstream
1040 * because its size includes garbage at the end (after final
1041 * \0). This is the allocated size, not the actual string size.
1042 */
1043 out = babeltrace_open_memstream(buf, &size);
1044 if (out == NULL) {
1045 perror("Metadata open_memstream");
1046 return -errno;
1047 }
1048 for (;;) {
1049 ret = ctf_open_trace_metadata_packet_read(td, in, out);
1050 if (ret) {
1051 break;
1052 }
1053 if (feof(in)) {
1054 ret = 0;
1055 break;
1056 }
1057 }
1058 /* close to flush the buffer */
1059 ret = babeltrace_close_memstream(buf, &size, out);
1060 if (ret < 0) {
1061 int closeret;
1062
1063 perror("babeltrace_flush_memstream");
1064 ret = -errno;
1065 closeret = fclose(in);
1066 if (closeret) {
1067 perror("Error in fclose");
1068 }
1069 return ret;
1070 }
1071 ret = fclose(in);
1072 if (ret) {
1073 perror("Error in fclose");
1074 }
1075 /* open for reading */
1076 buflen = strlen(*buf);
1077 if (!buflen) {
1078 *fp = NULL;
1079 return -ENOENT;
1080 }
1081 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1082 if (!*fp) {
1083 perror("Metadata fmemopen");
1084 return -errno;
1085 }
1086 return 0;
1087 }
1088
1089 static
1090 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1091 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1092 int whence), FILE *metadata_fp)
1093 {
1094 struct ctf_scanner *scanner;
1095 struct ctf_file_stream *metadata_stream;
1096 FILE *fp;
1097 char *buf = NULL;
1098 int ret = 0, closeret;
1099
1100 metadata_stream = g_new0(struct ctf_file_stream, 1);
1101 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1102
1103 if (packet_seek) {
1104 metadata_stream->pos.packet_seek = packet_seek;
1105 } else {
1106 fprintf(stderr, "[error] packet_seek function undefined.\n");
1107 ret = -1;
1108 goto end_free;
1109 }
1110
1111 if (metadata_fp) {
1112 fp = metadata_fp;
1113 metadata_stream->pos.fd = -1;
1114 } else {
1115 td->metadata = &metadata_stream->parent;
1116 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1117 if (metadata_stream->pos.fd < 0) {
1118 fprintf(stderr, "Unable to open metadata.\n");
1119 ret = -1;
1120 goto end_free;
1121 }
1122
1123 fp = fdopen(metadata_stream->pos.fd, "r");
1124 if (!fp) {
1125 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1126 perror("Metadata stream open");
1127 ret = -errno;
1128 goto end_stream;
1129 }
1130 /* fd now belongs to fp */
1131 metadata_stream->pos.fd = -1;
1132 }
1133 if (babeltrace_debug)
1134 yydebug = 1;
1135
1136 if (packet_metadata(td, fp)) {
1137 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1138 if (ret) {
1139 /* Warn about empty metadata */
1140 fprintf(stderr, "[warning] Empty metadata.\n");
1141 goto end_packet_read;
1142 }
1143 td->metadata_string = buf;
1144 td->metadata_packetized = 1;
1145 } else {
1146 unsigned int major, minor;
1147 ssize_t nr_items;
1148
1149 td->byte_order = BYTE_ORDER;
1150
1151 /* Check text-only metadata header and version */
1152 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1153 if (nr_items < 2)
1154 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1155 if (check_version(major, minor) < 0) {
1156 ret = -EINVAL;
1157 goto end_packet_read;
1158 }
1159 rewind(fp);
1160 }
1161
1162 scanner = ctf_scanner_alloc(fp);
1163 if (!scanner) {
1164 fprintf(stderr, "[error] Error allocating scanner\n");
1165 ret = -ENOMEM;
1166 goto end_scanner_alloc;
1167 }
1168 ret = ctf_scanner_append_ast(scanner);
1169 if (ret) {
1170 fprintf(stderr, "[error] Error creating AST\n");
1171 goto end;
1172 }
1173
1174 if (babeltrace_debug) {
1175 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1176 if (ret) {
1177 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1178 goto end;
1179 }
1180 }
1181
1182 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1183 if (ret) {
1184 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1185 goto end;
1186 }
1187 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1188 td, td->byte_order);
1189 if (ret) {
1190 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1191 goto end;
1192 }
1193 end:
1194 ctf_scanner_free(scanner);
1195 end_scanner_alloc:
1196 end_packet_read:
1197 if (fp) {
1198 closeret = fclose(fp);
1199 if (closeret) {
1200 perror("Error on fclose");
1201 }
1202 }
1203 end_stream:
1204 if (metadata_stream->pos.fd >= 0) {
1205 closeret = close(metadata_stream->pos.fd);
1206 if (closeret) {
1207 perror("Error on metadata stream fd close");
1208 }
1209 }
1210 end_free:
1211 if (ret)
1212 g_free(metadata_stream);
1213 return ret;
1214 }
1215
1216 static
1217 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1218 struct ctf_stream_definition *stream,
1219 struct ctf_event_declaration *event)
1220 {
1221 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1222
1223 if (event->context_decl) {
1224 struct bt_definition *definition =
1225 event->context_decl->p.definition_new(&event->context_decl->p,
1226 stream->parent_def_scope, 0, 0, "event.context");
1227 if (!definition) {
1228 goto error;
1229 }
1230 stream_event->event_context = container_of(definition,
1231 struct definition_struct, p);
1232 stream->parent_def_scope = stream_event->event_context->p.scope;
1233 }
1234 if (event->fields_decl) {
1235 struct bt_definition *definition =
1236 event->fields_decl->p.definition_new(&event->fields_decl->p,
1237 stream->parent_def_scope, 0, 0, "event.fields");
1238 if (!definition) {
1239 goto error;
1240 }
1241 stream_event->event_fields = container_of(definition,
1242 struct definition_struct, p);
1243 stream->parent_def_scope = stream_event->event_fields->p.scope;
1244 }
1245 stream_event->stream = stream;
1246 return stream_event;
1247
1248 error:
1249 if (stream_event->event_fields)
1250 bt_definition_unref(&stream_event->event_fields->p);
1251 if (stream_event->event_context)
1252 bt_definition_unref(&stream_event->event_context->p);
1253 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1254 g_quark_to_string(event->name));
1255 return NULL;
1256 }
1257
1258 static
1259 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1260 {
1261 struct ctf_stream_declaration *stream_class;
1262 int ret;
1263 int i;
1264
1265 if (stream->stream_definitions_created)
1266 return 0;
1267
1268 stream_class = stream->stream_class;
1269
1270 if (stream_class->packet_context_decl) {
1271 struct bt_definition *definition =
1272 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1273 stream->parent_def_scope, 0, 0, "stream.packet.context");
1274 if (!definition) {
1275 ret = -EINVAL;
1276 goto error;
1277 }
1278 stream->stream_packet_context = container_of(definition,
1279 struct definition_struct, p);
1280 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1281 }
1282 if (stream_class->event_header_decl) {
1283 struct bt_definition *definition =
1284 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1285 stream->parent_def_scope, 0, 0, "stream.event.header");
1286 if (!definition) {
1287 ret = -EINVAL;
1288 goto error;
1289 }
1290 stream->stream_event_header =
1291 container_of(definition, struct definition_struct, p);
1292 stream->parent_def_scope = stream->stream_event_header->p.scope;
1293 }
1294 if (stream_class->event_context_decl) {
1295 struct bt_definition *definition =
1296 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1297 stream->parent_def_scope, 0, 0, "stream.event.context");
1298 if (!definition) {
1299 ret = -EINVAL;
1300 goto error;
1301 }
1302 stream->stream_event_context =
1303 container_of(definition, struct definition_struct, p);
1304 stream->parent_def_scope = stream->stream_event_context->p.scope;
1305 }
1306 stream->events_by_id = g_ptr_array_new();
1307 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1308 for (i = 0; i < stream->events_by_id->len; i++) {
1309 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1310 struct ctf_event_definition *stream_event;
1311
1312 if (!event)
1313 continue;
1314 stream_event = create_event_definitions(td, stream, event);
1315 if (!stream_event) {
1316 ret = -EINVAL;
1317 goto error_event;
1318 }
1319 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1320 }
1321 return 0;
1322
1323 error_event:
1324 for (i = 0; i < stream->events_by_id->len; i++) {
1325 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1326 if (stream_event)
1327 g_free(stream_event);
1328 }
1329 g_ptr_array_free(stream->events_by_id, TRUE);
1330 error:
1331 if (stream->stream_event_context)
1332 bt_definition_unref(&stream->stream_event_context->p);
1333 if (stream->stream_event_header)
1334 bt_definition_unref(&stream->stream_event_header->p);
1335 if (stream->stream_packet_context)
1336 bt_definition_unref(&stream->stream_packet_context->p);
1337 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1338 stream_class->stream_id, strerror(-ret));
1339 return ret;
1340 }
1341
1342 static
1343 int stream_assign_class(struct ctf_trace *td,
1344 struct ctf_file_stream *file_stream,
1345 uint64_t stream_id)
1346 {
1347 struct ctf_stream_declaration *stream;
1348 int ret;
1349
1350 file_stream->parent.stream_id = stream_id;
1351 if (stream_id >= td->streams->len) {
1352 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1353 return -EINVAL;
1354 }
1355 stream = g_ptr_array_index(td->streams, stream_id);
1356 if (!stream) {
1357 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1358 return -EINVAL;
1359 }
1360 file_stream->parent.stream_class = stream;
1361 ret = create_stream_definitions(td, &file_stream->parent);
1362 if (ret)
1363 return ret;
1364 return 0;
1365 }
1366
1367 static
1368 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1369 struct ctf_trace *td,
1370 struct ctf_file_stream *file_stream,
1371 size_t filesize)
1372 {
1373 struct packet_index packet_index;
1374 uint64_t stream_id = 0;
1375 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1376 int first_packet = 0;
1377 int len_index;
1378 int ret;
1379
1380 begin:
1381 if (!pos->mmap_offset) {
1382 first_packet = 1;
1383 }
1384
1385 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1386 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1387 }
1388
1389 if (pos->base_mma) {
1390 /* unmap old base */
1391 ret = munmap_align(pos->base_mma);
1392 if (ret) {
1393 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1394 strerror(errno));
1395 return ret;
1396 }
1397 pos->base_mma = NULL;
1398 }
1399 /* map new base. Need mapping length from header. */
1400 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1401 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1402 assert(pos->base_mma != MAP_FAILED);
1403 /*
1404 * Use current mapping size as temporary content and packet
1405 * size.
1406 */
1407 pos->content_size = packet_map_len;
1408 pos->packet_size = packet_map_len;
1409 pos->offset = 0; /* Position of the packet header */
1410
1411 packet_index.offset = pos->mmap_offset;
1412 packet_index.content_size = 0;
1413 packet_index.packet_size = 0;
1414 packet_index.timestamp_begin = 0;
1415 packet_index.timestamp_end = 0;
1416 packet_index.events_discarded = 0;
1417 packet_index.events_discarded_len = 0;
1418
1419 /* read and check header, set stream id (and check) */
1420 if (file_stream->parent.trace_packet_header) {
1421 /* Read packet header */
1422 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1423 if (ret) {
1424 if (ret == -EFAULT)
1425 goto retry;
1426 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1427 return ret;
1428 }
1429 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1430 if (len_index >= 0) {
1431 struct bt_definition *field;
1432 uint64_t magic;
1433
1434 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1435 magic = bt_get_unsigned_int(field);
1436 if (magic != CTF_MAGIC) {
1437 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1438 magic,
1439 file_stream->pos.packet_cycles_index->len,
1440 (ssize_t) pos->mmap_offset);
1441 return -EINVAL;
1442 }
1443 }
1444
1445 /* check uuid */
1446 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1447 if (len_index >= 0) {
1448 struct definition_array *defarray;
1449 struct bt_definition *field;
1450 uint64_t i;
1451 uint8_t uuidval[BABELTRACE_UUID_LEN];
1452
1453 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1454 assert(field->declaration->id == CTF_TYPE_ARRAY);
1455 defarray = container_of(field, struct definition_array, p);
1456 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1457
1458 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1459 struct bt_definition *elem;
1460
1461 elem = bt_array_index(defarray, i);
1462 uuidval[i] = bt_get_unsigned_int(elem);
1463 }
1464 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1465 if (ret) {
1466 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1467 return -EINVAL;
1468 }
1469 }
1470
1471 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1472 if (len_index >= 0) {
1473 struct bt_definition *field;
1474
1475 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1476 stream_id = bt_get_unsigned_int(field);
1477 }
1478 }
1479
1480 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1481 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1482 stream_id,
1483 file_stream->parent.stream_id);
1484 return -EINVAL;
1485 }
1486 if (first_packet) {
1487 ret = stream_assign_class(td, file_stream, stream_id);
1488 if (ret)
1489 return ret;
1490 }
1491
1492 if (file_stream->parent.stream_packet_context) {
1493 /* Read packet context */
1494 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1495 if (ret) {
1496 if (ret == -EFAULT)
1497 goto retry;
1498 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1499 return ret;
1500 }
1501 /* read packet size from header */
1502 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1503 if (len_index >= 0) {
1504 struct bt_definition *field;
1505
1506 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1507 packet_index.packet_size = bt_get_unsigned_int(field);
1508 } else {
1509 /* Use file size for packet size */
1510 packet_index.packet_size = filesize * CHAR_BIT;
1511 }
1512
1513 /* read content size from header */
1514 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1515 if (len_index >= 0) {
1516 struct bt_definition *field;
1517
1518 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1519 packet_index.content_size = bt_get_unsigned_int(field);
1520 } else {
1521 /* Use packet size if non-zero, else file size */
1522 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1523 }
1524
1525 /* read timestamp begin from header */
1526 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1527 if (len_index >= 0) {
1528 struct bt_definition *field;
1529
1530 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1531 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1532 if (file_stream->parent.stream_class->trace->parent.collection) {
1533 packet_index.timestamp_begin =
1534 ctf_get_real_timestamp(
1535 &file_stream->parent,
1536 packet_index.timestamp_begin);
1537 }
1538 }
1539
1540 /* read timestamp end from header */
1541 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1542 if (len_index >= 0) {
1543 struct bt_definition *field;
1544
1545 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1546 packet_index.timestamp_end = bt_get_unsigned_int(field);
1547 if (file_stream->parent.stream_class->trace->parent.collection) {
1548 packet_index.timestamp_end =
1549 ctf_get_real_timestamp(
1550 &file_stream->parent,
1551 packet_index.timestamp_end);
1552 }
1553 }
1554
1555 /* read events discarded from header */
1556 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1557 if (len_index >= 0) {
1558 struct bt_definition *field;
1559
1560 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1561 packet_index.events_discarded = bt_get_unsigned_int(field);
1562 packet_index.events_discarded_len = bt_get_int_len(field);
1563 }
1564 } else {
1565 /* Use file size for packet size */
1566 packet_index.packet_size = filesize * CHAR_BIT;
1567 /* Use packet size if non-zero, else file size */
1568 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1569 }
1570
1571 /* Validate content size and packet size values */
1572 if (packet_index.content_size > packet_index.packet_size) {
1573 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1574 packet_index.content_size, packet_index.packet_size);
1575 return -EINVAL;
1576 }
1577
1578 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1579 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1580 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1581 return -EINVAL;
1582 }
1583
1584 if (packet_index.content_size < pos->offset) {
1585 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1586 return -EINVAL;
1587 }
1588
1589 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1590 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1591 return -EINVAL;
1592 }
1593
1594 /* Save position after header and context */
1595 packet_index.data_offset = pos->offset;
1596
1597 /* add index to packet array */
1598 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1599
1600 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1601
1602 return 0;
1603
1604 /* Retry with larger mapping */
1605 retry:
1606 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1607 /*
1608 * Reached EOF, but still expecting header/context data.
1609 */
1610 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1611 return -EFAULT;
1612 }
1613 /* Double the mapping len, and retry */
1614 tmp_map_len = packet_map_len << 1;
1615 if (tmp_map_len >> 1 != packet_map_len) {
1616 /* Overflow */
1617 fprintf(stderr, "[error] Packet mapping length overflow\n");
1618 return -EFAULT;
1619 }
1620 packet_map_len = tmp_map_len;
1621 goto begin;
1622 }
1623
1624 static
1625 int create_stream_packet_index(struct ctf_trace *td,
1626 struct ctf_file_stream *file_stream)
1627 {
1628 struct ctf_stream_pos *pos;
1629 struct stat filestats;
1630 int ret;
1631
1632 pos = &file_stream->pos;
1633
1634 ret = fstat(pos->fd, &filestats);
1635 if (ret < 0)
1636 return ret;
1637
1638 /* Deal with empty files */
1639 if (!filestats.st_size) {
1640 if (file_stream->parent.trace_packet_header
1641 || file_stream->parent.stream_packet_context) {
1642 /*
1643 * We expect a trace packet header and/or stream packet
1644 * context. Since a trace needs to have at least one
1645 * packet, empty files are therefore not accepted.
1646 */
1647 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1648 return -EINVAL;
1649 } else {
1650 /*
1651 * Without trace packet header nor stream packet
1652 * context, a one-packet trace can indeed be empty. This
1653 * is only valid if there is only one stream class: 0.
1654 */
1655 ret = stream_assign_class(td, file_stream, 0);
1656 if (ret)
1657 return ret;
1658 return 0;
1659 }
1660 }
1661
1662 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1663 ret = create_stream_one_packet_index(pos, td, file_stream,
1664 filestats.st_size);
1665 if (ret)
1666 return ret;
1667 }
1668 return 0;
1669 }
1670
1671 static
1672 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1673 {
1674 int ret;
1675
1676 if (td->packet_header_decl) {
1677 struct bt_definition *definition =
1678 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1679 stream->parent_def_scope, 0, 0, "trace.packet.header");
1680 if (!definition) {
1681 ret = -EINVAL;
1682 goto error;
1683 }
1684 stream->trace_packet_header =
1685 container_of(definition, struct definition_struct, p);
1686 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1687 }
1688
1689 return 0;
1690
1691 error:
1692 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
1693 return ret;
1694 }
1695
1696 static
1697 int import_stream_packet_index(struct ctf_trace *td,
1698 struct ctf_file_stream *file_stream)
1699 {
1700 struct ctf_stream_declaration *stream;
1701 struct ctf_stream_pos *pos;
1702 struct ctf_packet_index ctf_index;
1703 struct ctf_packet_index_file_hdr index_hdr;
1704 struct packet_index index;
1705 int index_read;
1706 int ret = 0;
1707 int first_packet = 1;
1708 size_t len;
1709
1710 pos = &file_stream->pos;
1711
1712 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
1713 if (len != 1) {
1714 perror("read index file header");
1715 goto error;
1716 }
1717
1718 /* Check the index header */
1719 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
1720 fprintf(stderr, "[error] wrong index magic\n");
1721 ret = -1;
1722 goto error;
1723 }
1724 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
1725 fprintf(stderr, "[error] Incompatible index file %" PRIu64
1726 ".%" PRIu64 ", supported %d.%d\n",
1727 be64toh(index_hdr.index_major),
1728 be64toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
1729 CTF_INDEX_MINOR);
1730 ret = -1;
1731 goto error;
1732 }
1733 if (index_hdr.packet_index_len == 0) {
1734 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
1735 ret = -1;
1736 goto error;
1737 }
1738
1739 while ((index_read = fread(&ctf_index, index_hdr.packet_index_len, 1,
1740 pos->index_fp)) == 1) {
1741 uint64_t stream_id;
1742
1743 memset(&index, 0, sizeof(index));
1744 index.offset = be64toh(ctf_index.offset);
1745 index.packet_size = be64toh(ctf_index.packet_size);
1746 index.content_size = be64toh(ctf_index.content_size);
1747 index.timestamp_begin = be64toh(ctf_index.timestamp_begin);
1748 index.timestamp_end = be64toh(ctf_index.timestamp_end);
1749 index.events_discarded = be64toh(ctf_index.events_discarded);
1750 index.events_discarded_len = 64;
1751 stream_id = be64toh(ctf_index.stream_id);
1752
1753 if (!first_packet) {
1754 /* add index to packet array */
1755 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1756 continue;
1757 }
1758
1759 file_stream->parent.stream_id = stream_id;
1760 stream = g_ptr_array_index(td->streams, stream_id);
1761 if (!stream) {
1762 fprintf(stderr, "[error] Stream %" PRIu64
1763 " is not declared in metadata.\n",
1764 stream_id);
1765 ret = -EINVAL;
1766 goto error;
1767 }
1768 file_stream->parent.stream_class = stream;
1769 ret = create_stream_definitions(td, &file_stream->parent);
1770 if (ret)
1771 goto error;
1772 first_packet = 0;
1773 /* add index to packet array */
1774 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1775 }
1776
1777 ret = 0;
1778
1779 error:
1780 return ret;
1781 }
1782
1783 /*
1784 * Note: many file streams can inherit from the same stream class
1785 * description (metadata).
1786 */
1787 static
1788 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1789 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1790 int whence))
1791 {
1792 int ret, fd, closeret;
1793 struct ctf_file_stream *file_stream;
1794 struct stat statbuf;
1795 char *index_name;
1796
1797 fd = openat(td->dirfd, path, flags);
1798 if (fd < 0) {
1799 perror("File stream openat()");
1800 ret = fd;
1801 goto error;
1802 }
1803
1804 /* Don't try to mmap subdirectories. Skip them, return success. */
1805 ret = fstat(fd, &statbuf);
1806 if (ret) {
1807 perror("File stream fstat()");
1808 goto fstat_error;
1809 }
1810 if (S_ISDIR(statbuf.st_mode)) {
1811 if (strncmp(path, "index", 5) != 0) {
1812 fprintf(stderr, "[warning] Skipping directory '%s' "
1813 "found in trace\n", path);
1814 }
1815 ret = 0;
1816 goto fd_is_dir_ok;
1817 }
1818
1819 file_stream = g_new0(struct ctf_file_stream, 1);
1820 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1821 file_stream->pos.fd = -1;
1822 file_stream->pos.index_fp = NULL;
1823
1824 strncpy(file_stream->parent.path, path, PATH_MAX);
1825 file_stream->parent.path[PATH_MAX - 1] = '\0';
1826
1827 if (packet_seek) {
1828 file_stream->pos.packet_seek = packet_seek;
1829 } else {
1830 fprintf(stderr, "[error] packet_seek function undefined.\n");
1831 ret = -1;
1832 goto error_def;
1833 }
1834
1835 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
1836 if (ret)
1837 goto error_def;
1838 ret = create_trace_definitions(td, &file_stream->parent);
1839 if (ret)
1840 goto error_def;
1841 /*
1842 * For now, only a single clock per trace is supported.
1843 */
1844 file_stream->parent.current_clock = td->parent.single_clock;
1845
1846 /*
1847 * Allocate the index name for this stream and try to open it.
1848 */
1849 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
1850 if (!index_name) {
1851 fprintf(stderr, "[error] Cannot allocate index filename\n");
1852 goto error_def;
1853 }
1854 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
1855 INDEX_PATH, path);
1856
1857 if (faccessat(td->dirfd, index_name, O_RDONLY, flags) < 0) {
1858 ret = create_stream_packet_index(td, file_stream);
1859 if (ret) {
1860 fprintf(stderr, "[error] Stream index creation error.\n");
1861 goto error_index;
1862 }
1863 } else {
1864 ret = openat(td->dirfd, index_name, flags);
1865 if (ret < 0) {
1866 perror("Index file openat()");
1867 ret = -1;
1868 goto error_free;
1869 }
1870 file_stream->pos.index_fp = fdopen(ret, "r");
1871 if (!file_stream->pos.index_fp) {
1872 perror("fdopen() error");
1873 goto error_free;
1874 }
1875 ret = import_stream_packet_index(td, file_stream);
1876 if (ret) {
1877 ret = -1;
1878 goto error_index;
1879 }
1880 ret = fclose(file_stream->pos.index_fp);
1881 if (ret < 0) {
1882 perror("close index");
1883 goto error_free;
1884 }
1885 }
1886 free(index_name);
1887
1888 /* Add stream file to stream class */
1889 g_ptr_array_add(file_stream->parent.stream_class->streams,
1890 &file_stream->parent);
1891 return 0;
1892
1893 error_index:
1894 if (file_stream->pos.index_fp) {
1895 ret = fclose(file_stream->pos.index_fp);
1896 if (ret < 0) {
1897 perror("close index");
1898 }
1899 }
1900 if (file_stream->parent.trace_packet_header)
1901 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1902 error_free:
1903 free(index_name);
1904 error_def:
1905 closeret = ctf_fini_pos(&file_stream->pos);
1906 if (closeret) {
1907 fprintf(stderr, "Error on ctf_fini_pos\n");
1908 }
1909 g_free(file_stream);
1910 fd_is_dir_ok:
1911 fstat_error:
1912 closeret = close(fd);
1913 if (closeret) {
1914 perror("Error on fd close");
1915 }
1916 error:
1917 return ret;
1918 }
1919
1920 static
1921 int ctf_open_trace_read(struct ctf_trace *td,
1922 const char *path, int flags,
1923 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1924 int whence), FILE *metadata_fp)
1925 {
1926 int ret, closeret;
1927 struct dirent *dirent;
1928 struct dirent *diriter;
1929 size_t dirent_len;
1930 char *ext;
1931
1932 td->flags = flags;
1933
1934 /* Open trace directory */
1935 td->dir = opendir(path);
1936 if (!td->dir) {
1937 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1938 ret = -ENOENT;
1939 goto error;
1940 }
1941
1942 td->dirfd = open(path, 0);
1943 if (td->dirfd < 0) {
1944 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1945 perror("Trace directory open");
1946 ret = -errno;
1947 goto error_dirfd;
1948 }
1949 strncpy(td->parent.path, path, sizeof(td->parent.path));
1950 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
1951
1952 /*
1953 * Keep the metadata file separate.
1954 */
1955
1956 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1957 if (ret) {
1958 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1959 goto error_metadata;
1960 }
1961
1962 /*
1963 * Open each stream: for each file, try to open, check magic
1964 * number, and get the stream ID to add to the right location in
1965 * the stream array.
1966 */
1967
1968 dirent_len = offsetof(struct dirent, d_name) +
1969 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1970
1971 dirent = malloc(dirent_len);
1972
1973 for (;;) {
1974 ret = readdir_r(td->dir, dirent, &diriter);
1975 if (ret) {
1976 fprintf(stderr, "[error] Readdir error.\n");
1977 goto readdir_error;
1978 }
1979 if (!diriter)
1980 break;
1981 /* Ignore hidden files, ., .. and metadata. */
1982 if (!strncmp(diriter->d_name, ".", 1)
1983 || !strcmp(diriter->d_name, "..")
1984 || !strcmp(diriter->d_name, "metadata"))
1985 continue;
1986
1987 /* Ignore index files : *.idx */
1988 ext = strrchr(diriter->d_name, '.');
1989 if (ext && (!strcmp(ext, ".idx"))) {
1990 continue;
1991 }
1992
1993 ret = ctf_open_file_stream_read(td, diriter->d_name,
1994 flags, packet_seek);
1995 if (ret) {
1996 fprintf(stderr, "[error] Open file stream error.\n");
1997 goto readdir_error;
1998 }
1999 }
2000
2001 free(dirent);
2002 return 0;
2003
2004 readdir_error:
2005 free(dirent);
2006 error_metadata:
2007 closeret = close(td->dirfd);
2008 if (closeret) {
2009 perror("Error on fd close");
2010 }
2011 error_dirfd:
2012 closeret = closedir(td->dir);
2013 if (closeret) {
2014 perror("Error on closedir");
2015 }
2016 error:
2017 return ret;
2018 }
2019
2020 /*
2021 * ctf_open_trace: Open a CTF trace and index it.
2022 * Note that the user must seek the trace after the open (using the iterator)
2023 * since the index creation read it entirely.
2024 */
2025 static
2026 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
2027 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2028 int whence), FILE *metadata_fp)
2029 {
2030 struct ctf_trace *td;
2031 int ret;
2032
2033 /*
2034 * If packet_seek is NULL, we provide our default version.
2035 */
2036 if (!packet_seek)
2037 packet_seek = ctf_packet_seek;
2038
2039 td = g_new0(struct ctf_trace, 1);
2040
2041 switch (flags & O_ACCMODE) {
2042 case O_RDONLY:
2043 if (!path) {
2044 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
2045 goto error;
2046 }
2047 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
2048 if (ret)
2049 goto error;
2050 break;
2051 case O_RDWR:
2052 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
2053 goto error;
2054 default:
2055 fprintf(stderr, "[error] Incorrect open flags.\n");
2056 goto error;
2057 }
2058
2059 return &td->parent;
2060 error:
2061 g_free(td);
2062 return NULL;
2063 }
2064
2065 static
2066 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
2067 struct bt_mmap_stream *mmap_info)
2068 {
2069 pos->mmap_offset = 0;
2070 pos->packet_size = 0;
2071 pos->content_size = 0;
2072 pos->content_size_loc = NULL;
2073 pos->fd = mmap_info->fd;
2074 pos->base_mma = NULL;
2075 pos->offset = 0;
2076 pos->dummy = false;
2077 pos->cur_index = 0;
2078 pos->packet_cycles_index = NULL;
2079 pos->packet_real_index = NULL;
2080 pos->prot = PROT_READ;
2081 pos->flags = MAP_PRIVATE;
2082 pos->parent.rw_table = read_dispatch_table;
2083 pos->parent.event_cb = ctf_read_event;
2084 }
2085
2086 static
2087 int prepare_mmap_stream_definition(struct ctf_trace *td,
2088 struct ctf_file_stream *file_stream)
2089 {
2090 struct ctf_stream_declaration *stream;
2091 uint64_t stream_id = 0;
2092 int ret;
2093
2094 file_stream->parent.stream_id = stream_id;
2095 if (stream_id >= td->streams->len) {
2096 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2097 "in metadata.\n", stream_id);
2098 ret = -EINVAL;
2099 goto end;
2100 }
2101 stream = g_ptr_array_index(td->streams, stream_id);
2102 if (!stream) {
2103 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2104 "in metadata.\n", stream_id);
2105 ret = -EINVAL;
2106 goto end;
2107 }
2108 file_stream->parent.stream_class = stream;
2109 ret = create_stream_definitions(td, &file_stream->parent);
2110 end:
2111 return ret;
2112 }
2113
2114 static
2115 int ctf_open_mmap_stream_read(struct ctf_trace *td,
2116 struct bt_mmap_stream *mmap_info,
2117 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2118 int whence))
2119 {
2120 int ret;
2121 struct ctf_file_stream *file_stream;
2122
2123 file_stream = g_new0(struct ctf_file_stream, 1);
2124 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2125 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2126
2127 file_stream->pos.packet_seek = packet_seek;
2128
2129 ret = create_trace_definitions(td, &file_stream->parent);
2130 if (ret) {
2131 goto error_def;
2132 }
2133
2134 ret = prepare_mmap_stream_definition(td, file_stream);
2135 if (ret)
2136 goto error_index;
2137
2138 /*
2139 * For now, only a single clock per trace is supported.
2140 */
2141 file_stream->parent.current_clock = td->parent.single_clock;
2142
2143 /* Add stream file to stream class */
2144 g_ptr_array_add(file_stream->parent.stream_class->streams,
2145 &file_stream->parent);
2146 return 0;
2147
2148 error_index:
2149 if (file_stream->parent.trace_packet_header)
2150 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2151 error_def:
2152 g_free(file_stream);
2153 return ret;
2154 }
2155
2156 static
2157 int ctf_open_mmap_trace_read(struct ctf_trace *td,
2158 struct bt_mmap_stream_list *mmap_list,
2159 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2160 int whence),
2161 FILE *metadata_fp)
2162 {
2163 int ret;
2164 struct bt_mmap_stream *mmap_info;
2165
2166 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
2167 if (ret) {
2168 goto error;
2169 }
2170
2171 /*
2172 * for each stream, try to open, check magic number, and get the
2173 * stream ID to add to the right location in the stream array.
2174 */
2175 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2176 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2177 if (ret) {
2178 fprintf(stderr, "[error] Open file mmap stream error.\n");
2179 goto error;
2180 }
2181 }
2182
2183 return 0;
2184
2185 error:
2186 return ret;
2187 }
2188
2189 static
2190 struct bt_trace_descriptor *ctf_open_mmap_trace(
2191 struct bt_mmap_stream_list *mmap_list,
2192 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2193 int whence),
2194 FILE *metadata_fp)
2195 {
2196 struct ctf_trace *td;
2197 int ret;
2198
2199 if (!metadata_fp) {
2200 fprintf(stderr, "[error] No metadata file pointer associated, "
2201 "required for mmap parsing\n");
2202 goto error;
2203 }
2204 if (!packet_seek) {
2205 fprintf(stderr, "[error] packet_seek function undefined.\n");
2206 goto error;
2207 }
2208 td = g_new0(struct ctf_trace, 1);
2209 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2210 if (ret)
2211 goto error_free;
2212
2213 return &td->parent;
2214
2215 error_free:
2216 g_free(td);
2217 error:
2218 return NULL;
2219 }
2220
2221 static
2222 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2223 {
2224 int i, j, k;
2225 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2226
2227 /* for each stream_class */
2228 for (i = 0; i < td->streams->len; i++) {
2229 struct ctf_stream_declaration *stream_class;
2230
2231 stream_class = g_ptr_array_index(td->streams, i);
2232 if (!stream_class)
2233 continue;
2234 /* for each file_stream */
2235 for (j = 0; j < stream_class->streams->len; j++) {
2236 struct ctf_stream_definition *stream;
2237 struct ctf_stream_pos *stream_pos;
2238 struct ctf_file_stream *cfs;
2239
2240 stream = g_ptr_array_index(stream_class->streams, j);
2241 if (!stream)
2242 continue;
2243 cfs = container_of(stream, struct ctf_file_stream,
2244 parent);
2245 stream_pos = &cfs->pos;
2246 if (!stream_pos->packet_cycles_index)
2247 continue;
2248
2249 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2250 struct packet_index *index;
2251 struct packet_index new_index;
2252
2253 index = &g_array_index(stream_pos->packet_cycles_index,
2254 struct packet_index, k);
2255 memcpy(&new_index, index,
2256 sizeof(struct packet_index));
2257 new_index.timestamp_begin =
2258 ctf_get_real_timestamp(stream,
2259 index->timestamp_begin);
2260 new_index.timestamp_end =
2261 ctf_get_real_timestamp(stream,
2262 index->timestamp_end);
2263 g_array_append_val(stream_pos->packet_real_index,
2264 new_index);
2265 }
2266 }
2267 }
2268 return 0;
2269 }
2270
2271 static
2272 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2273 {
2274 int ret;
2275
2276 ret = ctf_fini_pos(&file_stream->pos);
2277 if (ret) {
2278 fprintf(stderr, "Error on ctf_fini_pos\n");
2279 return -1;
2280 }
2281 ret = close(file_stream->pos.fd);
2282 if (ret) {
2283 perror("Error closing file fd");
2284 return -1;
2285 }
2286 return 0;
2287 }
2288
2289 static
2290 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2291 {
2292 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2293 int ret;
2294
2295 if (td->streams) {
2296 int i;
2297
2298 for (i = 0; i < td->streams->len; i++) {
2299 struct ctf_stream_declaration *stream;
2300 int j;
2301
2302 stream = g_ptr_array_index(td->streams, i);
2303 if (!stream)
2304 continue;
2305 for (j = 0; j < stream->streams->len; j++) {
2306 struct ctf_file_stream *file_stream;
2307 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2308 struct ctf_file_stream, parent);
2309 ret = ctf_close_file_stream(file_stream);
2310 if (ret)
2311 return ret;
2312 }
2313 }
2314 }
2315 ctf_destroy_metadata(td);
2316 ret = close(td->dirfd);
2317 if (ret) {
2318 perror("Error closing dirfd");
2319 return ret;
2320 }
2321 ret = closedir(td->dir);
2322 if (ret) {
2323 perror("Error closedir");
2324 return ret;
2325 }
2326 free(td->metadata_string);
2327 g_free(td);
2328 return 0;
2329 }
2330
2331 static
2332 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2333 struct bt_context *ctx)
2334 {
2335 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2336 parent);
2337
2338 td->parent.ctx = ctx;
2339 }
2340
2341 static
2342 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2343 struct bt_trace_handle *handle)
2344 {
2345 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2346 parent);
2347
2348 td->parent.handle = handle;
2349 }
2350
2351 static
2352 void __attribute__((constructor)) ctf_init(void)
2353 {
2354 int ret;
2355
2356 ctf_format.name = g_quark_from_static_string("ctf");
2357 ret = bt_register_format(&ctf_format);
2358 assert(!ret);
2359 }
2360
2361 static
2362 void __attribute__((destructor)) ctf_exit(void)
2363 {
2364 bt_unregister_format(&ctf_format);
2365 }
This page took 0.113995 seconds and 5 git commands to generate.