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