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