ir: rename ctf_string_encoding -> bt_ctf_string_encoding
[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 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
129 [ CTF_TYPE_FLOAT ] = ctf_float_read,
130 [ CTF_TYPE_ENUM ] = ctf_enum_read,
131 [ CTF_TYPE_STRING ] = ctf_string_read,
132 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
133 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
134 [ CTF_TYPE_ARRAY ] = ctf_array_read,
135 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
136 };
137
138 static
139 rw_dispatch write_dispatch_table[] = {
140 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
141 [ CTF_TYPE_FLOAT ] = ctf_float_write,
142 [ CTF_TYPE_ENUM ] = ctf_enum_write,
143 [ CTF_TYPE_STRING ] = ctf_string_write,
144 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
145 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
146 [ CTF_TYPE_ARRAY ] = ctf_array_write,
147 [ CTF_TYPE_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 * for SEEK_CUR: go to next packet.
926 * for SEEK_SET: go to packet numer (index).
927 */
928 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
929 {
930 struct ctf_stream_pos *pos =
931 container_of(stream_pos, struct ctf_stream_pos, parent);
932 struct ctf_file_stream *file_stream =
933 container_of(pos, struct ctf_file_stream, pos);
934 int ret;
935 struct packet_index *packet_index, *prev_index;
936
937 switch (whence) {
938 case SEEK_CUR:
939 case SEEK_SET: /* Fall-through */
940 break; /* OK */
941 default:
942 assert(0);
943 }
944
945 if ((pos->prot & PROT_WRITE) && pos->content_size_loc)
946 *pos->content_size_loc = pos->offset;
947
948 if (pos->base_mma) {
949 /* unmap old base */
950 ret = munmap_align(pos->base_mma);
951 if (ret) {
952 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
953 strerror(errno));
954 assert(0);
955 }
956 pos->base_mma = NULL;
957 }
958
959 /*
960 * The caller should never ask for ctf_move_pos across packets,
961 * except to get exactly at the beginning of the next packet.
962 */
963 if (pos->prot & PROT_WRITE) {
964 switch (whence) {
965 case SEEK_CUR:
966 /* The writer will add padding */
967 pos->mmap_offset += pos->packet_size / CHAR_BIT;
968 break;
969 case SEEK_SET:
970 assert(index == 0); /* only seek supported for now */
971 pos->cur_index = 0;
972 break;
973 default:
974 assert(0);
975 }
976 pos->content_size = -1U; /* Unknown at this point */
977 pos->packet_size = WRITE_PACKET_LEN;
978 do {
979 ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
980 pos->packet_size / CHAR_BIT);
981 } while (ret == EINTR);
982 assert(ret == 0);
983 pos->offset = 0;
984 } else {
985 read_next_packet:
986 switch (whence) {
987 case SEEK_CUR:
988 {
989 if (pos->offset == EOF) {
990 return;
991 }
992 assert(pos->cur_index < pos->packet_index->len);
993 /* The reader will expect us to skip padding */
994 ++pos->cur_index;
995 break;
996 }
997 case SEEK_SET:
998 if (index >= pos->packet_index->len) {
999 pos->offset = EOF;
1000 return;
1001 }
1002 pos->cur_index = index;
1003 break;
1004 default:
1005 assert(0);
1006 }
1007
1008 if (pos->cur_index >= pos->packet_index->len) {
1009 pos->offset = EOF;
1010 return;
1011 }
1012
1013 packet_index = &g_array_index(pos->packet_index,
1014 struct packet_index, pos->cur_index);
1015 if (pos->cur_index > 0) {
1016 prev_index = &g_array_index(pos->packet_index,
1017 struct packet_index,
1018 pos->cur_index - 1);
1019 } else {
1020 prev_index = NULL;
1021 }
1022 ctf_update_current_packet_index(&file_stream->parent,
1023 prev_index, packet_index);
1024
1025 /*
1026 * We need to check if we are in trace read or called
1027 * from packet indexing. In this last case, the
1028 * collection is not there, so we cannot print the
1029 * timestamps.
1030 */
1031 if ((&file_stream->parent)->stream_class->trace->parent.collection) {
1032 ctf_print_discarded_lost(stderr, &file_stream->parent);
1033 }
1034
1035 packet_index = &g_array_index(pos->packet_index,
1036 struct packet_index,
1037 pos->cur_index);
1038 file_stream->parent.cycles_timestamp = packet_index->ts_cycles.timestamp_begin;
1039
1040 file_stream->parent.real_timestamp = packet_index->ts_real.timestamp_begin;
1041
1042 /* Lookup context/packet size in index */
1043 if (packet_index->data_offset == -1) {
1044 ret = find_data_offset(pos, file_stream, packet_index);
1045 if (ret < 0) {
1046 return;
1047 }
1048 }
1049 pos->content_size = packet_index->content_size;
1050 pos->packet_size = packet_index->packet_size;
1051 pos->mmap_offset = packet_index->offset;
1052 pos->data_offset = packet_index->data_offset;
1053 if (pos->data_offset < packet_index->content_size) {
1054 pos->offset = 0; /* will read headers */
1055 } else if (pos->data_offset == packet_index->content_size) {
1056 /* empty packet */
1057 pos->offset = packet_index->data_offset;
1058 whence = SEEK_CUR;
1059 goto read_next_packet;
1060 } else {
1061 pos->offset = EOF;
1062 return;
1063 }
1064 }
1065 /* map new base. Need mapping length from header. */
1066 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
1067 pos->flags, pos->fd, pos->mmap_offset);
1068 if (pos->base_mma == MAP_FAILED) {
1069 fprintf(stderr, "[error] mmap error %s.\n",
1070 strerror(errno));
1071 assert(0);
1072 }
1073
1074 /* update trace_packet_header and stream_packet_context */
1075 if (!(pos->prot & PROT_WRITE) &&
1076 file_stream->parent.trace_packet_header) {
1077 /* Read packet header */
1078 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1079 assert(!ret);
1080 }
1081 if (!(pos->prot & PROT_WRITE) &&
1082 file_stream->parent.stream_packet_context) {
1083 /* Read packet context */
1084 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1085 assert(!ret);
1086 }
1087 }
1088
1089 static
1090 int packet_metadata(struct ctf_trace *td, FILE *fp)
1091 {
1092 uint32_t magic;
1093 size_t len;
1094 int ret = 0;
1095
1096 len = fread(&magic, sizeof(magic), 1, fp);
1097 if (len != 1) {
1098 goto end;
1099 }
1100 if (magic == TSDL_MAGIC) {
1101 ret = 1;
1102 td->byte_order = BYTE_ORDER;
1103 CTF_TRACE_SET_FIELD(td, byte_order);
1104 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
1105 ret = 1;
1106 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
1107 LITTLE_ENDIAN : BIG_ENDIAN;
1108 CTF_TRACE_SET_FIELD(td, byte_order);
1109 }
1110 end:
1111 rewind(fp);
1112 return ret;
1113 }
1114
1115 /*
1116 * Returns 0 on success, -1 on error.
1117 */
1118 static
1119 int check_version(unsigned int major, unsigned int minor)
1120 {
1121 switch (major) {
1122 case 1:
1123 switch (minor) {
1124 case 8:
1125 return 0;
1126 default:
1127 goto warning;
1128 }
1129 default:
1130 goto warning;
1131
1132 }
1133
1134 /* eventually return an error instead of warning */
1135 warning:
1136 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
1137 major, minor);
1138 return 0;
1139 }
1140
1141 static
1142 int ctf_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
1143 FILE *out)
1144 {
1145 struct metadata_packet_header header;
1146 size_t readlen, writelen, toread;
1147 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
1148 int ret = 0;
1149
1150 readlen = fread(&header, header_sizeof(header), 1, in);
1151 if (readlen < 1)
1152 return -EINVAL;
1153
1154 if (td->byte_order != BYTE_ORDER) {
1155 header.magic = GUINT32_SWAP_LE_BE(header.magic);
1156 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
1157 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
1158 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
1159 }
1160 if (header.checksum)
1161 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
1162 if (header.compression_scheme) {
1163 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
1164 header.compression_scheme);
1165 return -EINVAL;
1166 }
1167 if (header.encryption_scheme) {
1168 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
1169 header.encryption_scheme);
1170 return -EINVAL;
1171 }
1172 if (header.checksum_scheme) {
1173 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
1174 header.checksum_scheme);
1175 return -EINVAL;
1176 }
1177 if (check_version(header.major, header.minor) < 0)
1178 return -EINVAL;
1179 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
1180 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
1181 CTF_TRACE_SET_FIELD(td, uuid);
1182 } else {
1183 if (bt_uuid_compare(header.uuid, td->uuid))
1184 return -EINVAL;
1185 }
1186
1187 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
1188 return -EINVAL;
1189
1190 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
1191
1192 for (;;) {
1193 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
1194 if (ferror(in)) {
1195 ret = -EINVAL;
1196 break;
1197 }
1198 if (babeltrace_debug) {
1199 buf[readlen] = '\0';
1200 fprintf(stderr, "[debug] metadata packet read: %s\n",
1201 buf);
1202 }
1203
1204 writelen = fwrite(buf, sizeof(char), readlen, out);
1205 if (writelen < readlen) {
1206 ret = -EIO;
1207 break;
1208 }
1209 if (ferror(out)) {
1210 ret = -EINVAL;
1211 break;
1212 }
1213 toread -= readlen;
1214 if (!toread) {
1215 ret = 0; /* continue reading next packet */
1216 goto read_padding;
1217 }
1218 }
1219 return ret;
1220
1221 read_padding:
1222 toread = (header.packet_size - header.content_size) / CHAR_BIT;
1223 ret = fseek(in, toread, SEEK_CUR);
1224 if (ret < 0) {
1225 fprintf(stderr, "[warning] Missing padding at end of file\n");
1226 ret = 0;
1227 }
1228 return ret;
1229 }
1230
1231 static
1232 int ctf_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
1233 char **buf)
1234 {
1235 FILE *in, *out;
1236 size_t size, buflen;
1237 int ret;
1238
1239 in = *fp;
1240 /*
1241 * Using strlen on *buf instead of size of open_memstream
1242 * because its size includes garbage at the end (after final
1243 * \0). This is the allocated size, not the actual string size.
1244 */
1245 out = babeltrace_open_memstream(buf, &size);
1246 if (out == NULL) {
1247 perror("Metadata open_memstream");
1248 return -errno;
1249 }
1250 for (;;) {
1251 ret = ctf_trace_metadata_packet_read(td, in, out);
1252 if (ret) {
1253 break;
1254 }
1255 if (feof(in)) {
1256 ret = 0;
1257 break;
1258 }
1259 }
1260 /* close to flush the buffer */
1261 ret = babeltrace_close_memstream(buf, &size, out);
1262 if (ret < 0) {
1263 int closeret;
1264
1265 perror("babeltrace_flush_memstream");
1266 ret = -errno;
1267 closeret = fclose(in);
1268 if (closeret) {
1269 perror("Error in fclose");
1270 }
1271 return ret;
1272 }
1273 ret = fclose(in);
1274 if (ret) {
1275 perror("Error in fclose");
1276 }
1277 /* open for reading */
1278 buflen = strlen(*buf);
1279 if (!buflen) {
1280 *fp = NULL;
1281 return -ENOENT;
1282 }
1283 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1284 if (!*fp) {
1285 perror("Metadata fmemopen");
1286 return -errno;
1287 }
1288 return 0;
1289 }
1290
1291 static
1292 int ctf_trace_metadata_read(struct ctf_trace *td, FILE *metadata_fp,
1293 struct ctf_scanner *scanner, int append)
1294 {
1295 struct ctf_file_stream *metadata_stream;
1296 FILE *fp;
1297 char *buf = NULL;
1298 int ret = 0, closeret;
1299
1300 metadata_stream = g_new0(struct ctf_file_stream, 1);
1301 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1302
1303 if (metadata_fp) {
1304 fp = metadata_fp;
1305 metadata_stream->pos.fd = -1;
1306 } else {
1307 td->metadata = &metadata_stream->parent;
1308 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1309 if (metadata_stream->pos.fd < 0) {
1310 fprintf(stderr, "Unable to open metadata.\n");
1311 ret = -1;
1312 goto end_free;
1313 }
1314
1315 fp = fdopen(metadata_stream->pos.fd, "r");
1316 if (!fp) {
1317 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1318 perror("Metadata stream open");
1319 ret = -errno;
1320 goto end_stream;
1321 }
1322 /* fd now belongs to fp */
1323 metadata_stream->pos.fd = -1;
1324 }
1325 if (babeltrace_debug)
1326 yydebug = 1;
1327
1328 if (packet_metadata(td, fp)) {
1329 ret = ctf_trace_metadata_stream_read(td, &fp, &buf);
1330 if (ret) {
1331 goto end;
1332 }
1333 td->metadata_string = buf;
1334 td->metadata_packetized = 1;
1335 } else {
1336 if (!append) {
1337 unsigned int major, minor;
1338 ssize_t nr_items;
1339
1340 td->byte_order = BYTE_ORDER;
1341
1342 /* Check text-only metadata header and version */
1343 nr_items = fscanf(fp, "/* CTF %10u.%10u", &major, &minor);
1344 if (nr_items < 2)
1345 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1346 if (check_version(major, minor) < 0) {
1347 ret = -EINVAL;
1348 goto end;
1349 }
1350 rewind(fp);
1351 }
1352 }
1353
1354 ret = ctf_scanner_append_ast(scanner, fp);
1355 if (ret) {
1356 fprintf(stderr, "[error] Error creating AST\n");
1357 goto end;
1358 }
1359
1360 if (babeltrace_debug) {
1361 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1362 if (ret) {
1363 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1364 goto end;
1365 }
1366 }
1367
1368 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1369 if (ret) {
1370 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1371 goto end;
1372 }
1373 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1374 td, td->byte_order);
1375 if (ret) {
1376 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1377 goto end;
1378 }
1379 end:
1380 if (fp) {
1381 closeret = fclose(fp);
1382 if (closeret) {
1383 perror("Error on fclose");
1384 }
1385 }
1386 end_stream:
1387 if (metadata_stream->pos.fd >= 0) {
1388 closeret = close(metadata_stream->pos.fd);
1389 if (closeret) {
1390 perror("Error on metadata stream fd close");
1391 }
1392 }
1393 end_free:
1394 if (ret)
1395 g_free(metadata_stream);
1396 return ret;
1397 }
1398
1399 static
1400 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1401 struct ctf_stream_definition *stream,
1402 struct ctf_event_declaration *event)
1403 {
1404 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1405
1406 if (event->context_decl) {
1407 struct bt_definition *definition =
1408 event->context_decl->p.definition_new(&event->context_decl->p,
1409 stream->parent_def_scope, 0, 0, "event.context");
1410 if (!definition) {
1411 goto error;
1412 }
1413 stream_event->event_context = container_of(definition,
1414 struct definition_struct, p);
1415 stream->parent_def_scope = stream_event->event_context->p.scope;
1416 }
1417 if (event->fields_decl) {
1418 struct bt_definition *definition =
1419 event->fields_decl->p.definition_new(&event->fields_decl->p,
1420 stream->parent_def_scope, 0, 0, "event.fields");
1421 if (!definition) {
1422 goto error;
1423 }
1424 stream_event->event_fields = container_of(definition,
1425 struct definition_struct, p);
1426 stream->parent_def_scope = stream_event->event_fields->p.scope;
1427 }
1428 stream_event->stream = stream;
1429 return stream_event;
1430
1431 error:
1432 if (stream_event->event_fields)
1433 bt_definition_unref(&stream_event->event_fields->p);
1434 if (stream_event->event_context)
1435 bt_definition_unref(&stream_event->event_context->p);
1436 fprintf(stderr, "[error] Unable to create event definition for event \"%s\".\n",
1437 g_quark_to_string(event->name));
1438 return NULL;
1439 }
1440
1441 static
1442 int copy_event_declarations_stream_class_to_stream(struct ctf_trace *td,
1443 struct ctf_stream_declaration *stream_class,
1444 struct ctf_stream_definition *stream)
1445 {
1446 size_t def_size, class_size, i;
1447 int ret = 0;
1448
1449 def_size = stream->events_by_id->len;
1450 class_size = stream_class->events_by_id->len;
1451
1452 g_ptr_array_set_size(stream->events_by_id, class_size);
1453 for (i = def_size; i < class_size; i++) {
1454 struct ctf_event_declaration *event =
1455 g_ptr_array_index(stream_class->events_by_id, i);
1456 struct ctf_event_definition *stream_event;
1457
1458 if (!event)
1459 continue;
1460 stream_event = create_event_definitions(td, stream, event);
1461 if (!stream_event) {
1462 ret = -EINVAL;
1463 goto error;
1464 }
1465 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1466 }
1467 error:
1468 return ret;
1469 }
1470
1471 static
1472 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1473 {
1474 struct ctf_stream_declaration *stream_class;
1475 int ret;
1476 int i;
1477
1478 if (stream->stream_definitions_created)
1479 return 0;
1480
1481 stream_class = stream->stream_class;
1482
1483 if (stream_class->packet_context_decl) {
1484 struct bt_definition *definition =
1485 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1486 stream->parent_def_scope, 0, 0, "stream.packet.context");
1487 if (!definition) {
1488 ret = -EINVAL;
1489 goto error;
1490 }
1491 stream->stream_packet_context = container_of(definition,
1492 struct definition_struct, p);
1493 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1494 }
1495 if (stream_class->event_header_decl) {
1496 struct bt_definition *definition =
1497 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1498 stream->parent_def_scope, 0, 0, "stream.event.header");
1499 if (!definition) {
1500 ret = -EINVAL;
1501 goto error;
1502 }
1503 stream->stream_event_header =
1504 container_of(definition, struct definition_struct, p);
1505 stream->parent_def_scope = stream->stream_event_header->p.scope;
1506 }
1507 if (stream_class->event_context_decl) {
1508 struct bt_definition *definition =
1509 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1510 stream->parent_def_scope, 0, 0, "stream.event.context");
1511 if (!definition) {
1512 ret = -EINVAL;
1513 goto error;
1514 }
1515 stream->stream_event_context =
1516 container_of(definition, struct definition_struct, p);
1517 stream->parent_def_scope = stream->stream_event_context->p.scope;
1518 }
1519 stream->events_by_id = g_ptr_array_new();
1520 ret = copy_event_declarations_stream_class_to_stream(td,
1521 stream_class, stream);
1522 if (ret)
1523 goto error_event;
1524 return 0;
1525
1526 error_event:
1527 for (i = 0; i < stream->events_by_id->len; i++) {
1528 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1529 if (stream_event)
1530 g_free(stream_event);
1531 }
1532 g_ptr_array_free(stream->events_by_id, TRUE);
1533 error:
1534 if (stream->stream_event_context)
1535 bt_definition_unref(&stream->stream_event_context->p);
1536 if (stream->stream_event_header)
1537 bt_definition_unref(&stream->stream_event_header->p);
1538 if (stream->stream_packet_context)
1539 bt_definition_unref(&stream->stream_packet_context->p);
1540 fprintf(stderr, "[error] Unable to create stream (%" PRIu64 ") definitions: %s\n",
1541 stream_class->stream_id, strerror(-ret));
1542 return ret;
1543 }
1544
1545 static
1546 int stream_assign_class(struct ctf_trace *td,
1547 struct ctf_file_stream *file_stream,
1548 uint64_t stream_id)
1549 {
1550 struct ctf_stream_declaration *stream;
1551 int ret;
1552
1553 file_stream->parent.stream_id = stream_id;
1554 if (stream_id >= td->streams->len) {
1555 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1556 return -EINVAL;
1557 }
1558 stream = g_ptr_array_index(td->streams, stream_id);
1559 if (!stream) {
1560 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1561 return -EINVAL;
1562 }
1563 file_stream->parent.stream_class = stream;
1564 ret = create_stream_definitions(td, &file_stream->parent);
1565 if (ret)
1566 return ret;
1567 return 0;
1568 }
1569
1570 static
1571 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1572 struct ctf_trace *td,
1573 struct ctf_file_stream *file_stream,
1574 size_t filesize)
1575 {
1576 struct packet_index packet_index;
1577 uint64_t stream_id = 0;
1578 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1579 int first_packet = 0;
1580 int len_index;
1581 int ret;
1582
1583 begin:
1584 memset(&packet_index, 0, sizeof(packet_index));
1585 if (!pos->mmap_offset) {
1586 first_packet = 1;
1587 }
1588
1589 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1590 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1591 }
1592
1593 if (pos->base_mma) {
1594 /* unmap old base */
1595 ret = munmap_align(pos->base_mma);
1596 if (ret) {
1597 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1598 strerror(errno));
1599 return ret;
1600 }
1601 pos->base_mma = NULL;
1602 }
1603 /* map new base. Need mapping length from header. */
1604 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1605 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1606 assert(pos->base_mma != MAP_FAILED);
1607 /*
1608 * Use current mapping size as temporary content and packet
1609 * size.
1610 */
1611 pos->content_size = packet_map_len;
1612 pos->packet_size = packet_map_len;
1613 pos->offset = 0; /* Position of the packet header */
1614
1615 packet_index.offset = pos->mmap_offset;
1616
1617 /* read and check header, set stream id (and check) */
1618 if (file_stream->parent.trace_packet_header) {
1619 /* Read packet header */
1620 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1621 if (ret) {
1622 if (ret == -EFAULT)
1623 goto retry;
1624 fprintf(stderr, "[error] Unable to read packet header: %s\n", strerror(-ret));
1625 return ret;
1626 }
1627 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1628 if (len_index >= 0) {
1629 struct bt_definition *field;
1630 uint64_t magic;
1631
1632 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1633 magic = bt_get_unsigned_int(field);
1634 if (magic != CTF_MAGIC) {
1635 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1636 magic,
1637 file_stream->pos.packet_index->len,
1638 (ssize_t) pos->mmap_offset);
1639 return -EINVAL;
1640 }
1641 }
1642
1643 /* check uuid */
1644 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1645 if (len_index >= 0) {
1646 struct definition_array *defarray;
1647 struct bt_definition *field;
1648 uint64_t i;
1649 uint8_t uuidval[BABELTRACE_UUID_LEN];
1650
1651 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1652 assert(field->declaration->id == CTF_TYPE_ARRAY);
1653 defarray = container_of(field, struct definition_array, p);
1654 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1655
1656 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1657 struct bt_definition *elem;
1658
1659 elem = bt_array_index(defarray, i);
1660 uuidval[i] = bt_get_unsigned_int(elem);
1661 }
1662 ret = bt_uuid_compare(td->uuid, uuidval);
1663 if (ret) {
1664 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1665 return -EINVAL;
1666 }
1667 }
1668
1669 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1670 if (len_index >= 0) {
1671 struct bt_definition *field;
1672
1673 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1674 stream_id = bt_get_unsigned_int(field);
1675 }
1676 }
1677
1678 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1679 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1680 stream_id,
1681 file_stream->parent.stream_id);
1682 return -EINVAL;
1683 }
1684 if (first_packet) {
1685 ret = stream_assign_class(td, file_stream, stream_id);
1686 if (ret)
1687 return ret;
1688 }
1689
1690 if (file_stream->parent.stream_packet_context) {
1691 /* Read packet context */
1692 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1693 if (ret) {
1694 if (ret == -EFAULT)
1695 goto retry;
1696 fprintf(stderr, "[error] Unable to read packet context: %s\n", strerror(-ret));
1697 return ret;
1698 }
1699 /* read packet size from header */
1700 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
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.packet_size = bt_get_unsigned_int(field);
1706 } else {
1707 /* Use file size for packet size */
1708 packet_index.packet_size = filesize * CHAR_BIT;
1709 }
1710
1711 /* read content size from header */
1712 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1713 if (len_index >= 0) {
1714 struct bt_definition *field;
1715
1716 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1717 packet_index.content_size = bt_get_unsigned_int(field);
1718 } else {
1719 /* Use packet size if non-zero, else file size */
1720 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1721 }
1722
1723 /* read timestamp begin from header */
1724 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1725 if (len_index >= 0) {
1726 struct bt_definition *field;
1727
1728 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1729 packet_index.ts_cycles.timestamp_begin = bt_get_unsigned_int(field);
1730 if (file_stream->parent.stream_class->trace->parent.collection) {
1731 packet_index.ts_real.timestamp_begin =
1732 ctf_get_real_timestamp(
1733 &file_stream->parent,
1734 packet_index.ts_cycles.timestamp_begin);
1735 }
1736 }
1737
1738 /* read timestamp end from header */
1739 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1740 if (len_index >= 0) {
1741 struct bt_definition *field;
1742
1743 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1744 packet_index.ts_cycles.timestamp_end = bt_get_unsigned_int(field);
1745 if (file_stream->parent.stream_class->trace->parent.collection) {
1746 packet_index.ts_real.timestamp_end =
1747 ctf_get_real_timestamp(
1748 &file_stream->parent,
1749 packet_index.ts_cycles.timestamp_end);
1750 }
1751 }
1752
1753 /* read events discarded from header */
1754 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1755 if (len_index >= 0) {
1756 struct bt_definition *field;
1757
1758 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1759 packet_index.events_discarded = bt_get_unsigned_int(field);
1760 packet_index.events_discarded_len = bt_get_int_len(field);
1761 }
1762
1763 /* read packet_seq_num from header */
1764 len_index = bt_struct_declaration_lookup_field_index(
1765 file_stream->parent.stream_packet_context->declaration,
1766 g_quark_from_static_string("packet_seq_num"));
1767 if (len_index >= 0) {
1768 struct bt_definition *field;
1769
1770 field = bt_struct_definition_get_field_from_index(
1771 file_stream->parent.stream_packet_context,
1772 len_index);
1773 packet_index.packet_seq_num = bt_get_unsigned_int(field);
1774 }
1775 } else {
1776 /* Use file size for packet size */
1777 packet_index.packet_size = filesize * CHAR_BIT;
1778 /* Use packet size if non-zero, else file size */
1779 packet_index.content_size = packet_index.packet_size ? : filesize * CHAR_BIT;
1780 }
1781
1782 /* Validate content size and packet size values */
1783 if (packet_index.content_size > packet_index.packet_size) {
1784 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1785 packet_index.content_size, packet_index.packet_size);
1786 return -EINVAL;
1787 }
1788
1789 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1790 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1791 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1792 return -EINVAL;
1793 }
1794
1795 if (packet_index.content_size < pos->offset) {
1796 fprintf(stderr, "[error] Invalid CTF stream: content size is smaller than packet headers.\n");
1797 return -EINVAL;
1798 }
1799
1800 if ((packet_index.packet_size >> LOG2_CHAR_BIT) == 0) {
1801 fprintf(stderr, "[error] Invalid CTF stream: packet size needs to be at least one byte\n");
1802 return -EINVAL;
1803 }
1804
1805 /* Save position after header and context */
1806 packet_index.data_offset = pos->offset;
1807
1808 /* add index to packet array */
1809 g_array_append_val(file_stream->pos.packet_index, packet_index);
1810
1811 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1812
1813 return 0;
1814
1815 /* Retry with larger mapping */
1816 retry:
1817 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1818 /*
1819 * Reached EOF, but still expecting header/context data.
1820 */
1821 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1822 return -EFAULT;
1823 }
1824 /* Double the mapping len, and retry */
1825 tmp_map_len = packet_map_len << 1;
1826 if (tmp_map_len >> 1 != packet_map_len) {
1827 /* Overflow */
1828 fprintf(stderr, "[error] Packet mapping length overflow\n");
1829 return -EFAULT;
1830 }
1831 packet_map_len = tmp_map_len;
1832 goto begin;
1833 }
1834
1835 static
1836 int create_stream_packet_index(struct ctf_trace *td,
1837 struct ctf_file_stream *file_stream)
1838 {
1839 struct ctf_stream_pos *pos;
1840 struct stat filestats;
1841 int ret;
1842
1843 pos = &file_stream->pos;
1844
1845 ret = fstat(pos->fd, &filestats);
1846 if (ret < 0)
1847 return ret;
1848
1849 /* Deal with empty files */
1850 if (!filestats.st_size) {
1851 if (file_stream->parent.trace_packet_header
1852 || file_stream->parent.stream_packet_context) {
1853 /*
1854 * We expect a trace packet header and/or stream packet
1855 * context. Since a trace needs to have at least one
1856 * packet, empty files are therefore not accepted.
1857 */
1858 fprintf(stderr, "[error] Encountered an empty file, but expecting a trace packet header.\n");
1859 return -EINVAL;
1860 } else {
1861 /*
1862 * Without trace packet header nor stream packet
1863 * context, a one-packet trace can indeed be empty. This
1864 * is only valid if there is only one stream class: 0.
1865 */
1866 ret = stream_assign_class(td, file_stream, 0);
1867 if (ret)
1868 return ret;
1869 return 0;
1870 }
1871 }
1872
1873 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1874 ret = create_stream_one_packet_index(pos, td, file_stream,
1875 filestats.st_size);
1876 if (ret)
1877 return ret;
1878 }
1879 return 0;
1880 }
1881
1882 static
1883 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1884 {
1885 int ret;
1886
1887 if (td->packet_header_decl) {
1888 struct bt_definition *definition =
1889 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1890 stream->parent_def_scope, 0, 0, "trace.packet.header");
1891 if (!definition) {
1892 ret = -EINVAL;
1893 goto error;
1894 }
1895 stream->trace_packet_header =
1896 container_of(definition, struct definition_struct, p);
1897 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1898 }
1899
1900 return 0;
1901
1902 error:
1903 fprintf(stderr, "[error] Unable to create trace definitions: %s\n", strerror(-ret));
1904 return ret;
1905 }
1906
1907 static
1908 int import_stream_packet_index(struct ctf_trace *td,
1909 struct ctf_file_stream *file_stream)
1910 {
1911 struct ctf_stream_pos *pos;
1912 struct ctf_packet_index *ctf_index = NULL;
1913 struct ctf_packet_index_file_hdr index_hdr;
1914 struct packet_index index;
1915 uint32_t packet_index_len, index_minor;
1916 int ret = 0;
1917 int first_packet = 1;
1918 size_t len;
1919
1920 pos = &file_stream->pos;
1921
1922 len = fread(&index_hdr, sizeof(index_hdr), 1, pos->index_fp);
1923 if (len != 1) {
1924 perror("read index file header");
1925 goto error;
1926 }
1927
1928 /* Check the index header */
1929 if (be32toh(index_hdr.magic) != CTF_INDEX_MAGIC) {
1930 fprintf(stderr, "[error] wrong index magic\n");
1931 ret = -1;
1932 goto error;
1933 }
1934 if (be32toh(index_hdr.index_major) != CTF_INDEX_MAJOR) {
1935 fprintf(stderr, "[error] Incompatible index file %" PRIu32
1936 ".%" PRIu32 ", supported %d.%d\n",
1937 be32toh(index_hdr.index_major),
1938 be32toh(index_hdr.index_minor), CTF_INDEX_MAJOR,
1939 CTF_INDEX_MINOR);
1940 ret = -1;
1941 goto error;
1942 }
1943 index_minor = be32toh(index_hdr.index_minor);
1944
1945 packet_index_len = be32toh(index_hdr.packet_index_len);
1946 if (packet_index_len == 0) {
1947 fprintf(stderr, "[error] Packet index length cannot be 0.\n");
1948 ret = -1;
1949 goto error;
1950 }
1951 /*
1952 * Allocate the index length found in header, not internal
1953 * representation.
1954 */
1955 ctf_index = g_malloc0(packet_index_len);
1956 while (fread(ctf_index, packet_index_len, 1,
1957 pos->index_fp) == 1) {
1958 uint64_t stream_id;
1959 struct ctf_stream_declaration *stream = NULL;
1960
1961 memset(&index, 0, sizeof(index));
1962 index.offset = be64toh(ctf_index->offset);
1963 index.packet_size = be64toh(ctf_index->packet_size);
1964 index.content_size = be64toh(ctf_index->content_size);
1965 index.ts_cycles.timestamp_begin = be64toh(ctf_index->timestamp_begin);
1966 index.ts_cycles.timestamp_end = be64toh(ctf_index->timestamp_end);
1967 index.events_discarded = be64toh(ctf_index->events_discarded);
1968 index.events_discarded_len = 64;
1969 index.data_offset = -1;
1970 stream_id = be64toh(ctf_index->stream_id);
1971 if (index_minor >= 1) {
1972 index.stream_instance_id = be64toh(ctf_index->stream_instance_id);
1973 index.packet_seq_num = be64toh(ctf_index->packet_seq_num);
1974 }
1975
1976 if (!first_packet) {
1977 /* add index to packet array */
1978 g_array_append_val(file_stream->pos.packet_index, index);
1979 continue;
1980 }
1981
1982 file_stream->parent.stream_id = stream_id;
1983 if (stream_id < td->streams->len) {
1984 stream = g_ptr_array_index(td->streams, stream_id);
1985 }
1986 if (!stream) {
1987 fprintf(stderr, "[error] Stream %" PRIu64
1988 " is not declared in metadata.\n",
1989 stream_id);
1990 ret = -EINVAL;
1991 goto error;
1992 }
1993 file_stream->parent.stream_class = stream;
1994 ret = create_stream_definitions(td, &file_stream->parent);
1995 if (ret)
1996 goto error;
1997 first_packet = 0;
1998 /* add index to packet array */
1999 g_array_append_val(file_stream->pos.packet_index, index);
2000 }
2001
2002 /* Index containing only the header. */
2003 if (!file_stream->parent.stream_class) {
2004 ret = -1;
2005 goto error;
2006 }
2007
2008 ret = 0;
2009
2010 error:
2011 g_free(ctf_index);
2012 return ret;
2013 }
2014
2015 /*
2016 * Note: many file streams can inherit from the same stream class
2017 * description (metadata).
2018 */
2019 static
2020 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
2021 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2022 int whence))
2023 {
2024 int ret, fd, closeret;
2025 struct ctf_file_stream *file_stream;
2026 struct stat statbuf;
2027 char *index_name;
2028
2029 fd = openat(td->dirfd, path, flags);
2030 if (fd < 0) {
2031 perror("File stream openat()");
2032 ret = fd;
2033 goto error;
2034 }
2035
2036 /* Don't try to mmap subdirectories. Skip them, return success. */
2037 ret = fstat(fd, &statbuf);
2038 if (ret) {
2039 perror("File stream fstat()");
2040 goto fstat_error;
2041 }
2042 if (S_ISDIR(statbuf.st_mode)) {
2043 if (strncmp(path, "index", 5) != 0) {
2044 fprintf(stderr, "[warning] Skipping directory '%s' "
2045 "found in trace\n", path);
2046 }
2047 ret = 0;
2048 goto fd_is_dir_ok;
2049 }
2050 if (!statbuf.st_size) {
2051 /** Skip empty files. */
2052 ret = 0;
2053 goto fd_is_empty_file;
2054 }
2055
2056 file_stream = g_new0(struct ctf_file_stream, 1);
2057 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2058 file_stream->pos.fd = -1;
2059 file_stream->pos.index_fp = NULL;
2060
2061 strncpy(file_stream->parent.path, path, PATH_MAX);
2062 file_stream->parent.path[PATH_MAX - 1] = '\0';
2063
2064 if (packet_seek) {
2065 file_stream->pos.packet_seek = packet_seek;
2066 } else {
2067 fprintf(stderr, "[error] packet_seek function undefined.\n");
2068 ret = -1;
2069 goto error_def;
2070 }
2071
2072 ret = ctf_init_pos(&file_stream->pos, &td->parent, fd, flags);
2073 if (ret)
2074 goto error_def;
2075 ret = create_trace_definitions(td, &file_stream->parent);
2076 if (ret)
2077 goto error_def;
2078 /*
2079 * For now, only a single clock per trace is supported.
2080 */
2081 file_stream->parent.current_clock = td->parent.single_clock;
2082
2083 /*
2084 * Allocate the index name for this stream and try to open it.
2085 */
2086 index_name = malloc((strlen(path) + sizeof(INDEX_PATH)) * sizeof(char));
2087 if (!index_name) {
2088 fprintf(stderr, "[error] Cannot allocate index filename\n");
2089 ret = -ENOMEM;
2090 goto error_def;
2091 }
2092 snprintf(index_name, strlen(path) + sizeof(INDEX_PATH),
2093 INDEX_PATH, path);
2094
2095 if (bt_faccessat(td->dirfd, td->parent.path, index_name, O_RDONLY, 0) < 0) {
2096 ret = create_stream_packet_index(td, file_stream);
2097 if (ret) {
2098 fprintf(stderr, "[error] Stream index creation error.\n");
2099 goto error_index;
2100 }
2101 } else {
2102 ret = openat(td->dirfd, index_name, flags);
2103 if (ret < 0) {
2104 perror("Index file openat()");
2105 ret = -1;
2106 goto error_free;
2107 }
2108 file_stream->pos.index_fp = fdopen(ret, "r");
2109 if (!file_stream->pos.index_fp) {
2110 perror("fdopen() error");
2111 goto error_free;
2112 }
2113 ret = import_stream_packet_index(td, file_stream);
2114 if (ret) {
2115 ret = -1;
2116 goto error_index;
2117 }
2118 ret = fclose(file_stream->pos.index_fp);
2119 if (ret < 0) {
2120 perror("close index");
2121 goto error_free;
2122 }
2123 }
2124 free(index_name);
2125
2126 /* Add stream file to stream class */
2127 g_ptr_array_add(file_stream->parent.stream_class->streams,
2128 &file_stream->parent);
2129 return 0;
2130
2131 error_index:
2132 if (file_stream->pos.index_fp) {
2133 ret = fclose(file_stream->pos.index_fp);
2134 if (ret < 0) {
2135 perror("close index");
2136 }
2137 }
2138 if (file_stream->parent.trace_packet_header)
2139 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2140 error_free:
2141 free(index_name);
2142 error_def:
2143 closeret = ctf_fini_pos(&file_stream->pos);
2144 if (closeret) {
2145 fprintf(stderr, "Error on ctf_fini_pos\n");
2146 }
2147 g_free(file_stream);
2148 fd_is_empty_file:
2149 fd_is_dir_ok:
2150 fstat_error:
2151 closeret = close(fd);
2152 if (closeret) {
2153 perror("Error on fd close");
2154 }
2155 error:
2156 return ret;
2157 }
2158
2159 static
2160 int ctf_open_trace_read(struct ctf_trace *td,
2161 const char *path, int flags,
2162 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2163 int whence), FILE *metadata_fp)
2164 {
2165 struct ctf_scanner *scanner;
2166 int ret, closeret;
2167 struct dirent *dirent;
2168 struct dirent *diriter;
2169 size_t dirent_len;
2170 int pc_name_max;
2171 char *ext;
2172
2173 td->flags = flags;
2174
2175 /* Open trace directory */
2176 td->dir = opendir(path);
2177 if (!td->dir) {
2178 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
2179 ret = -ENOENT;
2180 goto error;
2181 }
2182
2183 td->dirfd = open(path, 0);
2184 if (td->dirfd < 0) {
2185 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
2186 perror("Trace directory open");
2187 ret = -errno;
2188 goto error_dirfd;
2189 }
2190 strncpy(td->parent.path, path, sizeof(td->parent.path));
2191 td->parent.path[sizeof(td->parent.path) - 1] = '\0';
2192
2193 /*
2194 * Keep the metadata file separate.
2195 * Keep scanner object local to the open. We don't support
2196 * incremental metadata append for on-disk traces.
2197 */
2198 scanner = ctf_scanner_alloc();
2199 if (!scanner) {
2200 fprintf(stderr, "[error] Error allocating scanner\n");
2201 ret = -ENOMEM;
2202 goto error_metadata;
2203 }
2204 ret = ctf_trace_metadata_read(td, metadata_fp, scanner, 0);
2205 ctf_scanner_free(scanner);
2206 if (ret) {
2207 if (ret == -ENOENT) {
2208 fprintf(stderr, "[warning] Empty metadata.\n");
2209 }
2210 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
2211 goto error_metadata;
2212 }
2213
2214 /*
2215 * Open each stream: for each file, try to open, check magic
2216 * number, and get the stream ID to add to the right location in
2217 * the stream array.
2218 */
2219
2220 pc_name_max = fpathconf(td->dirfd, _PC_NAME_MAX);
2221 if (pc_name_max < 0) {
2222 perror("Error on fpathconf");
2223 fprintf(stderr, "[error] Failed to get _PC_NAME_MAX for path \"%s\".\n", path);
2224 ret = -1;
2225 goto error_metadata;
2226 }
2227
2228 dirent_len = offsetof(struct dirent, d_name) + pc_name_max + 1;
2229
2230 dirent = malloc(dirent_len);
2231
2232 for (;;) {
2233 ret = readdir_r(td->dir, dirent, &diriter);
2234 if (ret) {
2235 fprintf(stderr, "[error] Readdir error.\n");
2236 goto readdir_error;
2237 }
2238 if (!diriter)
2239 break;
2240 /* Ignore hidden files, ., .. and metadata. */
2241 if (!strncmp(diriter->d_name, ".", 1)
2242 || !strcmp(diriter->d_name, "..")
2243 || !strcmp(diriter->d_name, "metadata"))
2244 continue;
2245
2246 /* Ignore index files : *.idx */
2247 ext = strrchr(diriter->d_name, '.');
2248 if (ext && (!strcmp(ext, ".idx"))) {
2249 continue;
2250 }
2251
2252 ret = ctf_open_file_stream_read(td, diriter->d_name,
2253 flags, packet_seek);
2254 if (ret) {
2255 fprintf(stderr, "[error] Open file stream error.\n");
2256 goto readdir_error;
2257 }
2258 }
2259
2260 free(dirent);
2261 return 0;
2262
2263 readdir_error:
2264 free(dirent);
2265 error_metadata:
2266 closeret = close(td->dirfd);
2267 if (closeret) {
2268 perror("Error on fd close");
2269 }
2270 error_dirfd:
2271 closeret = closedir(td->dir);
2272 if (closeret) {
2273 perror("Error on closedir");
2274 }
2275 error:
2276 return ret;
2277 }
2278
2279 /*
2280 * ctf_open_trace: Open a CTF trace and index it.
2281 * Note that the user must seek the trace after the open (using the iterator)
2282 * since the index creation read it entirely.
2283 */
2284 static
2285 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
2286 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2287 int whence), FILE *metadata_fp)
2288 {
2289 struct ctf_trace *td;
2290 int ret;
2291
2292 /*
2293 * If packet_seek is NULL, we provide our default version.
2294 */
2295 if (!packet_seek)
2296 packet_seek = ctf_packet_seek;
2297
2298 td = g_new0(struct ctf_trace, 1);
2299
2300 switch (flags & O_ACCMODE) {
2301 case O_RDONLY:
2302 if (!path) {
2303 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
2304 goto error;
2305 }
2306 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
2307 if (ret)
2308 goto error;
2309 break;
2310 case O_RDWR:
2311 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
2312 goto error;
2313 default:
2314 fprintf(stderr, "[error] Incorrect open flags.\n");
2315 goto error;
2316 }
2317
2318 return &td->parent;
2319 error:
2320 g_free(td);
2321 return NULL;
2322 }
2323
2324 static
2325 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
2326 struct bt_mmap_stream *mmap_info)
2327 {
2328 pos->mmap_offset = 0;
2329 pos->packet_size = 0;
2330 pos->content_size = 0;
2331 pos->content_size_loc = NULL;
2332 pos->fd = mmap_info->fd;
2333 pos->base_mma = NULL;
2334 pos->offset = 0;
2335 pos->dummy = false;
2336 pos->cur_index = 0;
2337 pos->prot = PROT_READ;
2338 pos->flags = MAP_PRIVATE;
2339 pos->parent.rw_table = read_dispatch_table;
2340 pos->parent.event_cb = ctf_read_event;
2341 pos->priv = mmap_info->priv;
2342 pos->packet_index = g_array_new(FALSE, TRUE,
2343 sizeof(struct packet_index));
2344 }
2345
2346 static
2347 int prepare_mmap_stream_definition(struct ctf_trace *td,
2348 struct ctf_file_stream *file_stream,
2349 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2350 int whence))
2351 {
2352 struct ctf_stream_declaration *stream;
2353 uint64_t stream_id;
2354 int ret;
2355
2356 /* Ask for the first packet to get the stream_id. */
2357 packet_seek(&file_stream->pos.parent, 0, SEEK_SET);
2358 stream_id = file_stream->parent.stream_id;
2359 if (stream_id >= td->streams->len) {
2360 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2361 "in metadata.\n", stream_id);
2362 ret = -EINVAL;
2363 goto end;
2364 }
2365 stream = g_ptr_array_index(td->streams, stream_id);
2366 if (!stream) {
2367 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
2368 "in metadata.\n", stream_id);
2369 ret = -EINVAL;
2370 goto end;
2371 }
2372 file_stream->parent.stream_class = stream;
2373 ret = create_stream_definitions(td, &file_stream->parent);
2374 end:
2375 return ret;
2376 }
2377
2378 static
2379 int ctf_open_mmap_stream_read(struct ctf_trace *td,
2380 struct bt_mmap_stream *mmap_info,
2381 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2382 int whence))
2383 {
2384 int ret;
2385 struct ctf_file_stream *file_stream;
2386
2387 file_stream = g_new0(struct ctf_file_stream, 1);
2388 file_stream->parent.stream_id = -1ULL;
2389 file_stream->pos.last_offset = LAST_OFFSET_POISON;
2390 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
2391
2392 file_stream->pos.packet_seek = packet_seek;
2393
2394 ret = create_trace_definitions(td, &file_stream->parent);
2395 if (ret) {
2396 goto error_def;
2397 }
2398
2399 ret = prepare_mmap_stream_definition(td, file_stream, packet_seek);
2400 if (ret)
2401 goto error_index;
2402
2403 /*
2404 * For now, only a single clock per trace is supported.
2405 */
2406 file_stream->parent.current_clock = td->parent.single_clock;
2407
2408 /* Add stream file to stream class */
2409 g_ptr_array_add(file_stream->parent.stream_class->streams,
2410 &file_stream->parent);
2411 return 0;
2412
2413 error_index:
2414 if (file_stream->parent.trace_packet_header)
2415 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
2416 error_def:
2417 g_free(file_stream);
2418 return ret;
2419 }
2420
2421 static
2422 int ctf_open_mmap_trace_read(struct ctf_trace *td,
2423 struct bt_mmap_stream_list *mmap_list,
2424 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2425 int whence),
2426 FILE *metadata_fp)
2427 {
2428 int ret;
2429 struct bt_mmap_stream *mmap_info;
2430
2431 td->scanner = ctf_scanner_alloc();
2432 if (!td->scanner) {
2433 fprintf(stderr, "[error] Error allocating scanner\n");
2434 ret = -ENOMEM;
2435 goto error;
2436 }
2437 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 0);
2438 if (ret) {
2439 if (ret == -ENOENT) {
2440 fprintf(stderr, "[warning] Empty metadata.\n");
2441 }
2442 goto error;
2443 }
2444
2445 /*
2446 * for each stream, try to open, check magic number, and get the
2447 * stream ID to add to the right location in the stream array.
2448 */
2449 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
2450 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
2451 if (ret) {
2452 fprintf(stderr, "[error] Open file mmap stream error.\n");
2453 goto error;
2454 }
2455 }
2456 return 0;
2457
2458 error:
2459 ctf_scanner_free(td->scanner);
2460 return ret;
2461 }
2462
2463 static
2464 struct bt_trace_descriptor *ctf_open_mmap_trace(
2465 struct bt_mmap_stream_list *mmap_list,
2466 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
2467 int whence),
2468 FILE *metadata_fp)
2469 {
2470 struct ctf_trace *td;
2471 int ret;
2472
2473 if (!metadata_fp) {
2474 fprintf(stderr, "[error] No metadata file pointer associated, "
2475 "required for mmap parsing\n");
2476 goto error;
2477 }
2478 if (!packet_seek) {
2479 fprintf(stderr, "[error] packet_seek function undefined.\n");
2480 goto error;
2481 }
2482 td = g_new0(struct ctf_trace, 1);
2483 td->dirfd = -1;
2484 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
2485 if (ret)
2486 goto error_free;
2487
2488 return &td->parent;
2489
2490 error_free:
2491 g_free(td);
2492 error:
2493 return NULL;
2494 }
2495
2496 int ctf_append_trace_metadata(struct bt_trace_descriptor *tdp,
2497 FILE *metadata_fp)
2498 {
2499 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2500 int i, j;
2501 int ret;
2502
2503 if (!td->scanner)
2504 return -EINVAL;
2505 ret = ctf_trace_metadata_read(td, metadata_fp, td->scanner, 1);
2506 if (ret)
2507 return ret;
2508 /* for each stream_class */
2509 for (i = 0; i < td->streams->len; i++) {
2510 struct ctf_stream_declaration *stream_class;
2511
2512 stream_class = g_ptr_array_index(td->streams, i);
2513 if (!stream_class)
2514 continue;
2515 /* for each stream */
2516 for (j = 0; j < stream_class->streams->len; j++) {
2517 struct ctf_stream_definition *stream;
2518
2519 stream = g_ptr_array_index(stream_class->streams, j);
2520 if (!stream)
2521 continue;
2522 ret = copy_event_declarations_stream_class_to_stream(td,
2523 stream_class, stream);
2524 if (ret)
2525 return ret;
2526 }
2527 }
2528 return 0;
2529 }
2530
2531 static
2532 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
2533 {
2534 int i, j, k;
2535 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2536
2537 /* for each stream_class */
2538 for (i = 0; i < td->streams->len; i++) {
2539 struct ctf_stream_declaration *stream_class;
2540
2541 stream_class = g_ptr_array_index(td->streams, i);
2542 if (!stream_class)
2543 continue;
2544 /* for each file_stream */
2545 for (j = 0; j < stream_class->streams->len; j++) {
2546 struct ctf_stream_definition *stream;
2547 struct ctf_stream_pos *stream_pos;
2548 struct ctf_file_stream *cfs;
2549
2550 stream = g_ptr_array_index(stream_class->streams, j);
2551 if (!stream)
2552 continue;
2553 cfs = container_of(stream, struct ctf_file_stream,
2554 parent);
2555 stream_pos = &cfs->pos;
2556 if (!stream_pos->packet_index)
2557 continue;
2558
2559 for (k = 0; k < stream_pos->packet_index->len; k++) {
2560 struct packet_index *index;
2561
2562 index = &g_array_index(stream_pos->packet_index,
2563 struct packet_index, k);
2564 index->ts_real.timestamp_begin =
2565 ctf_get_real_timestamp(stream,
2566 index->ts_cycles.timestamp_begin);
2567 index->ts_real.timestamp_end =
2568 ctf_get_real_timestamp(stream,
2569 index->ts_cycles.timestamp_end);
2570 }
2571 }
2572 }
2573 return 0;
2574 }
2575
2576 static
2577 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2578 {
2579 int ret;
2580
2581 ret = ctf_fini_pos(&file_stream->pos);
2582 if (ret) {
2583 fprintf(stderr, "Error on ctf_fini_pos\n");
2584 return -1;
2585 }
2586 if (file_stream->pos.fd >= 0) {
2587 ret = close(file_stream->pos.fd);
2588 if (ret) {
2589 perror("Error closing file fd");
2590 return -1;
2591 }
2592 }
2593 return 0;
2594 }
2595
2596 static
2597 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2598 {
2599 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2600 int ret;
2601
2602 if (td->streams) {
2603 int i;
2604
2605 for (i = 0; i < td->streams->len; i++) {
2606 struct ctf_stream_declaration *stream;
2607 int j;
2608
2609 stream = g_ptr_array_index(td->streams, i);
2610 if (!stream)
2611 continue;
2612 for (j = 0; j < stream->streams->len; j++) {
2613 struct ctf_file_stream *file_stream;
2614 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2615 struct ctf_file_stream, parent);
2616 ret = ctf_close_file_stream(file_stream);
2617 if (ret)
2618 return ret;
2619 }
2620 }
2621 }
2622 ctf_destroy_metadata(td);
2623 ctf_scanner_free(td->scanner);
2624 if (td->dirfd >= 0) {
2625 ret = close(td->dirfd);
2626 if (ret) {
2627 perror("Error closing dirfd");
2628 return ret;
2629 }
2630 }
2631 if (td->dir) {
2632 ret = closedir(td->dir);
2633 if (ret) {
2634 perror("Error closedir");
2635 return ret;
2636 }
2637 }
2638 free(td->metadata_string);
2639 g_free(td);
2640 return 0;
2641 }
2642
2643 static
2644 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2645 struct bt_context *ctx)
2646 {
2647 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2648 parent);
2649
2650 td->parent.ctx = ctx;
2651 }
2652
2653 static
2654 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2655 struct bt_trace_handle *handle)
2656 {
2657 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2658 parent);
2659
2660 td->parent.handle = handle;
2661 }
2662
2663 static
2664 void __attribute__((constructor)) ctf_init(void)
2665 {
2666 int ret;
2667
2668 ctf_format.name = g_quark_from_static_string("ctf");
2669 ret = bt_register_format(&ctf_format);
2670 assert(!ret);
2671 }
2672
2673 static
2674 void __attribute__((destructor)) ctf_exit(void)
2675 {
2676 bt_unregister_format(&ctf_format);
2677 }
This page took 0.079755 seconds and 5 git commands to generate.