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