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