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