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