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