552133c3238719ecc63a1acfb2fa3f27831b24ab
[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/uuid.h>
37 #include <babeltrace/endian.h>
38 #include <inttypes.h>
39 #include <stdio.h>
40 #include <sys/mman.h>
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <dirent.h>
46 #include <glib.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49
50 #include "metadata/ctf-scanner.h"
51 #include "metadata/ctf-parser.h"
52 #include "metadata/ctf-ast.h"
53 #include "events-private.h"
54 #include "memstream.h"
55
56 /*
57 * We currently simply map a page to read the packet header and packet
58 * context to get the packet length and content length. (in bits)
59 */
60 #define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
61 #define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
62
63 #ifndef min
64 #define min(a, b) (((a) < (b)) ? (a) : (b))
65 #endif
66
67 #define NSEC_PER_SEC 1000000000ULL
68
69 int opt_clock_cycles,
70 opt_clock_seconds,
71 opt_clock_date,
72 opt_clock_gmt;
73
74 uint64_t opt_clock_offset;
75
76 extern int yydebug;
77
78 static
79 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
80 void (*packet_seek)(struct stream_pos *pos, size_t index,
81 int whence),
82 FILE *metadata_fp);
83 static
84 struct trace_descriptor *ctf_open_mmap_trace(
85 struct mmap_stream_list *mmap_list,
86 void (*packet_seek)(struct stream_pos *pos, size_t index,
87 int whence),
88 FILE *metadata_fp);
89 static
90 void ctf_set_context(struct trace_descriptor *descriptor,
91 struct bt_context *ctx);
92 static
93 void ctf_set_handle(struct trace_descriptor *descriptor,
94 struct bt_trace_handle *handle);
95
96 static
97 int ctf_close_trace(struct trace_descriptor *descriptor);
98 static
99 uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
100 struct bt_trace_handle *handle, enum bt_clock_type type);
101 static
102 uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
103 struct bt_trace_handle *handle, enum bt_clock_type type);
104 static
105 int ctf_convert_index_timestamp(struct trace_descriptor *tdp);
106
107 static
108 rw_dispatch read_dispatch_table[] = {
109 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
110 [ CTF_TYPE_FLOAT ] = ctf_float_read,
111 [ CTF_TYPE_ENUM ] = ctf_enum_read,
112 [ CTF_TYPE_STRING ] = ctf_string_read,
113 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
114 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
115 [ CTF_TYPE_ARRAY ] = ctf_array_read,
116 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
117 };
118
119 static
120 rw_dispatch write_dispatch_table[] = {
121 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
122 [ CTF_TYPE_FLOAT ] = ctf_float_write,
123 [ CTF_TYPE_ENUM ] = ctf_enum_write,
124 [ CTF_TYPE_STRING ] = ctf_string_write,
125 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
126 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
127 [ CTF_TYPE_ARRAY ] = ctf_array_write,
128 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
129 };
130
131 static
132 struct format ctf_format = {
133 .open_trace = ctf_open_trace,
134 .open_mmap_trace = ctf_open_mmap_trace,
135 .close_trace = ctf_close_trace,
136 .set_context = ctf_set_context,
137 .set_handle = ctf_set_handle,
138 .timestamp_begin = ctf_timestamp_begin,
139 .timestamp_end = ctf_timestamp_end,
140 .convert_index_timestamp = ctf_convert_index_timestamp,
141 };
142
143 static
144 uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
145 struct bt_trace_handle *handle, enum bt_clock_type type)
146 {
147 struct ctf_trace *tin;
148 uint64_t begin = ULLONG_MAX;
149 int i, j;
150
151 tin = container_of(descriptor, struct ctf_trace, parent);
152
153 if (!tin)
154 goto error;
155
156 /* for each stream_class */
157 for (i = 0; i < tin->streams->len; i++) {
158 struct ctf_stream_declaration *stream_class;
159
160 stream_class = g_ptr_array_index(tin->streams, i);
161 if (!stream_class)
162 continue;
163 /* for each file_stream */
164 for (j = 0; j < stream_class->streams->len; j++) {
165 struct ctf_stream_definition *stream;
166 struct ctf_file_stream *cfs;
167 struct ctf_stream_pos *stream_pos;
168 struct packet_index *index;
169
170 stream = g_ptr_array_index(stream_class->streams, j);
171 cfs = container_of(stream, struct ctf_file_stream,
172 parent);
173 stream_pos = &cfs->pos;
174
175 if (!stream_pos->packet_real_index)
176 goto error;
177
178 if (stream_pos->packet_real_index->len <= 0)
179 continue;
180
181 if (type == BT_CLOCK_REAL) {
182 index = &g_array_index(stream_pos->packet_real_index,
183 struct packet_index,
184 stream_pos->packet_real_index->len - 1);
185 } else if (type == BT_CLOCK_CYCLES) {
186 index = &g_array_index(stream_pos->packet_cycles_index,
187 struct packet_index,
188 stream_pos->packet_real_index->len - 1);
189
190 } else {
191 goto error;
192 }
193 if (index->timestamp_begin < begin)
194 begin = index->timestamp_begin;
195 }
196 }
197
198 return begin;
199
200 error:
201 return -1ULL;
202 }
203
204 static
205 uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
206 struct bt_trace_handle *handle, enum bt_clock_type type)
207 {
208 struct ctf_trace *tin;
209 uint64_t end = 0;
210 int i, j;
211
212 tin = container_of(descriptor, struct ctf_trace, parent);
213
214 if (!tin)
215 goto error;
216
217 /* for each stream_class */
218 for (i = 0; i < tin->streams->len; i++) {
219 struct ctf_stream_declaration *stream_class;
220
221 stream_class = g_ptr_array_index(tin->streams, i);
222 if (!stream_class)
223 continue;
224 /* for each file_stream */
225 for (j = 0; j < stream_class->streams->len; j++) {
226 struct ctf_stream_definition *stream;
227 struct ctf_file_stream *cfs;
228 struct ctf_stream_pos *stream_pos;
229 struct packet_index *index;
230
231 stream = g_ptr_array_index(stream_class->streams, j);
232 cfs = container_of(stream, struct ctf_file_stream,
233 parent);
234 stream_pos = &cfs->pos;
235
236 if (!stream_pos->packet_real_index)
237 goto error;
238
239 if (stream_pos->packet_real_index->len <= 0)
240 continue;
241
242 if (type == BT_CLOCK_REAL) {
243 index = &g_array_index(stream_pos->packet_real_index,
244 struct packet_index,
245 stream_pos->packet_real_index->len - 1);
246 } else if (type == BT_CLOCK_CYCLES) {
247 index = &g_array_index(stream_pos->packet_cycles_index,
248 struct packet_index,
249 stream_pos->packet_real_index->len - 1);
250
251 } else {
252 goto error;
253 }
254 if (index->timestamp_end > end)
255 end = index->timestamp_end;
256 }
257 }
258
259 return end;
260
261 error:
262 return -1ULL;
263 }
264
265 /*
266 * Update stream current timestamp
267 */
268 static
269 void ctf_update_timestamp(struct ctf_stream_definition *stream,
270 struct definition_integer *integer_definition)
271 {
272 struct declaration_integer *integer_declaration =
273 integer_definition->declaration;
274 uint64_t oldval, newval, updateval;
275
276 if (unlikely(integer_declaration->len == 64)) {
277 stream->prev_cycles_timestamp = stream->cycles_timestamp;
278 stream->cycles_timestamp = integer_definition->value._unsigned;
279 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
280 stream->prev_cycles_timestamp);
281 stream->real_timestamp = ctf_get_real_timestamp(stream,
282 stream->cycles_timestamp);
283 return;
284 }
285 /* keep low bits */
286 oldval = stream->cycles_timestamp;
287 oldval &= (1ULL << integer_declaration->len) - 1;
288 newval = integer_definition->value._unsigned;
289 /* Test for overflow by comparing low bits */
290 if (newval < oldval)
291 newval += 1ULL << integer_declaration->len;
292 /* updateval contains old high bits, and new low bits (sum) */
293 updateval = stream->cycles_timestamp;
294 updateval &= ~((1ULL << integer_declaration->len) - 1);
295 updateval += newval;
296 stream->prev_cycles_timestamp = stream->cycles_timestamp;
297 stream->cycles_timestamp = updateval;
298
299 /* convert to real timestamp */
300 stream->prev_real_timestamp = ctf_get_real_timestamp(stream,
301 stream->prev_cycles_timestamp);
302 stream->real_timestamp = ctf_get_real_timestamp(stream,
303 stream->cycles_timestamp);
304 }
305
306 /*
307 * Print timestamp, rescaling clock frequency to nanoseconds and
308 * applying offsets as needed (unix time).
309 */
310 static
311 void ctf_print_timestamp_real(FILE *fp,
312 struct ctf_stream_definition *stream,
313 uint64_t timestamp)
314 {
315 uint64_t ts_sec = 0, ts_nsec;
316
317 ts_nsec = timestamp;
318
319 /* Add command-line offset */
320 ts_sec += opt_clock_offset;
321
322 ts_sec += ts_nsec / NSEC_PER_SEC;
323 ts_nsec = ts_nsec % NSEC_PER_SEC;
324
325 if (!opt_clock_seconds) {
326 struct tm tm;
327 time_t time_s = (time_t) ts_sec;
328
329 if (!opt_clock_gmt) {
330 struct tm *res;
331
332 res = localtime_r(&time_s, &tm);
333 if (!res) {
334 fprintf(stderr, "[warning] Unable to get localtime.\n");
335 goto seconds;
336 }
337 } else {
338 struct tm *res;
339
340 res = gmtime_r(&time_s, &tm);
341 if (!res) {
342 fprintf(stderr, "[warning] Unable to get gmtime.\n");
343 goto seconds;
344 }
345 }
346 if (opt_clock_date) {
347 char timestr[26];
348 size_t res;
349
350 /* Print date and time */
351 res = strftime(timestr, sizeof(timestr),
352 "%F ", &tm);
353 if (!res) {
354 fprintf(stderr, "[warning] Unable to print ascii time.\n");
355 goto seconds;
356 }
357 fprintf(fp, "%s", timestr);
358 }
359 /* Print time in HH:MM:SS.ns */
360 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
361 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
362 goto end;
363 }
364 seconds:
365 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
366 ts_sec, ts_nsec);
367
368 end:
369 return;
370 }
371
372 /*
373 * Print timestamp, in cycles
374 */
375 static
376 void ctf_print_timestamp_cycles(FILE *fp,
377 struct ctf_stream_definition *stream,
378 uint64_t timestamp)
379 {
380 fprintf(fp, "%020" PRIu64, timestamp);
381 }
382
383 void ctf_print_timestamp(FILE *fp,
384 struct ctf_stream_definition *stream,
385 uint64_t timestamp)
386 {
387 if (opt_clock_cycles) {
388 ctf_print_timestamp_cycles(fp, stream, timestamp);
389 } else {
390 ctf_print_timestamp_real(fp, stream, timestamp);
391 }
392 }
393
394 static
395 int ctf_read_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
396 {
397 struct ctf_stream_pos *pos =
398 container_of(ppos, struct ctf_stream_pos, parent);
399 struct ctf_stream_declaration *stream_class = stream->stream_class;
400 struct ctf_event_definition *event;
401 uint64_t id = 0;
402 int ret;
403
404 /* We need to check for EOF here for empty files. */
405 if (unlikely(pos->offset == EOF))
406 return EOF;
407
408 ctf_pos_get_event(pos);
409
410 /* save the current position as a restore point */
411 pos->last_offset = pos->offset;
412
413 /*
414 * This is the EOF check after we've advanced the position in
415 * ctf_pos_get_event.
416 */
417 if (unlikely(pos->offset == EOF))
418 return EOF;
419 assert(pos->offset < pos->content_size);
420
421 /* Read event header */
422 if (likely(stream->stream_event_header)) {
423 struct definition_integer *integer_definition;
424 struct definition *variant;
425
426 ret = generic_rw(ppos, &stream->stream_event_header->p);
427 if (unlikely(ret))
428 goto error;
429 /* lookup event id */
430 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "id", FALSE);
431 if (integer_definition) {
432 id = integer_definition->value._unsigned;
433 } else {
434 struct definition_enum *enum_definition;
435
436 enum_definition = bt_lookup_enum(&stream->stream_event_header->p, "id", FALSE);
437 if (enum_definition) {
438 id = enum_definition->integer->value._unsigned;
439 }
440 }
441
442 variant = bt_lookup_variant(&stream->stream_event_header->p, "v");
443 if (variant) {
444 integer_definition = bt_lookup_integer(variant, "id", FALSE);
445 if (integer_definition) {
446 id = integer_definition->value._unsigned;
447 }
448 }
449 stream->event_id = id;
450
451 /* lookup timestamp */
452 stream->has_timestamp = 0;
453 integer_definition = bt_lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
454 if (integer_definition) {
455 ctf_update_timestamp(stream, integer_definition);
456 stream->has_timestamp = 1;
457 } else {
458 if (variant) {
459 integer_definition = bt_lookup_integer(variant, "timestamp", FALSE);
460 if (integer_definition) {
461 ctf_update_timestamp(stream, integer_definition);
462 stream->has_timestamp = 1;
463 }
464 }
465 }
466 }
467
468 /* Read stream-declared event context */
469 if (stream->stream_event_context) {
470 ret = generic_rw(ppos, &stream->stream_event_context->p);
471 if (ret)
472 goto error;
473 }
474
475 if (unlikely(id >= stream_class->events_by_id->len)) {
476 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
477 return -EINVAL;
478 }
479 event = g_ptr_array_index(stream->events_by_id, id);
480 if (unlikely(!event)) {
481 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
482 return -EINVAL;
483 }
484
485 /* Read event-declared event context */
486 if (event->event_context) {
487 ret = generic_rw(ppos, &event->event_context->p);
488 if (ret)
489 goto error;
490 }
491
492 /* Read event payload */
493 if (likely(event->event_fields)) {
494 ret = generic_rw(ppos, &event->event_fields->p);
495 if (ret)
496 goto error;
497 }
498
499 return 0;
500
501 error:
502 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
503 return ret;
504 }
505
506 static
507 int ctf_write_event(struct stream_pos *pos, struct ctf_stream_definition *stream)
508 {
509 struct ctf_stream_declaration *stream_class = stream->stream_class;
510 struct ctf_event_definition *event;
511 uint64_t id;
512 int ret;
513
514 id = stream->event_id;
515
516 /* print event header */
517 if (likely(stream->stream_event_header)) {
518 ret = generic_rw(pos, &stream->stream_event_header->p);
519 if (ret)
520 goto error;
521 }
522
523 /* print stream-declared event context */
524 if (stream->stream_event_context) {
525 ret = generic_rw(pos, &stream->stream_event_context->p);
526 if (ret)
527 goto error;
528 }
529
530 if (unlikely(id >= stream_class->events_by_id->len)) {
531 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
532 return -EINVAL;
533 }
534 event = g_ptr_array_index(stream->events_by_id, id);
535 if (unlikely(!event)) {
536 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
537 return -EINVAL;
538 }
539
540 /* print event-declared event context */
541 if (event->event_context) {
542 ret = generic_rw(pos, &event->event_context->p);
543 if (ret)
544 goto error;
545 }
546
547 /* Read and print event payload */
548 if (likely(event->event_fields)) {
549 ret = generic_rw(pos, &event->event_fields->p);
550 if (ret)
551 goto error;
552 }
553
554 return 0;
555
556 error:
557 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
558 return ret;
559 }
560
561 int ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
562 {
563 pos->fd = fd;
564 if (fd >= 0) {
565 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
566 sizeof(struct packet_index));
567 pos->packet_real_index = g_array_new(FALSE, TRUE,
568 sizeof(struct packet_index));
569 } else {
570 pos->packet_cycles_index = NULL;
571 pos->packet_real_index = NULL;
572 }
573 switch (open_flags & O_ACCMODE) {
574 case O_RDONLY:
575 pos->prot = PROT_READ;
576 pos->flags = MAP_PRIVATE;
577 pos->parent.rw_table = read_dispatch_table;
578 pos->parent.event_cb = ctf_read_event;
579 break;
580 case O_RDWR:
581 pos->prot = PROT_WRITE; /* Write has priority */
582 pos->flags = MAP_SHARED;
583 pos->parent.rw_table = write_dispatch_table;
584 pos->parent.event_cb = ctf_write_event;
585 if (fd >= 0)
586 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
587 break;
588 default:
589 assert(0);
590 }
591 return 0;
592 }
593
594 int ctf_fini_pos(struct ctf_stream_pos *pos)
595 {
596 if (pos->prot == PROT_WRITE && pos->content_size_loc)
597 *pos->content_size_loc = pos->offset;
598 if (pos->base_mma) {
599 int ret;
600
601 /* unmap old base */
602 ret = munmap_align(pos->base_mma);
603 if (ret) {
604 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
605 strerror(errno));
606 return -1;
607 }
608 }
609 if (pos->packet_cycles_index)
610 (void) g_array_free(pos->packet_cycles_index, TRUE);
611 if (pos->packet_real_index)
612 (void) g_array_free(pos->packet_real_index, TRUE);
613 return 0;
614 }
615
616 /*
617 * for SEEK_CUR: go to next packet.
618 * for SEEK_POS: go to packet numer (index).
619 */
620 void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
621 {
622 struct ctf_stream_pos *pos =
623 container_of(stream_pos, struct ctf_stream_pos, parent);
624 struct ctf_file_stream *file_stream =
625 container_of(pos, struct ctf_file_stream, pos);
626 int ret;
627 off_t off;
628 struct packet_index *packet_index;
629
630 if (pos->prot == PROT_WRITE && pos->content_size_loc)
631 *pos->content_size_loc = pos->offset;
632
633 if (pos->base_mma) {
634 /* unmap old base */
635 ret = munmap_align(pos->base_mma);
636 if (ret) {
637 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
638 strerror(errno));
639 assert(0);
640 }
641 pos->base_mma = NULL;
642 }
643
644 /*
645 * The caller should never ask for ctf_move_pos across packets,
646 * except to get exactly at the beginning of the next packet.
647 */
648 if (pos->prot == PROT_WRITE) {
649 switch (whence) {
650 case SEEK_CUR:
651 /* The writer will add padding */
652 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
653 break;
654 case SEEK_SET:
655 assert(index == 0); /* only seek supported for now */
656 pos->cur_index = 0;
657 break;
658 default:
659 assert(0);
660 }
661 pos->content_size = -1U; /* Unknown at this point */
662 pos->packet_size = WRITE_PACKET_LEN;
663 off = posix_fallocate(pos->fd, pos->mmap_offset,
664 pos->packet_size / CHAR_BIT);
665 assert(off >= 0);
666 pos->offset = 0;
667 } else {
668 read_next_packet:
669 switch (whence) {
670 case SEEK_CUR:
671 {
672 uint64_t events_discarded_diff;
673
674 if (pos->offset == EOF) {
675 return;
676 }
677 /* For printing discarded event count */
678 packet_index = &g_array_index(pos->packet_cycles_index,
679 struct packet_index, pos->cur_index);
680 file_stream->parent.prev_cycles_timestamp_end =
681 packet_index->timestamp_end;
682 file_stream->parent.prev_cycles_timestamp =
683 packet_index->timestamp_begin;
684
685 packet_index = &g_array_index(pos->packet_real_index,
686 struct packet_index, pos->cur_index);
687 file_stream->parent.prev_real_timestamp_end =
688 packet_index->timestamp_end;
689 file_stream->parent.prev_real_timestamp =
690 packet_index->timestamp_begin;
691
692 events_discarded_diff = packet_index->events_discarded;
693 if (pos->cur_index > 0) {
694 packet_index = &g_array_index(pos->packet_real_index,
695 struct packet_index,
696 pos->cur_index - 1);
697 events_discarded_diff -= packet_index->events_discarded;
698 /*
699 * Deal with 32-bit wrap-around if the
700 * tracer provided a 32-bit field.
701 */
702 if (packet_index->events_discarded_len == 32) {
703 events_discarded_diff = (uint32_t) events_discarded_diff;
704 }
705 }
706 file_stream->parent.events_discarded = events_discarded_diff;
707 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
708 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
709 /* The reader will expect us to skip padding */
710 ++pos->cur_index;
711 break;
712 }
713 case SEEK_SET:
714 packet_index = &g_array_index(pos->packet_cycles_index,
715 struct packet_index, index);
716 pos->last_events_discarded = packet_index->events_discarded;
717 pos->cur_index = index;
718 file_stream->parent.prev_real_timestamp = 0;
719 file_stream->parent.prev_real_timestamp_end = 0;
720 file_stream->parent.prev_cycles_timestamp = 0;
721 file_stream->parent.prev_cycles_timestamp_end = 0;
722 break;
723 default:
724 assert(0);
725 }
726 if (pos->cur_index >= pos->packet_real_index->len) {
727 /*
728 * When a stream reaches the end of the
729 * file, we need to show the number of
730 * events discarded ourselves, because
731 * there is no next event scheduled to
732 * be printed in the output.
733 */
734 if (file_stream->parent.events_discarded) {
735 /*
736 * We need to check if we are in trace
737 * read or called from packet indexing.
738 * In this last case, the collection is
739 * not there, so we cannot print the
740 * timestamps.
741 */
742 if ((&file_stream->parent)->stream_class->trace->collection) {
743 fflush(stdout);
744 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events at end of stream between [",
745 file_stream->parent.events_discarded);
746 if (opt_clock_cycles) {
747 ctf_print_timestamp(stderr,
748 &file_stream->parent,
749 file_stream->parent.prev_cycles_timestamp);
750 fprintf(stderr, "] and [");
751 ctf_print_timestamp(stderr, &file_stream->parent,
752 file_stream->parent.prev_cycles_timestamp_end);
753 } else {
754 ctf_print_timestamp(stderr,
755 &file_stream->parent,
756 file_stream->parent.prev_real_timestamp);
757 fprintf(stderr, "] and [");
758 ctf_print_timestamp(stderr, &file_stream->parent,
759 file_stream->parent.prev_real_timestamp_end);
760 }
761 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
762 fflush(stderr);
763 }
764 file_stream->parent.events_discarded = 0;
765 }
766 pos->offset = EOF;
767 return;
768 }
769 packet_index = &g_array_index(pos->packet_cycles_index,
770 struct packet_index,
771 pos->cur_index);
772 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
773
774 packet_index = &g_array_index(pos->packet_real_index,
775 struct packet_index,
776 pos->cur_index);
777 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
778 pos->mmap_offset = packet_index->offset;
779
780 /* Lookup context/packet size in index */
781 pos->content_size = packet_index->content_size;
782 pos->packet_size = packet_index->packet_size;
783 if (packet_index->data_offset < packet_index->content_size) {
784 pos->offset = 0; /* will read headers */
785 } else if (packet_index->data_offset == packet_index->content_size) {
786 /* empty packet */
787 pos->offset = packet_index->data_offset;
788 whence = SEEK_CUR;
789 goto read_next_packet;
790 } else {
791 pos->offset = EOF;
792 return;
793 }
794 }
795 /* map new base. Need mapping length from header. */
796 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
797 pos->flags, pos->fd, pos->mmap_offset);
798 if (pos->base_mma == MAP_FAILED) {
799 fprintf(stderr, "[error] mmap error %s.\n",
800 strerror(errno));
801 assert(0);
802 }
803
804 /* update trace_packet_header and stream_packet_context */
805 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
806 /* Read packet header */
807 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
808 assert(!ret);
809 }
810 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
811 /* Read packet context */
812 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
813 assert(!ret);
814 }
815 }
816
817 static
818 int packet_metadata(struct ctf_trace *td, FILE *fp)
819 {
820 uint32_t magic;
821 size_t len;
822 int ret = 0;
823
824 len = fread(&magic, sizeof(magic), 1, fp);
825 if (len != 1) {
826 goto end;
827 }
828 if (magic == TSDL_MAGIC) {
829 ret = 1;
830 td->byte_order = BYTE_ORDER;
831 CTF_TRACE_SET_FIELD(td, byte_order);
832 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
833 ret = 1;
834 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
835 LITTLE_ENDIAN : BIG_ENDIAN;
836 CTF_TRACE_SET_FIELD(td, byte_order);
837 }
838 end:
839 rewind(fp);
840 return ret;
841 }
842
843 /*
844 * Returns 0 on success, -1 on error.
845 */
846 static
847 int check_version(unsigned int major, unsigned int minor)
848 {
849 switch (major) {
850 case 1:
851 switch (minor) {
852 case 8:
853 return 0;
854 default:
855 goto warning;
856 }
857 default:
858 goto warning;
859
860 }
861
862 /* eventually return an error instead of warning */
863 warning:
864 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
865 major, minor);
866 return 0;
867 }
868
869 static
870 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
871 FILE *out)
872 {
873 struct metadata_packet_header header;
874 size_t readlen, writelen, toread;
875 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
876 int ret = 0;
877
878 readlen = fread(&header, header_sizeof(header), 1, in);
879 if (readlen < 1)
880 return -EINVAL;
881
882 if (td->byte_order != BYTE_ORDER) {
883 header.magic = GUINT32_SWAP_LE_BE(header.magic);
884 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
885 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
886 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
887 }
888 if (header.checksum)
889 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
890 if (header.compression_scheme) {
891 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
892 header.compression_scheme);
893 return -EINVAL;
894 }
895 if (header.encryption_scheme) {
896 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
897 header.encryption_scheme);
898 return -EINVAL;
899 }
900 if (header.checksum_scheme) {
901 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
902 header.checksum_scheme);
903 return -EINVAL;
904 }
905 if (check_version(header.major, header.minor) < 0)
906 return -EINVAL;
907 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
908 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
909 CTF_TRACE_SET_FIELD(td, uuid);
910 } else {
911 if (babeltrace_uuid_compare(header.uuid, td->uuid))
912 return -EINVAL;
913 }
914
915 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
916 return -EINVAL;
917
918 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
919
920 for (;;) {
921 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
922 if (ferror(in)) {
923 ret = -EINVAL;
924 break;
925 }
926 if (babeltrace_debug) {
927 buf[readlen] = '\0';
928 fprintf(stderr, "[debug] metadata packet read: %s\n",
929 buf);
930 }
931
932 writelen = fwrite(buf, sizeof(char), readlen, out);
933 if (writelen < readlen) {
934 ret = -EIO;
935 break;
936 }
937 if (ferror(out)) {
938 ret = -EINVAL;
939 break;
940 }
941 toread -= readlen;
942 if (!toread) {
943 ret = 0; /* continue reading next packet */
944 goto read_padding;
945 }
946 }
947 return ret;
948
949 read_padding:
950 toread = (header.packet_size - header.content_size) / CHAR_BIT;
951 ret = fseek(in, toread, SEEK_CUR);
952 if (ret < 0) {
953 fprintf(stderr, "[warning] Missing padding at end of file\n");
954 ret = 0;
955 }
956 return ret;
957 }
958
959 static
960 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
961 char **buf)
962 {
963 FILE *in, *out;
964 size_t size, buflen;
965 int ret;
966
967 in = *fp;
968 /*
969 * Using strlen on *buf instead of size of open_memstream
970 * because its size includes garbage at the end (after final
971 * \0). This is the allocated size, not the actual string size.
972 */
973 out = babeltrace_open_memstream(buf, &size);
974 if (out == NULL) {
975 perror("Metadata open_memstream");
976 return -errno;
977 }
978 for (;;) {
979 ret = ctf_open_trace_metadata_packet_read(td, in, out);
980 if (ret) {
981 break;
982 }
983 if (feof(in)) {
984 ret = 0;
985 break;
986 }
987 }
988 /* close to flush the buffer */
989 ret = babeltrace_close_memstream(buf, &size, out);
990 if (ret < 0) {
991 int closeret;
992
993 perror("babeltrace_flush_memstream");
994 ret = -errno;
995 closeret = fclose(in);
996 if (closeret) {
997 perror("Error in fclose");
998 }
999 return ret;
1000 }
1001 ret = fclose(in);
1002 if (ret) {
1003 perror("Error in fclose");
1004 }
1005 /* open for reading */
1006 buflen = strlen(*buf);
1007 if (!buflen) {
1008 *fp = NULL;
1009 return -ENODATA;
1010 }
1011 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1012 if (!*fp) {
1013 perror("Metadata fmemopen");
1014 return -errno;
1015 }
1016 return 0;
1017 }
1018
1019 static
1020 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1021 void (*packet_seek)(struct stream_pos *pos, size_t index,
1022 int whence), FILE *metadata_fp)
1023 {
1024 struct ctf_scanner *scanner;
1025 struct ctf_file_stream *metadata_stream;
1026 FILE *fp;
1027 char *buf = NULL;
1028 int ret = 0, closeret;
1029
1030 metadata_stream = g_new0(struct ctf_file_stream, 1);
1031 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1032
1033 if (packet_seek) {
1034 metadata_stream->pos.packet_seek = packet_seek;
1035 } else {
1036 fprintf(stderr, "[error] packet_seek function undefined.\n");
1037 ret = -1;
1038 goto end_free;
1039 }
1040
1041 if (metadata_fp) {
1042 fp = metadata_fp;
1043 metadata_stream->pos.fd = -1;
1044 } else {
1045 td->metadata = &metadata_stream->parent;
1046 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1047 if (metadata_stream->pos.fd < 0) {
1048 fprintf(stderr, "Unable to open metadata.\n");
1049 ret = -1;
1050 goto end_free;
1051 }
1052
1053 fp = fdopen(metadata_stream->pos.fd, "r");
1054 if (!fp) {
1055 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1056 perror("Metadata stream open");
1057 ret = -errno;
1058 goto end_stream;
1059 }
1060 /* fd now belongs to fp */
1061 metadata_stream->pos.fd = -1;
1062 }
1063 if (babeltrace_debug)
1064 yydebug = 1;
1065
1066 if (packet_metadata(td, fp)) {
1067 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1068 if (ret) {
1069 /* Warn about empty metadata */
1070 fprintf(stderr, "[warning] Empty metadata.\n");
1071 goto end_packet_read;
1072 }
1073 } else {
1074 unsigned int major, minor;
1075 ssize_t nr_items;
1076
1077 td->byte_order = BYTE_ORDER;
1078
1079 /* Check text-only metadata header and version */
1080 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1081 if (nr_items < 2)
1082 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1083 if (check_version(major, minor) < 0) {
1084 ret = -EINVAL;
1085 goto end_packet_read;
1086 }
1087 rewind(fp);
1088 }
1089
1090 scanner = ctf_scanner_alloc(fp);
1091 if (!scanner) {
1092 fprintf(stderr, "[error] Error allocating scanner\n");
1093 ret = -ENOMEM;
1094 goto end_scanner_alloc;
1095 }
1096 ret = ctf_scanner_append_ast(scanner);
1097 if (ret) {
1098 fprintf(stderr, "[error] Error creating AST\n");
1099 goto end;
1100 }
1101
1102 if (babeltrace_debug) {
1103 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1104 if (ret) {
1105 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1106 goto end;
1107 }
1108 }
1109
1110 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1111 if (ret) {
1112 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1113 goto end;
1114 }
1115 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1116 td, td->byte_order);
1117 if (ret) {
1118 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1119 goto end;
1120 }
1121 end:
1122 ctf_scanner_free(scanner);
1123 end_scanner_alloc:
1124 end_packet_read:
1125 if (fp) {
1126 closeret = fclose(fp);
1127 if (closeret) {
1128 perror("Error on fclose");
1129 }
1130 }
1131 free(buf);
1132 end_stream:
1133 if (metadata_stream->pos.fd >= 0) {
1134 closeret = close(metadata_stream->pos.fd);
1135 if (closeret) {
1136 perror("Error on metadata stream fd close");
1137 }
1138 }
1139 end_free:
1140 if (ret)
1141 g_free(metadata_stream);
1142 return ret;
1143 }
1144
1145 static
1146 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1147 struct ctf_stream_definition *stream,
1148 struct ctf_event_declaration *event)
1149 {
1150 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1151
1152 if (event->context_decl) {
1153 struct definition *definition =
1154 event->context_decl->p.definition_new(&event->context_decl->p,
1155 stream->parent_def_scope, 0, 0, "event.context");
1156 if (!definition) {
1157 goto error;
1158 }
1159 stream_event->event_context = container_of(definition,
1160 struct definition_struct, p);
1161 stream->parent_def_scope = stream_event->event_context->p.scope;
1162 }
1163 if (event->fields_decl) {
1164 struct definition *definition =
1165 event->fields_decl->p.definition_new(&event->fields_decl->p,
1166 stream->parent_def_scope, 0, 0, "event.fields");
1167 if (!definition) {
1168 goto error;
1169 }
1170 stream_event->event_fields = container_of(definition,
1171 struct definition_struct, p);
1172 stream->parent_def_scope = stream_event->event_fields->p.scope;
1173 }
1174 stream_event->stream = stream;
1175 return stream_event;
1176
1177 error:
1178 if (stream_event->event_fields)
1179 bt_definition_unref(&stream_event->event_fields->p);
1180 if (stream_event->event_context)
1181 bt_definition_unref(&stream_event->event_context->p);
1182 return NULL;
1183 }
1184
1185 static
1186 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1187 {
1188 struct ctf_stream_declaration *stream_class;
1189 int ret;
1190 int i;
1191
1192 if (stream->stream_definitions_created)
1193 return 0;
1194
1195 stream_class = stream->stream_class;
1196
1197 if (stream_class->packet_context_decl) {
1198 struct definition *definition =
1199 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1200 stream->parent_def_scope, 0, 0, "stream.packet.context");
1201 if (!definition) {
1202 ret = -EINVAL;
1203 goto error;
1204 }
1205 stream->stream_packet_context = container_of(definition,
1206 struct definition_struct, p);
1207 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1208 }
1209 if (stream_class->event_header_decl) {
1210 struct definition *definition =
1211 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1212 stream->parent_def_scope, 0, 0, "stream.event.header");
1213 if (!definition) {
1214 ret = -EINVAL;
1215 goto error;
1216 }
1217 stream->stream_event_header =
1218 container_of(definition, struct definition_struct, p);
1219 stream->parent_def_scope = stream->stream_event_header->p.scope;
1220 }
1221 if (stream_class->event_context_decl) {
1222 struct definition *definition =
1223 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1224 stream->parent_def_scope, 0, 0, "stream.event.context");
1225 if (!definition) {
1226 ret = -EINVAL;
1227 goto error;
1228 }
1229 stream->stream_event_context =
1230 container_of(definition, struct definition_struct, p);
1231 stream->parent_def_scope = stream->stream_event_context->p.scope;
1232 }
1233 stream->events_by_id = g_ptr_array_new();
1234 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1235 for (i = 0; i < stream->events_by_id->len; i++) {
1236 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1237 struct ctf_event_definition *stream_event;
1238
1239 if (!event)
1240 continue;
1241 stream_event = create_event_definitions(td, stream, event);
1242 if (!stream_event)
1243 goto error_event;
1244 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1245 }
1246 return 0;
1247
1248 error_event:
1249 for (i = 0; i < stream->events_by_id->len; i++) {
1250 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1251 if (stream_event)
1252 g_free(stream_event);
1253 }
1254 g_ptr_array_free(stream->events_by_id, TRUE);
1255 error:
1256 if (stream->stream_event_context)
1257 bt_definition_unref(&stream->stream_event_context->p);
1258 if (stream->stream_event_header)
1259 bt_definition_unref(&stream->stream_event_header->p);
1260 if (stream->stream_packet_context)
1261 bt_definition_unref(&stream->stream_packet_context->p);
1262 return ret;
1263 }
1264
1265
1266 static
1267 int create_stream_packet_index(struct ctf_trace *td,
1268 struct ctf_file_stream *file_stream)
1269 {
1270 struct ctf_stream_declaration *stream;
1271 int len_index;
1272 struct ctf_stream_pos *pos;
1273 struct stat filestats;
1274 struct packet_index packet_index;
1275 int first_packet = 1;
1276 int ret;
1277
1278 pos = &file_stream->pos;
1279
1280 ret = fstat(pos->fd, &filestats);
1281 if (ret < 0)
1282 return ret;
1283
1284 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1285 return -EINVAL;
1286
1287 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1288 uint64_t stream_id = 0;
1289
1290 if (pos->base_mma) {
1291 /* unmap old base */
1292 ret = munmap_align(pos->base_mma);
1293 if (ret) {
1294 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1295 strerror(errno));
1296 return ret;
1297 }
1298 pos->base_mma = NULL;
1299 }
1300 /* map new base. Need mapping length from header. */
1301 pos->base_mma = mmap_align(MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
1302 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1303 assert(pos->base_mma != MAP_FAILED);
1304 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1305 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1306 pos->offset = 0; /* Position of the packet header */
1307
1308 packet_index.offset = pos->mmap_offset;
1309 packet_index.content_size = 0;
1310 packet_index.packet_size = 0;
1311 packet_index.timestamp_begin = 0;
1312 packet_index.timestamp_end = 0;
1313 packet_index.events_discarded = 0;
1314 packet_index.events_discarded_len = 0;
1315
1316 /* read and check header, set stream id (and check) */
1317 if (file_stream->parent.trace_packet_header) {
1318 /* Read packet header */
1319 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1320 if (ret)
1321 return ret;
1322 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1323 if (len_index >= 0) {
1324 struct definition *field;
1325 uint64_t magic;
1326
1327 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1328 magic = bt_get_unsigned_int(field);
1329 if (magic != CTF_MAGIC) {
1330 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1331 magic,
1332 file_stream->pos.packet_cycles_index->len,
1333 (ssize_t) pos->mmap_offset);
1334 return -EINVAL;
1335 }
1336 }
1337
1338 /* check uuid */
1339 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1340 if (len_index >= 0) {
1341 struct definition_array *defarray;
1342 struct definition *field;
1343 uint64_t i;
1344 uint8_t uuidval[BABELTRACE_UUID_LEN];
1345
1346 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1347 assert(field->declaration->id == CTF_TYPE_ARRAY);
1348 defarray = container_of(field, struct definition_array, p);
1349 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1350
1351 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1352 struct definition *elem;
1353
1354 elem = bt_array_index(defarray, i);
1355 uuidval[i] = bt_get_unsigned_int(elem);
1356 }
1357 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1358 if (ret) {
1359 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1360 return -EINVAL;
1361 }
1362 }
1363
1364
1365 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1366 if (len_index >= 0) {
1367 struct definition *field;
1368
1369 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1370 stream_id = bt_get_unsigned_int(field);
1371 }
1372 }
1373
1374 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1375 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
1376 return -EINVAL;
1377 }
1378 if (first_packet) {
1379 file_stream->parent.stream_id = stream_id;
1380 if (stream_id >= td->streams->len) {
1381 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1382 return -EINVAL;
1383 }
1384 stream = g_ptr_array_index(td->streams, stream_id);
1385 if (!stream) {
1386 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1387 return -EINVAL;
1388 }
1389 file_stream->parent.stream_class = stream;
1390 ret = create_stream_definitions(td, &file_stream->parent);
1391 if (ret)
1392 return ret;
1393 }
1394 first_packet = 0;
1395
1396 if (file_stream->parent.stream_packet_context) {
1397 /* Read packet context */
1398 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1399 if (ret)
1400 return ret;
1401 /* read content size from header */
1402 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1403 if (len_index >= 0) {
1404 struct definition *field;
1405
1406 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1407 packet_index.content_size = bt_get_unsigned_int(field);
1408 } else {
1409 /* Use file size for packet size */
1410 packet_index.content_size = filestats.st_size * CHAR_BIT;
1411 }
1412
1413 /* read packet size from header */
1414 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1415 if (len_index >= 0) {
1416 struct definition *field;
1417
1418 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1419 packet_index.packet_size = bt_get_unsigned_int(field);
1420 } else {
1421 /* Use content size if non-zero, else file size */
1422 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1423 }
1424
1425 /* read timestamp begin from header */
1426 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1427 if (len_index >= 0) {
1428 struct definition *field;
1429
1430 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1431 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1432 if (file_stream->parent.stream_class->trace->collection) {
1433 packet_index.timestamp_begin =
1434 ctf_get_real_timestamp(
1435 &file_stream->parent,
1436 packet_index.timestamp_begin);
1437 }
1438 }
1439
1440 /* read timestamp end from header */
1441 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1442 if (len_index >= 0) {
1443 struct definition *field;
1444
1445 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1446 packet_index.timestamp_end = bt_get_unsigned_int(field);
1447 if (file_stream->parent.stream_class->trace->collection) {
1448 packet_index.timestamp_end =
1449 ctf_get_real_timestamp(
1450 &file_stream->parent,
1451 packet_index.timestamp_end);
1452 }
1453 }
1454
1455 /* read events discarded from header */
1456 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1457 if (len_index >= 0) {
1458 struct definition *field;
1459
1460 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1461 packet_index.events_discarded = bt_get_unsigned_int(field);
1462 packet_index.events_discarded_len = bt_get_int_len(field);
1463 }
1464 } else {
1465 /* Use file size for packet size */
1466 packet_index.content_size = filestats.st_size * CHAR_BIT;
1467 /* Use content size if non-zero, else file size */
1468 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
1469 }
1470
1471 /* Validate content size and packet size values */
1472 if (packet_index.content_size > packet_index.packet_size) {
1473 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1474 packet_index.content_size, packet_index.packet_size);
1475 return -EINVAL;
1476 }
1477
1478 if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
1479 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1480 packet_index.packet_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
1481 return -EINVAL;
1482 }
1483
1484 /* Save position after header and context */
1485 packet_index.data_offset = pos->offset;
1486
1487 /* add index to packet array */
1488 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1489
1490 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
1491 }
1492
1493 return 0;
1494 }
1495
1496 static
1497 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1498 {
1499 int ret;
1500
1501 if (td->packet_header_decl) {
1502 struct definition *definition =
1503 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1504 stream->parent_def_scope, 0, 0, "trace.packet.header");
1505 if (!definition) {
1506 ret = -EINVAL;
1507 goto error;
1508 }
1509 stream->trace_packet_header =
1510 container_of(definition, struct definition_struct, p);
1511 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1512 }
1513
1514 return 0;
1515
1516 error:
1517 return ret;
1518 }
1519
1520 /*
1521 * Note: many file streams can inherit from the same stream class
1522 * description (metadata).
1523 */
1524 static
1525 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1526 void (*packet_seek)(struct stream_pos *pos, size_t index,
1527 int whence))
1528 {
1529 int ret, fd, closeret;
1530 struct ctf_file_stream *file_stream;
1531 struct stat statbuf;
1532
1533 fd = openat(td->dirfd, path, flags);
1534 if (fd < 0) {
1535 perror("File stream openat()");
1536 ret = fd;
1537 goto error;
1538 }
1539
1540 /* Don't try to mmap subdirectories. Skip them, return success. */
1541 ret = fstat(fd, &statbuf);
1542 if (ret) {
1543 perror("File stream fstat()");
1544 goto fstat_error;
1545 }
1546 if (S_ISDIR(statbuf.st_mode)) {
1547 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1548 ret = 0;
1549 goto fd_is_dir_ok;
1550 }
1551
1552 file_stream = g_new0(struct ctf_file_stream, 1);
1553 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1554
1555 if (packet_seek) {
1556 file_stream->pos.packet_seek = packet_seek;
1557 } else {
1558 fprintf(stderr, "[error] packet_seek function undefined.\n");
1559 ret = -1;
1560 goto error_def;
1561 }
1562
1563 ret = ctf_init_pos(&file_stream->pos, fd, flags);
1564 if (ret)
1565 goto error_def;
1566 ret = create_trace_definitions(td, &file_stream->parent);
1567 if (ret)
1568 goto error_def;
1569 /*
1570 * For now, only a single clock per trace is supported.
1571 */
1572 file_stream->parent.current_clock = td->single_clock;
1573 ret = create_stream_packet_index(td, file_stream);
1574 if (ret)
1575 goto error_index;
1576 /* Add stream file to stream class */
1577 g_ptr_array_add(file_stream->parent.stream_class->streams,
1578 &file_stream->parent);
1579 return 0;
1580
1581 error_index:
1582 if (file_stream->parent.trace_packet_header)
1583 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1584 error_def:
1585 closeret = ctf_fini_pos(&file_stream->pos);
1586 if (closeret) {
1587 fprintf(stderr, "Error on ctf_fini_pos\n");
1588 }
1589 g_free(file_stream);
1590 fd_is_dir_ok:
1591 fstat_error:
1592 closeret = close(fd);
1593 if (closeret) {
1594 perror("Error on fd close");
1595 }
1596 error:
1597 return ret;
1598 }
1599
1600 static
1601 int ctf_open_trace_read(struct ctf_trace *td,
1602 const char *path, int flags,
1603 void (*packet_seek)(struct stream_pos *pos, size_t index,
1604 int whence), FILE *metadata_fp)
1605 {
1606 int ret, closeret;
1607 struct dirent *dirent;
1608 struct dirent *diriter;
1609 size_t dirent_len;
1610
1611 td->flags = flags;
1612
1613 /* Open trace directory */
1614 td->dir = opendir(path);
1615 if (!td->dir) {
1616 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1617 ret = -ENOENT;
1618 goto error;
1619 }
1620
1621 td->dirfd = open(path, 0);
1622 if (td->dirfd < 0) {
1623 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1624 perror("Trace directory open");
1625 ret = -errno;
1626 goto error_dirfd;
1627 }
1628 strncpy(td->path, path, sizeof(td->path));
1629 td->path[sizeof(td->path) - 1] = '\0';
1630
1631 /*
1632 * Keep the metadata file separate.
1633 */
1634
1635 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1636 if (ret) {
1637 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1638 goto error_metadata;
1639 }
1640
1641 /*
1642 * Open each stream: for each file, try to open, check magic
1643 * number, and get the stream ID to add to the right location in
1644 * the stream array.
1645 */
1646
1647 dirent_len = offsetof(struct dirent, d_name) +
1648 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1649
1650 dirent = malloc(dirent_len);
1651
1652 for (;;) {
1653 ret = readdir_r(td->dir, dirent, &diriter);
1654 if (ret) {
1655 fprintf(stderr, "[error] Readdir error.\n");
1656 goto readdir_error;
1657 }
1658 if (!diriter)
1659 break;
1660 /* Ignore hidden files, ., .. and metadata. */
1661 if (!strncmp(diriter->d_name, ".", 1)
1662 || !strcmp(diriter->d_name, "..")
1663 || !strcmp(diriter->d_name, "metadata"))
1664 continue;
1665 ret = ctf_open_file_stream_read(td, diriter->d_name,
1666 flags, packet_seek);
1667 if (ret) {
1668 fprintf(stderr, "[error] Open file stream error.\n");
1669 goto readdir_error;
1670 }
1671 }
1672
1673 free(dirent);
1674 return 0;
1675
1676 readdir_error:
1677 free(dirent);
1678 error_metadata:
1679 closeret = close(td->dirfd);
1680 if (closeret) {
1681 perror("Error on fd close");
1682 }
1683 error_dirfd:
1684 closeret = closedir(td->dir);
1685 if (closeret) {
1686 perror("Error on closedir");
1687 }
1688 error:
1689 return ret;
1690 }
1691
1692 /*
1693 * ctf_open_trace: Open a CTF trace and index it.
1694 * Note that the user must seek the trace after the open (using the iterator)
1695 * since the index creation read it entirely.
1696 */
1697 static
1698 struct trace_descriptor *ctf_open_trace(const char *path, int flags,
1699 void (*packet_seek)(struct stream_pos *pos, size_t index,
1700 int whence), FILE *metadata_fp)
1701 {
1702 struct ctf_trace *td;
1703 int ret;
1704
1705 /*
1706 * If packet_seek is NULL, we provide our default version.
1707 */
1708 if (!packet_seek)
1709 packet_seek = ctf_packet_seek;
1710
1711 td = g_new0(struct ctf_trace, 1);
1712
1713 switch (flags & O_ACCMODE) {
1714 case O_RDONLY:
1715 if (!path) {
1716 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1717 goto error;
1718 }
1719 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1720 if (ret)
1721 goto error;
1722 break;
1723 case O_RDWR:
1724 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1725 goto error;
1726 default:
1727 fprintf(stderr, "[error] Incorrect open flags.\n");
1728 goto error;
1729 }
1730
1731 return &td->parent;
1732 error:
1733 g_free(td);
1734 return NULL;
1735 }
1736
1737 static
1738 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1739 struct mmap_stream *mmap_info)
1740 {
1741 pos->mmap_offset = 0;
1742 pos->packet_size = 0;
1743 pos->content_size = 0;
1744 pos->content_size_loc = NULL;
1745 pos->fd = mmap_info->fd;
1746 pos->base_mma = NULL;
1747 pos->offset = 0;
1748 pos->dummy = false;
1749 pos->cur_index = 0;
1750 pos->packet_cycles_index = NULL;
1751 pos->packet_real_index = NULL;
1752 pos->prot = PROT_READ;
1753 pos->flags = MAP_PRIVATE;
1754 pos->parent.rw_table = read_dispatch_table;
1755 pos->parent.event_cb = ctf_read_event;
1756 }
1757
1758 static
1759 int prepare_mmap_stream_definition(struct ctf_trace *td,
1760 struct ctf_file_stream *file_stream)
1761 {
1762 struct ctf_stream_declaration *stream;
1763 uint64_t stream_id = 0;
1764 int ret;
1765
1766 file_stream->parent.stream_id = stream_id;
1767 if (stream_id >= td->streams->len) {
1768 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1769 "in metadata.\n", stream_id);
1770 ret = -EINVAL;
1771 goto end;
1772 }
1773 stream = g_ptr_array_index(td->streams, stream_id);
1774 if (!stream) {
1775 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1776 "in metadata.\n", stream_id);
1777 ret = -EINVAL;
1778 goto end;
1779 }
1780 file_stream->parent.stream_class = stream;
1781 ret = create_stream_definitions(td, &file_stream->parent);
1782 end:
1783 return ret;
1784 }
1785
1786 static
1787 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1788 struct mmap_stream *mmap_info,
1789 void (*packet_seek)(struct stream_pos *pos, size_t index,
1790 int whence))
1791 {
1792 int ret;
1793 struct ctf_file_stream *file_stream;
1794
1795 file_stream = g_new0(struct ctf_file_stream, 1);
1796 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1797 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1798
1799 file_stream->pos.packet_seek = packet_seek;
1800
1801 ret = create_trace_definitions(td, &file_stream->parent);
1802 if (ret) {
1803 goto error_def;
1804 }
1805
1806 ret = prepare_mmap_stream_definition(td, file_stream);
1807 if (ret)
1808 goto error_index;
1809
1810 /*
1811 * For now, only a single clock per trace is supported.
1812 */
1813 file_stream->parent.current_clock = td->single_clock;
1814
1815 /* Add stream file to stream class */
1816 g_ptr_array_add(file_stream->parent.stream_class->streams,
1817 &file_stream->parent);
1818 return 0;
1819
1820 error_index:
1821 if (file_stream->parent.trace_packet_header)
1822 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1823 error_def:
1824 g_free(file_stream);
1825 return ret;
1826 }
1827
1828 static
1829 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1830 struct mmap_stream_list *mmap_list,
1831 void (*packet_seek)(struct stream_pos *pos, size_t index,
1832 int whence),
1833 FILE *metadata_fp)
1834 {
1835 int ret;
1836 struct mmap_stream *mmap_info;
1837
1838 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1839 if (ret) {
1840 goto error;
1841 }
1842
1843 /*
1844 * for each stream, try to open, check magic number, and get the
1845 * stream ID to add to the right location in the stream array.
1846 */
1847 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1848 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
1849 if (ret) {
1850 fprintf(stderr, "[error] Open file mmap stream error.\n");
1851 goto error;
1852 }
1853 }
1854
1855 return 0;
1856
1857 error:
1858 return ret;
1859 }
1860
1861 static
1862 struct trace_descriptor *ctf_open_mmap_trace(
1863 struct mmap_stream_list *mmap_list,
1864 void (*packet_seek)(struct stream_pos *pos, size_t index,
1865 int whence),
1866 FILE *metadata_fp)
1867 {
1868 struct ctf_trace *td;
1869 int ret;
1870
1871 if (!metadata_fp) {
1872 fprintf(stderr, "[error] No metadata file pointer associated, "
1873 "required for mmap parsing\n");
1874 goto error;
1875 }
1876 if (!packet_seek) {
1877 fprintf(stderr, "[error] packet_seek function undefined.\n");
1878 goto error;
1879 }
1880 td = g_new0(struct ctf_trace, 1);
1881 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
1882 if (ret)
1883 goto error_free;
1884
1885 return &td->parent;
1886
1887 error_free:
1888 g_free(td);
1889 error:
1890 return NULL;
1891 }
1892
1893 static
1894 int ctf_convert_index_timestamp(struct trace_descriptor *tdp)
1895 {
1896 int i, j, k;
1897 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1898
1899 /* for each stream_class */
1900 for (i = 0; i < td->streams->len; i++) {
1901 struct ctf_stream_declaration *stream_class;
1902
1903 stream_class = g_ptr_array_index(td->streams, i);
1904 if (!stream_class)
1905 continue;
1906 /* for each file_stream */
1907 for (j = 0; j < stream_class->streams->len; j++) {
1908 struct ctf_stream_definition *stream;
1909 struct ctf_stream_pos *stream_pos;
1910 struct ctf_file_stream *cfs;
1911
1912 stream = g_ptr_array_index(stream_class->streams, j);
1913 if (!stream)
1914 continue;
1915 cfs = container_of(stream, struct ctf_file_stream,
1916 parent);
1917 stream_pos = &cfs->pos;
1918 if (!stream_pos->packet_cycles_index)
1919 continue;
1920
1921 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
1922 struct packet_index *index;
1923 struct packet_index new_index;
1924
1925 index = &g_array_index(stream_pos->packet_cycles_index,
1926 struct packet_index, k);
1927 memcpy(&new_index, index,
1928 sizeof(struct packet_index));
1929 new_index.timestamp_begin =
1930 ctf_get_real_timestamp(stream,
1931 index->timestamp_begin);
1932 new_index.timestamp_end =
1933 ctf_get_real_timestamp(stream,
1934 index->timestamp_end);
1935 g_array_append_val(stream_pos->packet_real_index,
1936 new_index);
1937 }
1938 }
1939 }
1940 return 0;
1941 }
1942
1943 static
1944 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
1945 {
1946 int ret;
1947
1948 ret = ctf_fini_pos(&file_stream->pos);
1949 if (ret) {
1950 fprintf(stderr, "Error on ctf_fini_pos\n");
1951 return -1;
1952 }
1953 ret = close(file_stream->pos.fd);
1954 if (ret) {
1955 perror("Error closing file fd");
1956 return -1;
1957 }
1958 return 0;
1959 }
1960
1961 static
1962 int ctf_close_trace(struct trace_descriptor *tdp)
1963 {
1964 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1965 int ret;
1966
1967 if (td->streams) {
1968 int i;
1969
1970 for (i = 0; i < td->streams->len; i++) {
1971 struct ctf_stream_declaration *stream;
1972 int j;
1973
1974 stream = g_ptr_array_index(td->streams, i);
1975 if (!stream)
1976 continue;
1977 for (j = 0; j < stream->streams->len; j++) {
1978 struct ctf_file_stream *file_stream;
1979 file_stream = container_of(g_ptr_array_index(stream->streams, j),
1980 struct ctf_file_stream, parent);
1981 ret = ctf_close_file_stream(file_stream);
1982 if (ret)
1983 return ret;
1984 }
1985 }
1986 }
1987 ctf_destroy_metadata(td);
1988 ret = close(td->dirfd);
1989 if (ret) {
1990 perror("Error closing dirfd");
1991 return ret;
1992 }
1993 ret = closedir(td->dir);
1994 if (ret) {
1995 perror("Error closedir");
1996 return ret;
1997 }
1998 g_free(td);
1999 return 0;
2000 }
2001
2002 static
2003 void ctf_set_context(struct trace_descriptor *descriptor,
2004 struct bt_context *ctx)
2005 {
2006 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2007 parent);
2008
2009 td->ctx = ctx;
2010 }
2011
2012 static
2013 void ctf_set_handle(struct trace_descriptor *descriptor,
2014 struct bt_trace_handle *handle)
2015 {
2016 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2017 parent);
2018
2019 td->handle = handle;
2020 }
2021
2022 static
2023 void __attribute__((constructor)) ctf_init(void)
2024 {
2025 int ret;
2026
2027 ctf_format.name = g_quark_from_static_string("ctf");
2028 ret = bt_register_format(&ctf_format);
2029 assert(!ret);
2030 }
2031
2032 static
2033 void __attribute__((destructor)) ctf_exit(void)
2034 {
2035 bt_unregister_format(&ctf_format);
2036 }
This page took 0.071732 seconds and 3 git commands to generate.