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