Be more widely accepting for missing variant/enum fields
[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 /*
631 * One side-effect of this function is to unmap pos mmap base if one is
632 * mapped.
633 */
634 static
635 int find_data_offset(struct ctf_stream_pos *pos,
636 struct ctf_file_stream *file_stream,
637 struct packet_index *packet_index)
638 {
639 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
640 struct stat filestats;
641 size_t filesize;
642 int ret;
643
644 pos = &file_stream->pos;
645
646 ret = fstat(pos->fd, &filestats);
647 if (ret < 0)
648 return ret;
649 filesize = filestats.st_size;
650
651 /* Deal with empty files */
652 if (!filesize) {
653 return 0;
654 }
655
656 begin:
657 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
658 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
659 }
660
661 if (pos->base_mma) {
662 /* unmap old base */
663 ret = munmap_align(pos->base_mma);
664 if (ret) {
665 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
666 strerror(errno));
667 return ret;
668 }
669 pos->base_mma = NULL;
670 }
671 /* map new base. Need mapping length from header. */
672 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
673 MAP_PRIVATE, pos->fd, pos->mmap_offset);
674 assert(pos->base_mma != MAP_FAILED);
675
676 pos->content_size = packet_map_len;
677 pos->packet_size = packet_map_len;
678 pos->offset = 0; /* Position of the packet header */
679
680 /* update trace_packet_header and stream_packet_context */
681 if (pos->prot == PROT_READ && file_stream->parent.trace_packet_header) {
682 /* Read packet header */
683 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
684 if (ret) {
685 if (ret == -EFAULT)
686 goto retry;
687 }
688 }
689 if (pos->prot == PROT_READ && file_stream->parent.stream_packet_context) {
690 /* Read packet context */
691 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
692 if (ret) {
693 if (ret == -EFAULT)
694 goto retry;
695 }
696 }
697 packet_index->data_offset = pos->offset;
698
699 /* unmap old base */
700 ret = munmap_align(pos->base_mma);
701 if (ret) {
702 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
703 strerror(errno));
704 return ret;
705 }
706 pos->base_mma = NULL;
707
708 return 0;
709
710 /* Retry with larger mapping */
711 retry:
712 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
713 /*
714 * Reached EOF, but still expecting header/context data.
715 */
716 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
717 return -EFAULT;
718 }
719 /* Double the mapping len, and retry */
720 tmp_map_len = packet_map_len << 1;
721 if (tmp_map_len >> 1 != packet_map_len) {
722 /* Overflow */
723 fprintf(stderr, "[error] Packet mapping length overflow\n");
724 return -EFAULT;
725 }
726 packet_map_len = tmp_map_len;
727 goto begin;
728 }
729
730
731 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
732 int fd, int open_flags)
733 {
734 pos->fd = fd;
735 if (fd >= 0) {
736 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
737 sizeof(struct packet_index));
738 pos->packet_real_index = g_array_new(FALSE, TRUE,
739 sizeof(struct packet_index));
740 } else {
741 pos->packet_cycles_index = NULL;
742 pos->packet_real_index = NULL;
743 }
744 switch (open_flags & O_ACCMODE) {
745 case O_RDONLY:
746 pos->prot = PROT_READ;
747 pos->flags = MAP_PRIVATE;
748 pos->parent.rw_table = read_dispatch_table;
749 pos->parent.event_cb = ctf_read_event;
750 pos->parent.trace = trace;
751 break;
752 case O_RDWR:
753 pos->prot = PROT_WRITE; /* Write has priority */
754 pos->flags = MAP_SHARED;
755 pos->parent.rw_table = write_dispatch_table;
756 pos->parent.event_cb = ctf_write_event;
757 pos->parent.trace = trace;
758 if (fd >= 0)
759 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
760 break;
761 default:
762 assert(0);
763 }
764 return 0;
765 }
766
767 int ctf_fini_pos(struct ctf_stream_pos *pos)
768 {
769 if (pos->prot == PROT_WRITE && pos->content_size_loc)
770 *pos->content_size_loc = pos->offset;
771 if (pos->base_mma) {
772 int ret;
773
774 /* unmap old base */
775 ret = munmap_align(pos->base_mma);
776 if (ret) {
777 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
778 strerror(errno));
779 return -1;
780 }
781 }
782 if (pos->packet_cycles_index)
783 (void) g_array_free(pos->packet_cycles_index, TRUE);
784 if (pos->packet_real_index)
785 (void) g_array_free(pos->packet_real_index, TRUE);
786 return 0;
787 }
788
789 /*
790 * for SEEK_CUR: go to next packet.
791 * for SEEK_SET: go to packet numer (index).
792 */
793 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
794 {
795 struct ctf_stream_pos *pos =
796 container_of(stream_pos, struct ctf_stream_pos, parent);
797 struct ctf_file_stream *file_stream =
798 container_of(pos, struct ctf_file_stream, pos);
799 int ret;
800 off_t off;
801 struct packet_index *packet_index;
802
803 switch (whence) {
804 case SEEK_CUR:
805 case SEEK_SET: /* Fall-through */
806 break; /* OK */
807 default:
808 assert(0);
809 }
810
811 if (pos->prot == PROT_WRITE && pos->content_size_loc)
812 *pos->content_size_loc = pos->offset;
813
814 if (pos->base_mma) {
815 /* unmap old base */
816 ret = munmap_align(pos->base_mma);
817 if (ret) {
818 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
819 strerror(errno));
820 assert(0);
821 }
822 pos->base_mma = NULL;
823 }
824
825 /*
826 * The caller should never ask for ctf_move_pos across packets,
827 * except to get exactly at the beginning of the next packet.
828 */
829 if (pos->prot == PROT_WRITE) {
830 switch (whence) {
831 case SEEK_CUR:
832 /* The writer will add padding */
833 pos->mmap_offset += pos->packet_size / CHAR_BIT;
834 break;
835 case SEEK_SET:
836 assert(index == 0); /* only seek supported for now */
837 pos->cur_index = 0;
838 break;
839 default:
840 assert(0);
841 }
842 pos->content_size = -1U; /* Unknown at this point */
843 pos->packet_size = WRITE_PACKET_LEN;
844 off = posix_fallocate(pos->fd, pos->mmap_offset,
845 pos->packet_size / CHAR_BIT);
846 assert(off >= 0);
847 pos->offset = 0;
848 } else {
849 read_next_packet:
850 switch (whence) {
851 case SEEK_CUR:
852 {
853 uint64_t events_discarded_diff;
854
855 if (pos->offset == EOF) {
856 return;
857 }
858 assert(pos->cur_index < pos->packet_cycles_index->len);
859 assert(pos->cur_index < pos->packet_real_index->len);
860
861 /* For printing discarded event count */
862 packet_index = &g_array_index(pos->packet_cycles_index,
863 struct packet_index, pos->cur_index);
864 file_stream->parent.prev_cycles_timestamp_end =
865 packet_index->timestamp_end;
866 file_stream->parent.prev_cycles_timestamp =
867 packet_index->timestamp_begin;
868
869 packet_index = &g_array_index(pos->packet_real_index,
870 struct packet_index, pos->cur_index);
871 file_stream->parent.prev_real_timestamp_end =
872 packet_index->timestamp_end;
873 file_stream->parent.prev_real_timestamp =
874 packet_index->timestamp_begin;
875
876 events_discarded_diff = packet_index->events_discarded;
877 if (pos->cur_index > 0) {
878 packet_index = &g_array_index(pos->packet_real_index,
879 struct packet_index,
880 pos->cur_index - 1);
881 events_discarded_diff -= packet_index->events_discarded;
882 /*
883 * Deal with 32-bit wrap-around if the
884 * tracer provided a 32-bit field.
885 */
886 if (packet_index->events_discarded_len == 32) {
887 events_discarded_diff = (uint32_t) events_discarded_diff;
888 }
889 }
890 file_stream->parent.events_discarded = events_discarded_diff;
891 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
892 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
893 /* The reader will expect us to skip padding */
894 ++pos->cur_index;
895 break;
896 }
897 case SEEK_SET:
898 if (index >= pos->packet_cycles_index->len) {
899 pos->offset = EOF;
900 return;
901 }
902 packet_index = &g_array_index(pos->packet_cycles_index,
903 struct packet_index, index);
904 pos->last_events_discarded = packet_index->events_discarded;
905 pos->cur_index = index;
906 file_stream->parent.prev_real_timestamp = 0;
907 file_stream->parent.prev_real_timestamp_end = 0;
908 file_stream->parent.prev_cycles_timestamp = 0;
909 file_stream->parent.prev_cycles_timestamp_end = 0;
910 break;
911 default:
912 assert(0);
913 }
914 if (pos->cur_index >= pos->packet_real_index->len) {
915 /*
916 * We need to check if we are in trace read or
917 * called from packet indexing. In this last
918 * case, the collection is not there, so we
919 * cannot print the timestamps.
920 */
921 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
922 /*
923 * When a stream reaches the end of the
924 * file, we need to show the number of
925 * events discarded ourselves, because
926 * there is no next event scheduled to
927 * be printed in the output.
928 */
929 if (file_stream->parent.events_discarded) {
930 fflush(stdout);
931 ctf_print_discarded(stderr,
932 &file_stream->parent,
933 1);
934 file_stream->parent.events_discarded = 0;
935 }
936 }
937 pos->offset = EOF;
938 return;
939 }
940 packet_index = &g_array_index(pos->packet_cycles_index,
941 struct packet_index,
942 pos->cur_index);
943 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
944
945 packet_index = &g_array_index(pos->packet_real_index,
946 struct packet_index,
947 pos->cur_index);
948 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
949
950 /* Lookup context/packet size in index */
951 if (packet_index->data_offset == -1) {
952 ret = find_data_offset(pos, file_stream, packet_index);
953 if (ret < 0) {
954 return;
955 }
956 }
957 pos->content_size = packet_index->content_size;
958 pos->packet_size = packet_index->packet_size;
959 pos->mmap_offset = packet_index->offset;
960 if (packet_index->data_offset < packet_index->content_size) {
961 pos->offset = 0; /* will read headers */
962 } else if (packet_index->data_offset == packet_index->content_size) {
963 /* empty packet */
964 pos->offset = packet_index->data_offset;
965 whence = SEEK_CUR;
966 goto read_next_packet;
967 } else {
968 pos->offset = EOF;
969 return;
970 }
971 }
972 /* map new base. Need mapping length from header. */
973 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
974 pos->flags, pos->fd, pos->mmap_offset);
975 if (pos->base_mma == MAP_FAILED) {
976 fprintf(stderr, "[error] mmap error %s.\n",
977 strerror(errno));
978 assert(0);
979 }
980
981 /* update trace_packet_header and stream_packet_context */
982 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
983 /* Read packet header */
984 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
985 assert(!ret);
986 }
987 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
988 /* Read packet context */
989 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
990 assert(!ret);
991 }
992 }
993
994 static
995 int packet_metadata(struct ctf_trace *td, FILE *fp)
996 {
997 uint32_t magic;
998 size_t len;
999 int ret = 0;
1000
1001 len = fread(&magic, sizeof(magic), 1, fp);
1002 if (len != 1) {
1003 goto end;
1004 }
1005 if (magic == TSDL_MAGIC) {
1006 ret = 1;
1007 td->byte_order = BYTE_ORDER;
1008 CTF_TRACE_SET_FIELD(td, byte_order);
1009 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1010 ret = 1;
1011 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1012 LITTLE_ENDIAN : BIG_ENDIAN;
1013 CTF_TRACE_SET_FIELD(td, byte_order);
1014 }
1015 end:
1016 rewind(fp);
1017 return ret;
1018 }
1019
1020 /*
1021 * Returns 0 on success, -1 on error.
1022 */
1023 static
1024 int check_version(unsigned int major, unsigned int minor)
1025 {
1026 switch (major) {
1027 case 1:
1028 switch (minor) {
1029 case 8:
1030 return 0;
1031 default:
1032 goto warning;
1033 }
1034 default:
1035 goto warning;
1036
1037 }
1038
1039 /* eventually return an error instead of warning */
1040 warning:
1041 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1042 major, minor);
1043 return 0;
1044 }
1045
1046 static
1047 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
1048 FILE *out)
1049 {
1050 struct metadata_packet_header header;
1051 size_t readlen, writelen, toread;
1052 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
1053 int ret = 0;
1054
1055 readlen = fread(&header, header_sizeof(header), 1, in);
1056 if (readlen < 1)
1057 return -EINVAL;
1058
1059 if (td->byte_order != BYTE_ORDER) {
1060 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1061 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1062 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1063 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1064 }
1065 if (header.checksum)
1066 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
1067 if (header.compression_scheme) {
1068 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
1069 header.compression_scheme);
1070 return -EINVAL;
1071 }
1072 if (header.encryption_scheme) {
1073 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
1074 header.encryption_scheme);
1075 return -EINVAL;
1076 }
1077 if (header.checksum_scheme) {
1078 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
1079 header.checksum_scheme);
1080 return -EINVAL;
1081 }
1082 if (check_version(header.major, header.minor) < 0)
1083 return -EINVAL;
1084 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1085 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1086 CTF_TRACE_SET_FIELD(td, uuid);
1087 } else {
1088 if (babeltrace_uuid_compare(header.uuid, td->uuid))
1089 return -EINVAL;
1090 }
1091
1092 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1093 return -EINVAL;
1094
1095 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
1096
1097 for (;;) {
1098 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
1099 if (ferror(in)) {
1100 ret = -EINVAL;
1101 break;
1102 }
1103 if (babeltrace_debug) {
1104 buf[readlen] = '\0';
1105 fprintf(stderr, "[debug] metadata packet read: %s\n",
1106 buf);
1107 }
1108
1109 writelen = fwrite(buf, sizeof(char), readlen, out);
1110 if (writelen < readlen) {
1111 ret = -EIO;
1112 break;
1113 }
1114 if (ferror(out)) {
1115 ret = -EINVAL;
1116 break;
1117 }
1118 toread -= readlen;
1119 if (!toread) {
1120 ret = 0; /* continue reading next packet */
1121 goto read_padding;
1122 }
1123 }
1124 return ret;
1125
1126 read_padding:
1127 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1128 ret = fseek(in, toread, SEEK_CUR);
1129 if (ret < 0) {
1130 fprintf(stderr, "[warning] Missing padding at end of file\n");
1131 ret = 0;
1132 }
1133 return ret;
1134 }
1135
1136 static
1137 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1138 char **buf)
1139 {
1140 FILE *in, *out;
1141 size_t size, buflen;
1142 int ret;
1143
1144 in = *fp;
1145 /*
1146 * Using strlen on *buf instead of size of open_memstream
1147 * because its size includes garbage at the end (after final
1148 * \0). This is the allocated size, not the actual string size.
1149 */
1150 out = babeltrace_open_memstream(buf, &size);
1151 if (out == NULL) {
1152 perror("Metadata open_memstream");
1153 return -errno;
1154 }
1155 for (;;) {
1156 ret = ctf_open_trace_metadata_packet_read(td, in, out);
1157 if (ret) {
1158 break;
1159 }
1160 if (feof(in)) {
1161 ret = 0;
1162 break;
1163 }
1164 }
1165 /* close to flush the buffer */
1166 ret = babeltrace_close_memstream(buf, &size, out);
1167 if (ret < 0) {
1168 int closeret;
1169
1170 perror("babeltrace_flush_memstream");
1171 ret = -errno;
1172 closeret = fclose(in);
1173 if (closeret) {
1174 perror("Error in fclose");
1175 }
1176 return ret;
1177 }
1178 ret = fclose(in);
1179 if (ret) {
1180 perror("Error in fclose");
1181 }
1182 /* open for reading */
1183 buflen = strlen(*buf);
1184 if (!buflen) {
1185 *fp = NULL;
1186 return -ENOENT;
1187 }
1188 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1189 if (!*fp) {
1190 perror("Metadata fmemopen");
1191 return -errno;
1192 }
1193 return 0;
1194 }
1195
1196 static
1197 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1198 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1199 int whence), FILE *metadata_fp)
1200 {
1201 struct ctf_scanner *scanner;
1202 struct ctf_file_stream *metadata_stream;
1203 FILE *fp;
1204 char *buf = NULL;
1205 int ret = 0, closeret;
1206
1207 metadata_stream = g_new0(struct ctf_file_stream, 1);
1208 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1209
1210 if (packet_seek) {
1211 metadata_stream->pos.packet_seek = packet_seek;
1212 } else {
1213 fprintf(stderr, "[error] packet_seek function undefined.\n");
1214 ret = -1;
1215 goto end_free;
1216 }
1217
1218 if (metadata_fp) {
1219 fp = metadata_fp;
1220 metadata_stream->pos.fd = -1;
1221 } else {
1222 td->metadata = &metadata_stream->parent;
1223 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1224 if (metadata_stream->pos.fd < 0) {
1225 fprintf(stderr, "Unable to open metadata.\n");
1226 ret = -1;
1227 goto end_free;
1228 }
1229
1230 fp = fdopen(metadata_stream->pos.fd, "r");
1231 if (!fp) {
1232 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1233 perror("Metadata stream open");
1234 ret = -errno;
1235 goto end_stream;
1236 }
1237 /* fd now belongs to fp */
1238 metadata_stream->pos.fd = -1;
1239 }
1240 if (babeltrace_debug)
1241 yydebug = 1;
1242
1243 if (packet_metadata(td, fp)) {
1244 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1245 if (ret) {
1246 /* Warn about empty metadata */
1247 fprintf(stderr, "[warning] Empty metadata.\n");
1248 goto end_packet_read;
1249 }
1250 td->metadata_string = buf;
1251 td->metadata_packetized = 1;
1252 } else {
1253 unsigned int major, minor;
1254 ssize_t nr_items;
1255
1256 td->byte_order = BYTE_ORDER;
1257
1258 /* Check text-only metadata header and version */
1259 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1260 if (nr_items < 2)
1261 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1262 if (check_version(major, minor) < 0) {
1263 ret = -EINVAL;
1264 goto end_packet_read;
1265 }
1266 rewind(fp);
1267 }
1268
1269 scanner = ctf_scanner_alloc(fp);
1270 if (!scanner) {
1271 fprintf(stderr, "[error] Error allocating scanner\n");
1272 ret = -ENOMEM;
1273 goto end_scanner_alloc;
1274 }
1275 ret = ctf_scanner_append_ast(scanner);
1276 if (ret) {
1277 fprintf(stderr, "[error] Error creating AST\n");
1278 goto end;
1279 }
1280
1281 if (babeltrace_debug) {
1282 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1283 if (ret) {
1284 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1285 goto end;
1286 }
1287 }
1288
1289 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1290 if (ret) {
1291 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1292 goto end;
1293 }
1294 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1295 td, td->byte_order);
1296 if (ret) {
1297 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1298 goto end;
1299 }
1300 end:
1301 ctf_scanner_free(scanner);
1302 end_scanner_alloc:
1303 end_packet_read:
1304 if (fp) {
1305 closeret = fclose(fp);
1306 if (closeret) {
1307 perror("Error on fclose");
1308 }
1309 }
1310 end_stream:
1311 if (metadata_stream->pos.fd >= 0) {
1312 closeret = close(metadata_stream->pos.fd);
1313 if (closeret) {
1314 perror("Error on metadata stream fd close");
1315 }
1316 }
1317 end_free:
1318 if (ret)
1319 g_free(metadata_stream);
1320 return ret;
1321 }
1322
1323 static
1324 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1325 struct ctf_stream_definition *stream,
1326 struct ctf_event_declaration *event)
1327 {
1328 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1329
1330 if (event->context_decl) {
1331 struct bt_definition *definition =
1332 event->context_decl->p.definition_new(&event->context_decl->p,
1333 stream->parent_def_scope, 0, 0, "event.context");
1334 if (!definition) {
1335 goto error;
1336 }
1337 stream_event->event_context = container_of(definition,
1338 struct definition_struct, p);
1339 stream->parent_def_scope = stream_event->event_context->p.scope;
1340 }
1341 if (event->fields_decl) {
1342 struct bt_definition *definition =
1343 event->fields_decl->p.definition_new(&event->fields_decl->p,
1344 stream->parent_def_scope, 0, 0, "event.fields");
1345 if (!definition) {
1346 goto error;
1347 }
1348 stream_event->event_fields = container_of(definition,
1349 struct definition_struct, p);
1350 stream->parent_def_scope = stream_event->event_fields->p.scope;
1351 }
1352 stream_event->stream = stream;
1353 return stream_event;
1354
1355 error:
1356 if (stream_event->event_fields)
1357 bt_definition_unref(&stream_event->event_fields->p);
1358 if (stream_event->event_context)
1359 bt_definition_unref(&stream_event->event_context->p);
1360 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1361 g_quark_to_string(event->name));
1362 return NULL;
1363 }
1364
1365 static
1366 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1367 {
1368 struct ctf_stream_declaration *stream_class;
1369 int ret;
1370 int i;
1371
1372 if (stream->stream_definitions_created)
1373 return 0;
1374
1375 stream_class = stream->stream_class;
1376
1377 if (stream_class->packet_context_decl) {
1378 struct bt_definition *definition =
1379 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1380 stream->parent_def_scope, 0, 0, "stream.packet.context");
1381 if (!definition) {
1382 ret = -EINVAL;
1383 goto error;
1384 }
1385 stream->stream_packet_context = container_of(definition,
1386 struct definition_struct, p);
1387 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1388 }
1389 if (stream_class->event_header_decl) {
1390 struct bt_definition *definition =
1391 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1392 stream->parent_def_scope, 0, 0, "stream.event.header");
1393 if (!definition) {
1394 ret = -EINVAL;
1395 goto error;
1396 }
1397 stream->stream_event_header =
1398 container_of(definition, struct definition_struct, p);
1399 stream->parent_def_scope = stream->stream_event_header->p.scope;
1400 }
1401 if (stream_class->event_context_decl) {
1402 struct bt_definition *definition =
1403 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1404 stream->parent_def_scope, 0, 0, "stream.event.context");
1405 if (!definition) {
1406 ret = -EINVAL;
1407 goto error;
1408 }
1409 stream->stream_event_context =
1410 container_of(definition, struct definition_struct, p);
1411 stream->parent_def_scope = stream->stream_event_context->p.scope;
1412 }
1413 stream->events_by_id = g_ptr_array_new();
1414 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1415 for (i = 0; i < stream->events_by_id->len; i++) {
1416 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1417 struct ctf_event_definition *stream_event;
1418
1419 if (!event)
1420 continue;
1421 stream_event = create_event_definitions(td, stream, event);
1422 if (!stream_event) {
1423 ret = -EINVAL;
1424 goto error_event;
1425 }
1426 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1427 }
1428 return 0;
1429
1430 error_event:
1431 for (i = 0; i < stream->events_by_id->len; i++) {
1432 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1433 if (stream_event)
1434 g_free(stream_event);
1435 }
1436 g_ptr_array_free(stream->events_by_id, TRUE);
1437 error:
1438 if (stream->stream_event_context)
1439 bt_definition_unref(&stream->stream_event_context->p);
1440 if (stream->stream_event_header)
1441 bt_definition_unref(&stream->stream_event_header->p);
1442 if (stream->stream_packet_context)
1443 bt_definition_unref(&stream->stream_packet_context->p);
1444 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1445 stream_class->stream_id, strerror(-ret));
1446 return ret;
1447 }
1448
1449 static
1450 int stream_assign_class(struct ctf_trace *td,
1451 struct ctf_file_stream *file_stream,
1452 uint64_t stream_id)
1453 {
1454 struct ctf_stream_declaration *stream;
1455 int ret;
1456
1457 file_stream->parent.stream_id = stream_id;
1458 if (stream_id >= td->streams->len) {
1459 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1460 return -EINVAL;
1461 }
1462 stream = g_ptr_array_index(td->streams, stream_id);
1463 if (!stream) {
1464 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1465 return -EINVAL;
1466 }
1467 file_stream->parent.stream_class = stream;
1468 ret = create_stream_definitions(td, &file_stream->parent);
1469 if (ret)
1470 return ret;
1471 return 0;
1472 }
1473
1474 static
1475 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1476 struct ctf_trace *td,
1477 struct ctf_file_stream *file_stream,
1478 size_t filesize)
1479 {
1480 struct packet_index packet_index;
1481 uint64_t stream_id = 0;
1482 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1483 int first_packet = 0;
1484 int len_index;
1485 int ret;
1486
1487 begin:
1488 if (!pos->mmap_offset) {
1489 first_packet = 1;
1490 }
1491
1492 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1493 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1494 }
1495
1496 if (pos->base_mma) {
1497 /* unmap old base */
1498 ret = munmap_align(pos->base_mma);
1499 if (ret) {
1500 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1501 strerror(errno));
1502 return ret;
1503 }
1504 pos->base_mma = NULL;
1505 }
1506 /* map new base. Need mapping length from header. */
1507 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1508 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1509 assert(pos->base_mma != MAP_FAILED);
1510 /*
1511 * Use current mapping size as temporary content and packet
1512 * size.
1513 */
1514 pos->content_size = packet_map_len;
1515 pos->packet_size = packet_map_len;
1516 pos->offset = 0; /* Position of the packet header */
1517
1518 packet_index.offset = pos->mmap_offset;
1519 packet_index.content_size = 0;
1520 packet_index.packet_size = 0;
1521 packet_index.timestamp_begin = 0;
1522 packet_index.timestamp_end = 0;
1523 packet_index.events_discarded = 0;
1524 packet_index.events_discarded_len = 0;
1525
1526 /* read and check header, set stream id (and check) */
1527 if (file_stream->parent.trace_packet_header) {
1528 /* Read packet header */
1529 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1530 if (ret) {
1531 if (ret == -EFAULT)
1532 goto retry;
1533 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1534 return ret;
1535 }
1536 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1537 if (len_index >= 0) {
1538 struct bt_definition *field;
1539 uint64_t magic;
1540
1541 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1542 magic = bt_get_unsigned_int(field);
1543 if (magic != CTF_MAGIC) {
1544 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1545 magic,
1546 file_stream->pos.packet_cycles_index->len,
1547 (ssize_t) pos->mmap_offset);
1548 return -EINVAL;
1549 }
1550 }
1551
1552 /* check uuid */
1553 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1554 if (len_index >= 0) {
1555 struct definition_array *defarray;
1556 struct bt_definition *field;
1557 uint64_t i;
1558 uint8_t uuidval[BABELTRACE_UUID_LEN];
1559
1560 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1561 assert(field->declaration->id == CTF_TYPE_ARRAY);
1562 defarray = container_of(field, struct definition_array, p);
1563 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1564
1565 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1566 struct bt_definition *elem;
1567
1568 elem = bt_array_index(defarray, i);
1569 uuidval[i] = bt_get_unsigned_int(elem);
1570 }
1571 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1572 if (ret) {
1573 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1574 return -EINVAL;
1575 }
1576 }
1577
1578 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1579 if (len_index >= 0) {
1580 struct bt_definition *field;
1581
1582 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1583 stream_id = bt_get_unsigned_int(field);
1584 }
1585 }
1586
1587 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1588 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1589 stream_id,
1590 file_stream->parent.stream_id);
1591 return -EINVAL;
1592 }
1593 if (first_packet) {
1594 ret = stream_assign_class(td, file_stream, stream_id);
1595 if (ret)
1596 return ret;
1597 }
1598
1599 if (file_stream->parent.stream_packet_context) {
1600 /* Read packet context */
1601 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1602 if (ret) {
1603 if (ret == -EFAULT)
1604 goto retry;
1605 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1606 return ret;
1607 }
1608 /* read packet size from header */
1609 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1610 if (len_index >= 0) {
1611 struct bt_definition *field;
1612
1613 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1614 packet_index.packet_size = bt_get_unsigned_int(field);
1615 } else {
1616 /* Use file size for packet size */
1617 packet_index.packet_size = filesize * CHAR_BIT;
1618 }
1619
1620 /* read content size from header */
1621 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1622 if (len_index >= 0) {
1623 struct bt_definition *field;
1624
1625 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1626 packet_index.content_size = bt_get_unsigned_int(field);
1627 } else {
1628 /* Use packet size if non-zero, else file size */
1629 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1630 }
1631
1632 /* read timestamp begin from header */
1633 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1634 if (len_index >= 0) {
1635 struct bt_definition *field;
1636
1637 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1638 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1639 if (file_stream->parent.stream_class->trace->parent.collection) {
1640 packet_index.timestamp_begin =
1641 ctf_get_real_timestamp(
1642 &file_stream->parent,
1643 packet_index.timestamp_begin);
1644 }
1645 }
1646
1647 /* read timestamp end from header */
1648 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1649 if (len_index >= 0) {
1650 struct bt_definition *field;
1651
1652 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1653 packet_index.timestamp_end = bt_get_unsigned_int(field);
1654 if (file_stream->parent.stream_class->trace->parent.collection) {
1655 packet_index.timestamp_end =
1656 ctf_get_real_timestamp(
1657 &file_stream->parent,
1658 packet_index.timestamp_end);
1659 }
1660 }
1661
1662 /* read events discarded from header */
1663 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1664 if (len_index >= 0) {
1665 struct bt_definition *field;
1666
1667 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1668 packet_index.events_discarded = bt_get_unsigned_int(field);
1669 packet_index.events_discarded_len = bt_get_int_len(field);
1670 }
1671 } else {
1672 /* Use file size for packet size */
1673 packet_index.packet_size = filesize * CHAR_BIT;
1674 /* Use packet size if non-zero, else file size */
1675 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1676 }
1677
1678 /* Validate content size and packet size values */
1679 if (packet_index.content_size > packet_index.packet_size) {
1680 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1681 packet_index.content_size, packet_index.packet_size);
1682 return -EINVAL;
1683 }
1684
1685 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1686 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1687 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1688 return -EINVAL;
1689 }
1690
1691 if (packet_index.content_size < pos->offset) {
1692 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1693 return -EINVAL;
1694 }
1695
1696 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1697 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1698 return -EINVAL;
1699 }
1700
1701 /* Save position after header and context */
1702 packet_index.data_offset = pos->offset;
1703
1704 /* add index to packet array */
1705 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1706
1707 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1708
1709 return 0;
1710
1711 /* Retry with larger mapping */
1712 retry:
1713 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1714 /*
1715 * Reached EOF, but still expecting header/context data.
1716 */
1717 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1718 return -EFAULT;
1719 }
1720 /* Double the mapping len, and retry */
1721 tmp_map_len = packet_map_len << 1;
1722 if (tmp_map_len >> 1 != packet_map_len) {
1723 /* Overflow */
1724 fprintf(stderr, "[error] Packet mapping length overflow\n");
1725 return -EFAULT;
1726 }
1727 packet_map_len = tmp_map_len;
1728 goto begin;
1729 }
1730
1731 static
1732 int create_stream_packet_index(struct ctf_trace *td,
1733 struct ctf_file_stream *file_stream)
1734 {
1735 struct ctf_stream_pos *pos;
1736 struct stat filestats;
1737 int ret;
1738
1739 pos = &file_stream->pos;
1740
1741 ret = fstat(pos->fd, &filestats);
1742 if (ret < 0)
1743 return ret;
1744
1745 /* Deal with empty files */
1746 if (!filestats.st_size) {
1747 if (file_stream->parent.trace_packet_header
1748 || file_stream->parent.stream_packet_context) {
1749 /*
1750 * We expect a trace packet header and/or stream packet
1751 * context. Since a trace needs to have at least one
1752 * packet, empty files are therefore not accepted.
1753 */
1754 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1755 return -EINVAL;
1756 } else {
1757 /*
1758 * Without trace packet header nor stream packet
1759 * context, a one-packet trace can indeed be empty. This
1760 * is only valid if there is only one stream class: 0.
1761 */
1762 ret = stream_assign_class(td, file_stream, 0);
1763 if (ret)
1764 return ret;
1765 return 0;
1766 }
1767 }
1768
1769 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1770 ret = create_stream_one_packet_index(pos, td, file_stream,
1771 filestats.st_size);
1772 if (ret)
1773 return ret;
1774 }
1775 return 0;
1776 }
1777
1778 static
1779 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1780 {
1781 int ret;
1782
1783 if (td->packet_header_decl) {
1784 struct bt_definition *definition =
1785 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1786 stream->parent_def_scope, 0, 0, "trace.packet.header");
1787 if (!definition) {
1788 ret = -EINVAL;
1789 goto error;
1790 }
1791 stream->trace_packet_header =
1792 container_of(definition, struct definition_struct, p);
1793 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1794 }
1795
1796 return 0;
1797
1798 error:
1799 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
1800 return ret;
1801 }
1802
1803 static
1804 int import_stream_packet_index(struct ctf_trace *td,
1805 struct ctf_file_stream *file_stream)
1806 {
1807 struct ctf_stream_declaration *stream;
1808 struct ctf_stream_pos *pos;
1809 struct ctf_packet_index ctf_index;
1810 struct ctf_packet_index_file_hdr index_hdr;
1811 struct packet_index index;
1812 int ret = 0;
1813 int first_packet = 1;
1814 size_t len;
1815
1816 pos = &file_stream->pos;
1817
1818 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
1819 if (len != 1) {
1820 perror("read index file header");
1821 goto error;
1822 }
1823
1824 /* Check the index header */
1825 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
1826 fprintf(stderr, "[error] wrong index magic\n");
1827 ret = -1;
1828 goto error;
1829 }
1830 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
1831 fprintf(stderr, "[error] Incompatible index file %" PRIu64
1832 ".%" PRIu64 ", supported %d.%d\n",
1833 be64toh(index_hdr.index_major),
1834 be64toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
1835 CTF_INDEX_MINOR);
1836 ret = -1;
1837 goto error;
1838 }
1839 if (index_hdr.packet_index_len == 0) {
1840 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
1841 ret = -1;
1842 goto error;
1843 }
1844
1845 while (fread(&ctf_index, index_hdr.packet_index_len, 1,
1846 pos->index_fp) == 1) {
1847 uint64_t stream_id;
1848
1849 memset(&index, 0, sizeof(index));
1850 index.offset = be64toh(ctf_index.offset);
1851 index.packet_size = be64toh(ctf_index.packet_size);
1852 index.content_size = be64toh(ctf_index.content_size);
1853 index.timestamp_begin = be64toh(ctf_index.timestamp_begin);
1854 index.timestamp_end = be64toh(ctf_index.timestamp_end);
1855 index.events_discarded = be64toh(ctf_index.events_discarded);
1856 index.events_discarded_len = 64;
1857 index.data_offset = -1;
1858 stream_id = be64toh(ctf_index.stream_id);
1859
1860 if (!first_packet) {
1861 /* add index to packet array */
1862 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1863 continue;
1864 }
1865
1866 file_stream->parent.stream_id = stream_id;
1867 stream = g_ptr_array_index(td->streams, stream_id);
1868 if (!stream) {
1869 fprintf(stderr, "[error] Stream %" PRIu64
1870 " is not declared in metadata.\n",
1871 stream_id);
1872 ret = -EINVAL;
1873 goto error;
1874 }
1875 file_stream->parent.stream_class = stream;
1876 ret = create_stream_definitions(td, &file_stream->parent);
1877 if (ret)
1878 goto error;
1879 first_packet = 0;
1880 /* add index to packet array */
1881 g_array_append_val(file_stream->pos.packet_cycles_index, index);
1882 }
1883
1884 ret = 0;
1885
1886 error:
1887 return ret;
1888 }
1889
1890 /*
1891 * Note: many file streams can inherit from the same stream class
1892 * description (metadata).
1893 */
1894 static
1895 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1896 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1897 int whence))
1898 {
1899 int ret, fd, closeret;
1900 struct ctf_file_stream *file_stream;
1901 struct stat statbuf;
1902 char *index_name;
1903
1904 fd = openat(td->dirfd, path, flags);
1905 if (fd < 0) {
1906 perror("File stream openat()");
1907 ret = fd;
1908 goto error;
1909 }
1910
1911 /* Don't try to mmap subdirectories. Skip them, return success. */
1912 ret = fstat(fd, &statbuf);
1913 if (ret) {
1914 perror("File stream fstat()");
1915 goto fstat_error;
1916 }
1917 if (S_ISDIR(statbuf.st_mode)) {
1918 if (strncmp(path, "index", 5) != 0) {
1919 fprintf(stderr, "[warning] Skipping directory '%s' "
1920 "found in trace\n", path);
1921 }
1922 ret = 0;
1923 goto fd_is_dir_ok;
1924 }
1925
1926 file_stream = g_new0(struct ctf_file_stream, 1);
1927 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1928 file_stream->pos.fd = -1;
1929 file_stream->pos.index_fp = NULL;
1930
1931 strncpy(file_stream->parent.path, path, PATH_MAX);
1932 file_stream->parent.path[PATH_MAX - 1] = '\0';
1933
1934 if (packet_seek) {
1935 file_stream->pos.packet_seek = packet_seek;
1936 } else {
1937 fprintf(stderr, "[error] packet_seek function undefined.\n");
1938 ret = -1;
1939 goto error_def;
1940 }
1941
1942 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
1943 if (ret)
1944 goto error_def;
1945 ret = create_trace_definitions(td, &file_stream->parent);
1946 if (ret)
1947 goto error_def;
1948 /*
1949 * For now, only a single clock per trace is supported.
1950 */
1951 file_stream->parent.current_clock = td->parent.single_clock;
1952
1953 /*
1954 * Allocate the index name for this stream and try to open it.
1955 */
1956 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
1957 if (!index_name) {
1958 fprintf(stderr, "[error] Cannot allocate index filename\n");
1959 goto error_def;
1960 }
1961 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
1962 INDEX_PATH, path);
1963
1964 if (faccessat(td->dirfd, index_name, O_RDONLY, flags) < 0) {
1965 ret = create_stream_packet_index(td, file_stream);
1966 if (ret) {
1967 fprintf(stderr, "[error] Stream index creation error.\n");
1968 goto error_index;
1969 }
1970 } else {
1971 ret = openat(td->dirfd, index_name, flags);
1972 if (ret < 0) {
1973 perror("Index file openat()");
1974 ret = -1;
1975 goto error_free;
1976 }
1977 file_stream->pos.index_fp = fdopen(ret, "r");
1978 if (!file_stream->pos.index_fp) {
1979 perror("fdopen() error");
1980 goto error_free;
1981 }
1982 ret = import_stream_packet_index(td, file_stream);
1983 if (ret) {
1984 ret = -1;
1985 goto error_index;
1986 }
1987 ret = fclose(file_stream->pos.index_fp);
1988 if (ret < 0) {
1989 perror("close index");
1990 goto error_free;
1991 }
1992 }
1993 free(index_name);
1994
1995 /* Add stream file to stream class */
1996 g_ptr_array_add(file_stream->parent.stream_class->streams,
1997 &file_stream->parent);
1998 return 0;
1999
2000 error_index:
2001 if (file_stream->pos.index_fp) {
2002 ret = fclose(file_stream->pos.index_fp);
2003 if (ret < 0) {
2004 perror("close index");
2005 }
2006 }
2007 if (file_stream->parent.trace_packet_header)
2008 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2009 error_free:
2010 free(index_name);
2011 error_def:
2012 closeret = ctf_fini_pos(&file_stream->pos);
2013 if (closeret) {
2014 fprintf(stderr, "Error on ctf_fini_pos\n");
2015 }
2016 g_free(file_stream);
2017 fd_is_dir_ok:
2018 fstat_error:
2019 closeret = close(fd);
2020 if (closeret) {
2021 perror("Error on fd close");
2022 }
2023 error:
2024 return ret;
2025 }
2026
2027 static
2028 int ctf_open_trace_read(struct ctf_trace *td,
2029 const char *path, int flags,
2030 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2031 int whence), FILE *metadata_fp)
2032 {
2033 int ret, closeret;
2034 struct dirent *dirent;
2035 struct dirent *diriter;
2036 size_t dirent_len;
2037 char *ext;
2038
2039 td->flags = flags;
2040
2041 /* Open trace directory */
2042 td->dir = opendir(path);
2043 if (!td->dir) {
2044 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
2045 ret = -ENOENT;
2046 goto error;
2047 }
2048
2049 td->dirfd = open(path, 0);
2050 if (td->dirfd < 0) {
2051 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
2052 perror("Trace directory open");
2053 ret = -errno;
2054 goto error_dirfd;
2055 }
2056 strncpy(td->parent.path, path, sizeof(td->parent.path));
2057 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
2058
2059 /*
2060 * Keep the metadata file separate.
2061 */
2062
2063 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
2064 if (ret) {
2065 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
2066 goto error_metadata;
2067 }
2068
2069 /*
2070 * Open each stream: for each file, try to open, check magic
2071 * number, and get the stream ID to add to the right location in
2072 * the stream array.
2073 */
2074
2075 dirent_len = offsetof(struct dirent, d_name) +
2076 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
2077
2078 dirent = malloc(dirent_len);
2079
2080 for (;;) {
2081 ret = readdir_r(td->dir, dirent, &diriter);
2082 if (ret) {
2083 fprintf(stderr, "[error] Readdir error.\n");
2084 goto readdir_error;
2085 }
2086 if (!diriter)
2087 break;
2088 /* Ignore hidden files, ., .. and metadata. */
2089 if (!strncmp(diriter->d_name, ".", 1)
2090 || !strcmp(diriter->d_name, "..")
2091 || !strcmp(diriter->d_name, "metadata"))
2092 continue;
2093
2094 /* Ignore index files : *.idx */
2095 ext = strrchr(diriter->d_name, '.');
2096 if (ext && (!strcmp(ext, ".idx"))) {
2097 continue;
2098 }
2099
2100 ret = ctf_open_file_stream_read(td, diriter->d_name,
2101 flags, packet_seek);
2102 if (ret) {
2103 fprintf(stderr, "[error] Open file stream error.\n");
2104 goto readdir_error;
2105 }
2106 }
2107
2108 free(dirent);
2109 return 0;
2110
2111 readdir_error:
2112 free(dirent);
2113 error_metadata:
2114 closeret = close(td->dirfd);
2115 if (closeret) {
2116 perror("Error on fd close");
2117 }
2118 error_dirfd:
2119 closeret = closedir(td->dir);
2120 if (closeret) {
2121 perror("Error on closedir");
2122 }
2123 error:
2124 return ret;
2125 }
2126
2127 /*
2128 * ctf_open_trace: Open a CTF trace and index it.
2129 * Note that the user must seek the trace after the open (using the iterator)
2130 * since the index creation read it entirely.
2131 */
2132 static
2133 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
2134 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2135 int whence), FILE *metadata_fp)
2136 {
2137 struct ctf_trace *td;
2138 int ret;
2139
2140 /*
2141 * If packet_seek is NULL, we provide our default version.
2142 */
2143 if (!packet_seek)
2144 packet_seek = ctf_packet_seek;
2145
2146 td = g_new0(struct ctf_trace, 1);
2147
2148 switch (flags & O_ACCMODE) {
2149 case O_RDONLY:
2150 if (!path) {
2151 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
2152 goto error;
2153 }
2154 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
2155 if (ret)
2156 goto error;
2157 break;
2158 case O_RDWR:
2159 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
2160 goto error;
2161 default:
2162 fprintf(stderr, "[error] Incorrect open flags.\n");
2163 goto error;
2164 }
2165
2166 return &td->parent;
2167 error:
2168 g_free(td);
2169 return NULL;
2170 }
2171
2172 static
2173 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
2174 struct bt_mmap_stream *mmap_info)
2175 {
2176 pos->mmap_offset = 0;
2177 pos->packet_size = 0;
2178 pos->content_size = 0;
2179 pos->content_size_loc = NULL;
2180 pos->fd = mmap_info->fd;
2181 pos->base_mma = NULL;
2182 pos->offset = 0;
2183 pos->dummy = false;
2184 pos->cur_index = 0;
2185 pos->packet_cycles_index = NULL;
2186 pos->packet_real_index = NULL;
2187 pos->prot = PROT_READ;
2188 pos->flags = MAP_PRIVATE;
2189 pos->parent.rw_table = read_dispatch_table;
2190 pos->parent.event_cb = ctf_read_event;
2191 }
2192
2193 static
2194 int prepare_mmap_stream_definition(struct ctf_trace *td,
2195 struct ctf_file_stream *file_stream)
2196 {
2197 struct ctf_stream_declaration *stream;
2198 uint64_t stream_id = 0;
2199 int ret;
2200
2201 file_stream->parent.stream_id = stream_id;
2202 if (stream_id >= td->streams->len) {
2203 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2204 "in metadata.\n", stream_id);
2205 ret = -EINVAL;
2206 goto end;
2207 }
2208 stream = g_ptr_array_index(td->streams, stream_id);
2209 if (!stream) {
2210 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2211 "in metadata.\n", stream_id);
2212 ret = -EINVAL;
2213 goto end;
2214 }
2215 file_stream->parent.stream_class = stream;
2216 ret = create_stream_definitions(td, &file_stream->parent);
2217 end:
2218 return ret;
2219 }
2220
2221 static
2222 int ctf_open_mmap_stream_read(struct ctf_trace *td,
2223 struct bt_mmap_stream *mmap_info,
2224 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2225 int whence))
2226 {
2227 int ret;
2228 struct ctf_file_stream *file_stream;
2229
2230 file_stream = g_new0(struct ctf_file_stream, 1);
2231 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2232 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2233
2234 file_stream->pos.packet_seek = packet_seek;
2235
2236 ret = create_trace_definitions(td, &file_stream->parent);
2237 if (ret) {
2238 goto error_def;
2239 }
2240
2241 ret = prepare_mmap_stream_definition(td, file_stream);
2242 if (ret)
2243 goto error_index;
2244
2245 /*
2246 * For now, only a single clock per trace is supported.
2247 */
2248 file_stream->parent.current_clock = td->parent.single_clock;
2249
2250 /* Add stream file to stream class */
2251 g_ptr_array_add(file_stream->parent.stream_class->streams,
2252 &file_stream->parent);
2253 return 0;
2254
2255 error_index:
2256 if (file_stream->parent.trace_packet_header)
2257 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2258 error_def:
2259 g_free(file_stream);
2260 return ret;
2261 }
2262
2263 static
2264 int ctf_open_mmap_trace_read(struct ctf_trace *td,
2265 struct bt_mmap_stream_list *mmap_list,
2266 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2267 int whence),
2268 FILE *metadata_fp)
2269 {
2270 int ret;
2271 struct bt_mmap_stream *mmap_info;
2272
2273 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
2274 if (ret) {
2275 goto error;
2276 }
2277
2278 /*
2279 * for each stream, try to open, check magic number, and get the
2280 * stream ID to add to the right location in the stream array.
2281 */
2282 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2283 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2284 if (ret) {
2285 fprintf(stderr, "[error] Open file mmap stream error.\n");
2286 goto error;
2287 }
2288 }
2289
2290 return 0;
2291
2292 error:
2293 return ret;
2294 }
2295
2296 static
2297 struct bt_trace_descriptor *ctf_open_mmap_trace(
2298 struct bt_mmap_stream_list *mmap_list,
2299 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2300 int whence),
2301 FILE *metadata_fp)
2302 {
2303 struct ctf_trace *td;
2304 int ret;
2305
2306 if (!metadata_fp) {
2307 fprintf(stderr, "[error] No metadata file pointer associated, "
2308 "required for mmap parsing\n");
2309 goto error;
2310 }
2311 if (!packet_seek) {
2312 fprintf(stderr, "[error] packet_seek function undefined.\n");
2313 goto error;
2314 }
2315 td = g_new0(struct ctf_trace, 1);
2316 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2317 if (ret)
2318 goto error_free;
2319
2320 return &td->parent;
2321
2322 error_free:
2323 g_free(td);
2324 error:
2325 return NULL;
2326 }
2327
2328 static
2329 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2330 {
2331 int i, j, k;
2332 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2333
2334 /* for each stream_class */
2335 for (i = 0; i < td->streams->len; i++) {
2336 struct ctf_stream_declaration *stream_class;
2337
2338 stream_class = g_ptr_array_index(td->streams, i);
2339 if (!stream_class)
2340 continue;
2341 /* for each file_stream */
2342 for (j = 0; j < stream_class->streams->len; j++) {
2343 struct ctf_stream_definition *stream;
2344 struct ctf_stream_pos *stream_pos;
2345 struct ctf_file_stream *cfs;
2346
2347 stream = g_ptr_array_index(stream_class->streams, j);
2348 if (!stream)
2349 continue;
2350 cfs = container_of(stream, struct ctf_file_stream,
2351 parent);
2352 stream_pos = &cfs->pos;
2353 if (!stream_pos->packet_cycles_index)
2354 continue;
2355
2356 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2357 struct packet_index *index;
2358 struct packet_index new_index;
2359
2360 index = &g_array_index(stream_pos->packet_cycles_index,
2361 struct packet_index, k);
2362 memcpy(&new_index, index,
2363 sizeof(struct packet_index));
2364 new_index.timestamp_begin =
2365 ctf_get_real_timestamp(stream,
2366 index->timestamp_begin);
2367 new_index.timestamp_end =
2368 ctf_get_real_timestamp(stream,
2369 index->timestamp_end);
2370 g_array_append_val(stream_pos->packet_real_index,
2371 new_index);
2372 }
2373 }
2374 }
2375 return 0;
2376 }
2377
2378 static
2379 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2380 {
2381 int ret;
2382
2383 ret = ctf_fini_pos(&file_stream->pos);
2384 if (ret) {
2385 fprintf(stderr, "Error on ctf_fini_pos\n");
2386 return -1;
2387 }
2388 ret = close(file_stream->pos.fd);
2389 if (ret) {
2390 perror("Error closing file fd");
2391 return -1;
2392 }
2393 return 0;
2394 }
2395
2396 static
2397 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2398 {
2399 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2400 int ret;
2401
2402 if (td->streams) {
2403 int i;
2404
2405 for (i = 0; i < td->streams->len; i++) {
2406 struct ctf_stream_declaration *stream;
2407 int j;
2408
2409 stream = g_ptr_array_index(td->streams, i);
2410 if (!stream)
2411 continue;
2412 for (j = 0; j < stream->streams->len; j++) {
2413 struct ctf_file_stream *file_stream;
2414 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2415 struct ctf_file_stream, parent);
2416 ret = ctf_close_file_stream(file_stream);
2417 if (ret)
2418 return ret;
2419 }
2420 }
2421 }
2422 ctf_destroy_metadata(td);
2423 ret = close(td->dirfd);
2424 if (ret) {
2425 perror("Error closing dirfd");
2426 return ret;
2427 }
2428 ret = closedir(td->dir);
2429 if (ret) {
2430 perror("Error closedir");
2431 return ret;
2432 }
2433 free(td->metadata_string);
2434 g_free(td);
2435 return 0;
2436 }
2437
2438 static
2439 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2440 struct bt_context *ctx)
2441 {
2442 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2443 parent);
2444
2445 td->parent.ctx = ctx;
2446 }
2447
2448 static
2449 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2450 struct bt_trace_handle *handle)
2451 {
2452 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2453 parent);
2454
2455 td->parent.handle = handle;
2456 }
2457
2458 static
2459 void __attribute__((constructor)) ctf_init(void)
2460 {
2461 int ret;
2462
2463 ctf_format.name = g_quark_from_static_string("ctf");
2464 ret = bt_register_format(&ctf_format);
2465 assert(!ret);
2466 }
2467
2468 static
2469 void __attribute__((destructor)) ctf_exit(void)
2470 {
2471 bt_unregister_format(&ctf_format);
2472 }
This page took 0.119637 seconds and 4 git commands to generate.