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