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