Use uint64_t for packet_map_len
[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, int fd, int open_flags)
610 {
611 pos->fd = fd;
612 if (fd >= 0) {
613 pos->packet_cycles_index = g_array_new(FALSE, TRUE,
614 sizeof(struct packet_index));
615 pos->packet_real_index = g_array_new(FALSE, TRUE,
616 sizeof(struct packet_index));
617 } else {
618 pos->packet_cycles_index = NULL;
619 pos->packet_real_index = NULL;
620 }
621 switch (open_flags & O_ACCMODE) {
622 case O_RDONLY:
623 pos->prot = PROT_READ;
624 pos->flags = MAP_PRIVATE;
625 pos->parent.rw_table = read_dispatch_table;
626 pos->parent.event_cb = ctf_read_event;
627 break;
628 case O_RDWR:
629 pos->prot = PROT_WRITE; /* Write has priority */
630 pos->flags = MAP_SHARED;
631 pos->parent.rw_table = write_dispatch_table;
632 pos->parent.event_cb = ctf_write_event;
633 if (fd >= 0)
634 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
635 break;
636 default:
637 assert(0);
638 }
639 return 0;
640 }
641
642 int ctf_fini_pos(struct ctf_stream_pos *pos)
643 {
644 if (pos->prot == PROT_WRITE && pos->content_size_loc)
645 *pos->content_size_loc = pos->offset;
646 if (pos->base_mma) {
647 int ret;
648
649 /* unmap old base */
650 ret = munmap_align(pos->base_mma);
651 if (ret) {
652 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
653 strerror(errno));
654 return -1;
655 }
656 }
657 if (pos->packet_cycles_index)
658 (void) g_array_free(pos->packet_cycles_index, TRUE);
659 if (pos->packet_real_index)
660 (void) g_array_free(pos->packet_real_index, TRUE);
661 return 0;
662 }
663
664 /*
665 * for SEEK_CUR: go to next packet.
666 * for SEEK_POS: go to packet numer (index).
667 */
668 void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
669 {
670 struct ctf_stream_pos *pos =
671 container_of(stream_pos, struct ctf_stream_pos, parent);
672 struct ctf_file_stream *file_stream =
673 container_of(pos, struct ctf_file_stream, pos);
674 int ret;
675 off_t off;
676 struct packet_index *packet_index;
677
678 if (pos->prot == PROT_WRITE && pos->content_size_loc)
679 *pos->content_size_loc = pos->offset;
680
681 if (pos->base_mma) {
682 /* unmap old base */
683 ret = munmap_align(pos->base_mma);
684 if (ret) {
685 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
686 strerror(errno));
687 assert(0);
688 }
689 pos->base_mma = NULL;
690 }
691
692 /*
693 * The caller should never ask for ctf_move_pos across packets,
694 * except to get exactly at the beginning of the next packet.
695 */
696 if (pos->prot == PROT_WRITE) {
697 switch (whence) {
698 case SEEK_CUR:
699 /* The writer will add padding */
700 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
701 break;
702 case SEEK_SET:
703 assert(index == 0); /* only seek supported for now */
704 pos->cur_index = 0;
705 break;
706 default:
707 assert(0);
708 }
709 pos->content_size = -1U; /* Unknown at this point */
710 pos->packet_size = WRITE_PACKET_LEN;
711 off = posix_fallocate(pos->fd, pos->mmap_offset,
712 pos->packet_size / CHAR_BIT);
713 assert(off >= 0);
714 pos->offset = 0;
715 } else {
716 read_next_packet:
717 switch (whence) {
718 case SEEK_CUR:
719 {
720 uint64_t events_discarded_diff;
721
722 if (pos->offset == EOF) {
723 return;
724 }
725 /* For printing discarded event count */
726 packet_index = &g_array_index(pos->packet_cycles_index,
727 struct packet_index, pos->cur_index);
728 file_stream->parent.prev_cycles_timestamp_end =
729 packet_index->timestamp_end;
730 file_stream->parent.prev_cycles_timestamp =
731 packet_index->timestamp_begin;
732
733 packet_index = &g_array_index(pos->packet_real_index,
734 struct packet_index, pos->cur_index);
735 file_stream->parent.prev_real_timestamp_end =
736 packet_index->timestamp_end;
737 file_stream->parent.prev_real_timestamp =
738 packet_index->timestamp_begin;
739
740 events_discarded_diff = packet_index->events_discarded;
741 if (pos->cur_index > 0) {
742 packet_index = &g_array_index(pos->packet_real_index,
743 struct packet_index,
744 pos->cur_index - 1);
745 events_discarded_diff -= packet_index->events_discarded;
746 /*
747 * Deal with 32-bit wrap-around if the
748 * tracer provided a 32-bit field.
749 */
750 if (packet_index->events_discarded_len == 32) {
751 events_discarded_diff = (uint32_t) events_discarded_diff;
752 }
753 }
754 file_stream->parent.events_discarded = events_discarded_diff;
755 file_stream->parent.prev_real_timestamp = file_stream->parent.real_timestamp;
756 file_stream->parent.prev_cycles_timestamp = file_stream->parent.cycles_timestamp;
757 /* The reader will expect us to skip padding */
758 ++pos->cur_index;
759 break;
760 }
761 case SEEK_SET:
762 packet_index = &g_array_index(pos->packet_cycles_index,
763 struct packet_index, index);
764 pos->last_events_discarded = packet_index->events_discarded;
765 pos->cur_index = index;
766 file_stream->parent.prev_real_timestamp = 0;
767 file_stream->parent.prev_real_timestamp_end = 0;
768 file_stream->parent.prev_cycles_timestamp = 0;
769 file_stream->parent.prev_cycles_timestamp_end = 0;
770 break;
771 default:
772 assert(0);
773 }
774 if (pos->cur_index >= pos->packet_real_index->len) {
775 /*
776 * We need to check if we are in trace read or
777 * called from packet indexing. In this last
778 * case, the collection is not there, so we
779 * cannot print the timestamps.
780 */
781 if ((&file_stream->parent)->stream_class->trace->collection) {
782 /*
783 * When a stream reaches the end of the
784 * file, we need to show the number of
785 * events discarded ourselves, because
786 * there is no next event scheduled to
787 * be printed in the output.
788 */
789 if (file_stream->parent.events_discarded) {
790 fflush(stdout);
791 ctf_print_discarded(stderr,
792 &file_stream->parent,
793 1);
794 file_stream->parent.events_discarded = 0;
795 }
796 }
797 pos->offset = EOF;
798 return;
799 }
800 packet_index = &g_array_index(pos->packet_cycles_index,
801 struct packet_index,
802 pos->cur_index);
803 file_stream->parent.cycles_timestamp = packet_index->timestamp_begin;
804
805 packet_index = &g_array_index(pos->packet_real_index,
806 struct packet_index,
807 pos->cur_index);
808 file_stream->parent.real_timestamp = packet_index->timestamp_begin;
809 pos->mmap_offset = packet_index->offset;
810
811 /* Lookup context/packet size in index */
812 pos->content_size = packet_index->content_size;
813 pos->packet_size = packet_index->packet_size;
814 if (packet_index->data_offset < packet_index->content_size) {
815 pos->offset = 0; /* will read headers */
816 } else if (packet_index->data_offset == packet_index->content_size) {
817 /* empty packet */
818 pos->offset = packet_index->data_offset;
819 whence = SEEK_CUR;
820 goto read_next_packet;
821 } else {
822 pos->offset = EOF;
823 return;
824 }
825 }
826 /* map new base. Need mapping length from header. */
827 pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
828 pos->flags, pos->fd, pos->mmap_offset);
829 if (pos->base_mma == MAP_FAILED) {
830 fprintf(stderr, "[error] mmap error %s.\n",
831 strerror(errno));
832 assert(0);
833 }
834
835 /* update trace_packet_header and stream_packet_context */
836 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
837 /* Read packet header */
838 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
839 assert(!ret);
840 }
841 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
842 /* Read packet context */
843 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
844 assert(!ret);
845 }
846 }
847
848 static
849 int packet_metadata(struct ctf_trace *td, FILE *fp)
850 {
851 uint32_t magic;
852 size_t len;
853 int ret = 0;
854
855 len = fread(&magic, sizeof(magic), 1, fp);
856 if (len != 1) {
857 goto end;
858 }
859 if (magic == TSDL_MAGIC) {
860 ret = 1;
861 td->byte_order = BYTE_ORDER;
862 CTF_TRACE_SET_FIELD(td, byte_order);
863 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
864 ret = 1;
865 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
866 LITTLE_ENDIAN : BIG_ENDIAN;
867 CTF_TRACE_SET_FIELD(td, byte_order);
868 }
869 end:
870 rewind(fp);
871 return ret;
872 }
873
874 /*
875 * Returns 0 on success, -1 on error.
876 */
877 static
878 int check_version(unsigned int major, unsigned int minor)
879 {
880 switch (major) {
881 case 1:
882 switch (minor) {
883 case 8:
884 return 0;
885 default:
886 goto warning;
887 }
888 default:
889 goto warning;
890
891 }
892
893 /* eventually return an error instead of warning */
894 warning:
895 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
896 major, minor);
897 return 0;
898 }
899
900 static
901 int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
902 FILE *out)
903 {
904 struct metadata_packet_header header;
905 size_t readlen, writelen, toread;
906 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
907 int ret = 0;
908
909 readlen = fread(&header, header_sizeof(header), 1, in);
910 if (readlen < 1)
911 return -EINVAL;
912
913 if (td->byte_order != BYTE_ORDER) {
914 header.magic = GUINT32_SWAP_LE_BE(header.magic);
915 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
916 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
917 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
918 }
919 if (header.checksum)
920 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
921 if (header.compression_scheme) {
922 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
923 header.compression_scheme);
924 return -EINVAL;
925 }
926 if (header.encryption_scheme) {
927 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
928 header.encryption_scheme);
929 return -EINVAL;
930 }
931 if (header.checksum_scheme) {
932 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
933 header.checksum_scheme);
934 return -EINVAL;
935 }
936 if (check_version(header.major, header.minor) < 0)
937 return -EINVAL;
938 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
939 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
940 CTF_TRACE_SET_FIELD(td, uuid);
941 } else {
942 if (babeltrace_uuid_compare(header.uuid, td->uuid))
943 return -EINVAL;
944 }
945
946 if ((header.content_size / CHAR_BIT) < header_sizeof(header))
947 return -EINVAL;
948
949 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
950
951 for (;;) {
952 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
953 if (ferror(in)) {
954 ret = -EINVAL;
955 break;
956 }
957 if (babeltrace_debug) {
958 buf[readlen] = '\0';
959 fprintf(stderr, "[debug] metadata packet read: %s\n",
960 buf);
961 }
962
963 writelen = fwrite(buf, sizeof(char), readlen, out);
964 if (writelen < readlen) {
965 ret = -EIO;
966 break;
967 }
968 if (ferror(out)) {
969 ret = -EINVAL;
970 break;
971 }
972 toread -= readlen;
973 if (!toread) {
974 ret = 0; /* continue reading next packet */
975 goto read_padding;
976 }
977 }
978 return ret;
979
980 read_padding:
981 toread = (header.packet_size - header.content_size) / CHAR_BIT;
982 ret = fseek(in, toread, SEEK_CUR);
983 if (ret < 0) {
984 fprintf(stderr, "[warning] Missing padding at end of file\n");
985 ret = 0;
986 }
987 return ret;
988 }
989
990 static
991 int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
992 char **buf)
993 {
994 FILE *in, *out;
995 size_t size, buflen;
996 int ret;
997
998 in = *fp;
999 /*
1000 * Using strlen on *buf instead of size of open_memstream
1001 * because its size includes garbage at the end (after final
1002 * \0). This is the allocated size, not the actual string size.
1003 */
1004 out = babeltrace_open_memstream(buf, &size);
1005 if (out == NULL) {
1006 perror("Metadata open_memstream");
1007 return -errno;
1008 }
1009 for (;;) {
1010 ret = ctf_open_trace_metadata_packet_read(td, in, out);
1011 if (ret) {
1012 break;
1013 }
1014 if (feof(in)) {
1015 ret = 0;
1016 break;
1017 }
1018 }
1019 /* close to flush the buffer */
1020 ret = babeltrace_close_memstream(buf, &size, out);
1021 if (ret < 0) {
1022 int closeret;
1023
1024 perror("babeltrace_flush_memstream");
1025 ret = -errno;
1026 closeret = fclose(in);
1027 if (closeret) {
1028 perror("Error in fclose");
1029 }
1030 return ret;
1031 }
1032 ret = fclose(in);
1033 if (ret) {
1034 perror("Error in fclose");
1035 }
1036 /* open for reading */
1037 buflen = strlen(*buf);
1038 if (!buflen) {
1039 *fp = NULL;
1040 return -ENODATA;
1041 }
1042 *fp = babeltrace_fmemopen(*buf, buflen, "rb");
1043 if (!*fp) {
1044 perror("Metadata fmemopen");
1045 return -errno;
1046 }
1047 return 0;
1048 }
1049
1050 static
1051 int ctf_open_trace_metadata_read(struct ctf_trace *td,
1052 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1053 int whence), FILE *metadata_fp)
1054 {
1055 struct ctf_scanner *scanner;
1056 struct ctf_file_stream *metadata_stream;
1057 FILE *fp;
1058 char *buf = NULL;
1059 int ret = 0, closeret;
1060
1061 metadata_stream = g_new0(struct ctf_file_stream, 1);
1062 metadata_stream->pos.last_offset = LAST_OFFSET_POISON;
1063
1064 if (packet_seek) {
1065 metadata_stream->pos.packet_seek = packet_seek;
1066 } else {
1067 fprintf(stderr, "[error] packet_seek function undefined.\n");
1068 ret = -1;
1069 goto end_free;
1070 }
1071
1072 if (metadata_fp) {
1073 fp = metadata_fp;
1074 metadata_stream->pos.fd = -1;
1075 } else {
1076 td->metadata = &metadata_stream->parent;
1077 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
1078 if (metadata_stream->pos.fd < 0) {
1079 fprintf(stderr, "Unable to open metadata.\n");
1080 ret = -1;
1081 goto end_free;
1082 }
1083
1084 fp = fdopen(metadata_stream->pos.fd, "r");
1085 if (!fp) {
1086 fprintf(stderr, "[error] Unable to open metadata stream.\n");
1087 perror("Metadata stream open");
1088 ret = -errno;
1089 goto end_stream;
1090 }
1091 /* fd now belongs to fp */
1092 metadata_stream->pos.fd = -1;
1093 }
1094 if (babeltrace_debug)
1095 yydebug = 1;
1096
1097 if (packet_metadata(td, fp)) {
1098 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
1099 if (ret) {
1100 /* Warn about empty metadata */
1101 fprintf(stderr, "[warning] Empty metadata.\n");
1102 goto end_packet_read;
1103 }
1104 td->metadata_string = buf;
1105 td->metadata_packetized = 1;
1106 } else {
1107 unsigned int major, minor;
1108 ssize_t nr_items;
1109
1110 td->byte_order = BYTE_ORDER;
1111
1112 /* Check text-only metadata header and version */
1113 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
1114 if (nr_items < 2)
1115 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
1116 if (check_version(major, minor) < 0) {
1117 ret = -EINVAL;
1118 goto end_packet_read;
1119 }
1120 rewind(fp);
1121 }
1122
1123 scanner = ctf_scanner_alloc(fp);
1124 if (!scanner) {
1125 fprintf(stderr, "[error] Error allocating scanner\n");
1126 ret = -ENOMEM;
1127 goto end_scanner_alloc;
1128 }
1129 ret = ctf_scanner_append_ast(scanner);
1130 if (ret) {
1131 fprintf(stderr, "[error] Error creating AST\n");
1132 goto end;
1133 }
1134
1135 if (babeltrace_debug) {
1136 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
1137 if (ret) {
1138 fprintf(stderr, "[error] Error visiting AST for XML output\n");
1139 goto end;
1140 }
1141 }
1142
1143 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
1144 if (ret) {
1145 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
1146 goto end;
1147 }
1148 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
1149 td, td->byte_order);
1150 if (ret) {
1151 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
1152 goto end;
1153 }
1154 end:
1155 ctf_scanner_free(scanner);
1156 end_scanner_alloc:
1157 end_packet_read:
1158 if (fp) {
1159 closeret = fclose(fp);
1160 if (closeret) {
1161 perror("Error on fclose");
1162 }
1163 }
1164 end_stream:
1165 if (metadata_stream->pos.fd >= 0) {
1166 closeret = close(metadata_stream->pos.fd);
1167 if (closeret) {
1168 perror("Error on metadata stream fd close");
1169 }
1170 }
1171 end_free:
1172 if (ret)
1173 g_free(metadata_stream);
1174 return ret;
1175 }
1176
1177 static
1178 struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
1179 struct ctf_stream_definition *stream,
1180 struct ctf_event_declaration *event)
1181 {
1182 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
1183
1184 if (event->context_decl) {
1185 struct bt_definition *definition =
1186 event->context_decl->p.definition_new(&event->context_decl->p,
1187 stream->parent_def_scope, 0, 0, "event.context");
1188 if (!definition) {
1189 goto error;
1190 }
1191 stream_event->event_context = container_of(definition,
1192 struct definition_struct, p);
1193 stream->parent_def_scope = stream_event->event_context->p.scope;
1194 }
1195 if (event->fields_decl) {
1196 struct bt_definition *definition =
1197 event->fields_decl->p.definition_new(&event->fields_decl->p,
1198 stream->parent_def_scope, 0, 0, "event.fields");
1199 if (!definition) {
1200 goto error;
1201 }
1202 stream_event->event_fields = container_of(definition,
1203 struct definition_struct, p);
1204 stream->parent_def_scope = stream_event->event_fields->p.scope;
1205 }
1206 stream_event->stream = stream;
1207 return stream_event;
1208
1209 error:
1210 if (stream_event->event_fields)
1211 bt_definition_unref(&stream_event->event_fields->p);
1212 if (stream_event->event_context)
1213 bt_definition_unref(&stream_event->event_context->p);
1214 return NULL;
1215 }
1216
1217 static
1218 int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1219 {
1220 struct ctf_stream_declaration *stream_class;
1221 int ret;
1222 int i;
1223
1224 if (stream->stream_definitions_created)
1225 return 0;
1226
1227 stream_class = stream->stream_class;
1228
1229 if (stream_class->packet_context_decl) {
1230 struct bt_definition *definition =
1231 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1232 stream->parent_def_scope, 0, 0, "stream.packet.context");
1233 if (!definition) {
1234 ret = -EINVAL;
1235 goto error;
1236 }
1237 stream->stream_packet_context = container_of(definition,
1238 struct definition_struct, p);
1239 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1240 }
1241 if (stream_class->event_header_decl) {
1242 struct bt_definition *definition =
1243 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1244 stream->parent_def_scope, 0, 0, "stream.event.header");
1245 if (!definition) {
1246 ret = -EINVAL;
1247 goto error;
1248 }
1249 stream->stream_event_header =
1250 container_of(definition, struct definition_struct, p);
1251 stream->parent_def_scope = stream->stream_event_header->p.scope;
1252 }
1253 if (stream_class->event_context_decl) {
1254 struct bt_definition *definition =
1255 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1256 stream->parent_def_scope, 0, 0, "stream.event.context");
1257 if (!definition) {
1258 ret = -EINVAL;
1259 goto error;
1260 }
1261 stream->stream_event_context =
1262 container_of(definition, struct definition_struct, p);
1263 stream->parent_def_scope = stream->stream_event_context->p.scope;
1264 }
1265 stream->events_by_id = g_ptr_array_new();
1266 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1267 for (i = 0; i < stream->events_by_id->len; i++) {
1268 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
1269 struct ctf_event_definition *stream_event;
1270
1271 if (!event)
1272 continue;
1273 stream_event = create_event_definitions(td, stream, event);
1274 if (!stream_event)
1275 goto error_event;
1276 g_ptr_array_index(stream->events_by_id, i) = stream_event;
1277 }
1278 return 0;
1279
1280 error_event:
1281 for (i = 0; i < stream->events_by_id->len; i++) {
1282 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
1283 if (stream_event)
1284 g_free(stream_event);
1285 }
1286 g_ptr_array_free(stream->events_by_id, TRUE);
1287 error:
1288 if (stream->stream_event_context)
1289 bt_definition_unref(&stream->stream_event_context->p);
1290 if (stream->stream_event_header)
1291 bt_definition_unref(&stream->stream_event_header->p);
1292 if (stream->stream_packet_context)
1293 bt_definition_unref(&stream->stream_packet_context->p);
1294 return ret;
1295 }
1296
1297 static
1298 int create_stream_one_packet_index(struct ctf_stream_pos *pos,
1299 struct ctf_trace *td,
1300 struct ctf_file_stream *file_stream,
1301 size_t filesize)
1302 {
1303 struct packet_index packet_index;
1304 struct ctf_stream_declaration *stream;
1305 uint64_t stream_id = 0;
1306 uint64_t packet_map_len = DEFAULT_HEADER_LEN, tmp_map_len;
1307 int first_packet = 0;
1308 int len_index;
1309 int ret;
1310
1311 begin:
1312 if (!pos->mmap_offset) {
1313 first_packet = 1;
1314 }
1315
1316 if (filesize - pos->mmap_offset < (packet_map_len >> LOG2_CHAR_BIT)) {
1317 packet_map_len = (filesize - pos->mmap_offset) << LOG2_CHAR_BIT;
1318 }
1319
1320 if (pos->base_mma) {
1321 /* unmap old base */
1322 ret = munmap_align(pos->base_mma);
1323 if (ret) {
1324 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
1325 strerror(errno));
1326 return ret;
1327 }
1328 pos->base_mma = NULL;
1329 }
1330 /* map new base. Need mapping length from header. */
1331 pos->base_mma = mmap_align(packet_map_len >> LOG2_CHAR_BIT, PROT_READ,
1332 MAP_PRIVATE, pos->fd, pos->mmap_offset);
1333 assert(pos->base_mma != MAP_FAILED);
1334 /*
1335 * Use current mapping size as temporary content and packet
1336 * size.
1337 */
1338 pos->content_size = packet_map_len;
1339 pos->packet_size = packet_map_len;
1340 pos->offset = 0; /* Position of the packet header */
1341
1342 packet_index.offset = pos->mmap_offset;
1343 packet_index.content_size = 0;
1344 packet_index.packet_size = 0;
1345 packet_index.timestamp_begin = 0;
1346 packet_index.timestamp_end = 0;
1347 packet_index.events_discarded = 0;
1348 packet_index.events_discarded_len = 0;
1349
1350 /* read and check header, set stream id (and check) */
1351 if (file_stream->parent.trace_packet_header) {
1352 /* Read packet header */
1353 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1354 if (ret) {
1355 if (ret == -EFAULT)
1356 goto retry;
1357 return ret;
1358 }
1359 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
1360 if (len_index >= 0) {
1361 struct bt_definition *field;
1362 uint64_t magic;
1363
1364 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1365 magic = bt_get_unsigned_int(field);
1366 if (magic != CTF_MAGIC) {
1367 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
1368 magic,
1369 file_stream->pos.packet_cycles_index->len,
1370 (ssize_t) pos->mmap_offset);
1371 return -EINVAL;
1372 }
1373 }
1374
1375 /* check uuid */
1376 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
1377 if (len_index >= 0) {
1378 struct definition_array *defarray;
1379 struct bt_definition *field;
1380 uint64_t i;
1381 uint8_t uuidval[BABELTRACE_UUID_LEN];
1382
1383 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1384 assert(field->declaration->id == CTF_TYPE_ARRAY);
1385 defarray = container_of(field, struct definition_array, p);
1386 assert(bt_array_len(defarray) == BABELTRACE_UUID_LEN);
1387
1388 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
1389 struct bt_definition *elem;
1390
1391 elem = bt_array_index(defarray, i);
1392 uuidval[i] = bt_get_unsigned_int(elem);
1393 }
1394 ret = babeltrace_uuid_compare(td->uuid, uuidval);
1395 if (ret) {
1396 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
1397 return -EINVAL;
1398 }
1399 }
1400
1401 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
1402 if (len_index >= 0) {
1403 struct bt_definition *field;
1404
1405 field = bt_struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
1406 stream_id = bt_get_unsigned_int(field);
1407 }
1408 }
1409
1410 if (!first_packet && file_stream->parent.stream_id != stream_id) {
1411 fprintf(stderr, "[error] Stream ID is changing within a stream: expecting %" PRIu64 ", but packet has %" PRIu64 "\n",
1412 stream_id,
1413 file_stream->parent.stream_id);
1414 return -EINVAL;
1415 }
1416 if (first_packet) {
1417 file_stream->parent.stream_id = stream_id;
1418 if (stream_id >= td->streams->len) {
1419 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1420 return -EINVAL;
1421 }
1422 stream = g_ptr_array_index(td->streams, stream_id);
1423 if (!stream) {
1424 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
1425 return -EINVAL;
1426 }
1427 file_stream->parent.stream_class = stream;
1428 ret = create_stream_definitions(td, &file_stream->parent);
1429 if (ret)
1430 return ret;
1431 }
1432
1433 if (file_stream->parent.stream_packet_context) {
1434 /* Read packet context */
1435 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1436 if (ret) {
1437 if (ret == -EFAULT)
1438 goto retry;
1439 return ret;
1440 }
1441 /* read content size from header */
1442 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
1443 if (len_index >= 0) {
1444 struct bt_definition *field;
1445
1446 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1447 packet_index.content_size = bt_get_unsigned_int(field);
1448 } else {
1449 /* Use file size for packet size */
1450 packet_index.content_size = filesize * CHAR_BIT;
1451 }
1452
1453 /* read packet size from header */
1454 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
1455 if (len_index >= 0) {
1456 struct bt_definition *field;
1457
1458 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1459 packet_index.packet_size = bt_get_unsigned_int(field);
1460 } else {
1461 /* Use content size if non-zero, else file size */
1462 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1463 }
1464
1465 /* read timestamp begin from header */
1466 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
1467 if (len_index >= 0) {
1468 struct bt_definition *field;
1469
1470 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1471 packet_index.timestamp_begin = bt_get_unsigned_int(field);
1472 if (file_stream->parent.stream_class->trace->collection) {
1473 packet_index.timestamp_begin =
1474 ctf_get_real_timestamp(
1475 &file_stream->parent,
1476 packet_index.timestamp_begin);
1477 }
1478 }
1479
1480 /* read timestamp end from header */
1481 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
1482 if (len_index >= 0) {
1483 struct bt_definition *field;
1484
1485 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1486 packet_index.timestamp_end = bt_get_unsigned_int(field);
1487 if (file_stream->parent.stream_class->trace->collection) {
1488 packet_index.timestamp_end =
1489 ctf_get_real_timestamp(
1490 &file_stream->parent,
1491 packet_index.timestamp_end);
1492 }
1493 }
1494
1495 /* read events discarded from header */
1496 len_index = bt_struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1497 if (len_index >= 0) {
1498 struct bt_definition *field;
1499
1500 field = bt_struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1501 packet_index.events_discarded = bt_get_unsigned_int(field);
1502 packet_index.events_discarded_len = bt_get_int_len(field);
1503 }
1504 } else {
1505 /* Use file size for packet size */
1506 packet_index.content_size = filesize * CHAR_BIT;
1507 /* Use content size if non-zero, else file size */
1508 packet_index.packet_size = packet_index.content_size ? : filesize * CHAR_BIT;
1509 }
1510
1511 /* Validate content size and packet size values */
1512 if (packet_index.content_size > packet_index.packet_size) {
1513 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
1514 packet_index.content_size, packet_index.packet_size);
1515 return -EINVAL;
1516 }
1517
1518 if (packet_index.packet_size > ((uint64_t) filesize - packet_index.offset) * CHAR_BIT) {
1519 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1520 packet_index.packet_size, ((uint64_t) filesize - packet_index.offset) * CHAR_BIT);
1521 return -EINVAL;
1522 }
1523
1524 /* Save position after header and context */
1525 packet_index.data_offset = pos->offset;
1526
1527 /* add index to packet array */
1528 g_array_append_val(file_stream->pos.packet_cycles_index, packet_index);
1529
1530 pos->mmap_offset += packet_index.packet_size >> LOG2_CHAR_BIT;
1531
1532 return 0;
1533
1534 /* Retry with larger mapping */
1535 retry:
1536 if (packet_map_len == ((filesize - pos->mmap_offset) << LOG2_CHAR_BIT)) {
1537 /*
1538 * Reached EOF, but still expecting header/context data.
1539 */
1540 fprintf(stderr, "[error] Reached end of file, but still expecting header or context fields.\n");
1541 return -EFAULT;
1542 }
1543 /* Double the mapping len, and retry */
1544 tmp_map_len = packet_map_len << 1;
1545 if (tmp_map_len >> 1 != packet_map_len) {
1546 /* Overflow */
1547 return -EFAULT;
1548 }
1549 packet_map_len = tmp_map_len;
1550 goto begin;
1551 }
1552
1553 static
1554 int create_stream_packet_index(struct ctf_trace *td,
1555 struct ctf_file_stream *file_stream)
1556 {
1557 struct ctf_stream_pos *pos;
1558 struct stat filestats;
1559 int ret;
1560
1561 pos = &file_stream->pos;
1562
1563 ret = fstat(pos->fd, &filestats);
1564 if (ret < 0)
1565 return ret;
1566
1567 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1568 ret = create_stream_one_packet_index(pos, td, file_stream,
1569 filestats.st_size);
1570 if (ret)
1571 return ret;
1572 }
1573 return 0;
1574 }
1575
1576 static
1577 int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
1578 {
1579 int ret;
1580
1581 if (td->packet_header_decl) {
1582 struct bt_definition *definition =
1583 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1584 stream->parent_def_scope, 0, 0, "trace.packet.header");
1585 if (!definition) {
1586 ret = -EINVAL;
1587 goto error;
1588 }
1589 stream->trace_packet_header =
1590 container_of(definition, struct definition_struct, p);
1591 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1592 }
1593
1594 return 0;
1595
1596 error:
1597 return ret;
1598 }
1599
1600 /*
1601 * Note: many file streams can inherit from the same stream class
1602 * description (metadata).
1603 */
1604 static
1605 int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
1606 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1607 int whence))
1608 {
1609 int ret, fd, closeret;
1610 struct ctf_file_stream *file_stream;
1611 struct stat statbuf;
1612
1613 fd = openat(td->dirfd, path, flags);
1614 if (fd < 0) {
1615 perror("File stream openat()");
1616 ret = fd;
1617 goto error;
1618 }
1619
1620 /* Don't try to mmap subdirectories. Skip them, return success. */
1621 ret = fstat(fd, &statbuf);
1622 if (ret) {
1623 perror("File stream fstat()");
1624 goto fstat_error;
1625 }
1626 if (S_ISDIR(statbuf.st_mode)) {
1627 fprintf(stderr, "[warning] Skipping directory '%s' found in trace\n", path);
1628 ret = 0;
1629 goto fd_is_dir_ok;
1630 }
1631
1632 file_stream = g_new0(struct ctf_file_stream, 1);
1633 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1634
1635 strncpy(file_stream->parent.path, path, PATH_MAX);
1636 file_stream->parent.path[PATH_MAX - 1] = '\0';
1637
1638 if (packet_seek) {
1639 file_stream->pos.packet_seek = packet_seek;
1640 } else {
1641 fprintf(stderr, "[error] packet_seek function undefined.\n");
1642 ret = -1;
1643 goto error_def;
1644 }
1645
1646 ret = ctf_init_pos(&file_stream->pos, fd, flags);
1647 if (ret)
1648 goto error_def;
1649 ret = create_trace_definitions(td, &file_stream->parent);
1650 if (ret)
1651 goto error_def;
1652 /*
1653 * For now, only a single clock per trace is supported.
1654 */
1655 file_stream->parent.current_clock = td->single_clock;
1656 ret = create_stream_packet_index(td, file_stream);
1657 if (ret)
1658 goto error_index;
1659 /* Add stream file to stream class */
1660 g_ptr_array_add(file_stream->parent.stream_class->streams,
1661 &file_stream->parent);
1662 return 0;
1663
1664 error_index:
1665 if (file_stream->parent.trace_packet_header)
1666 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1667 error_def:
1668 closeret = ctf_fini_pos(&file_stream->pos);
1669 if (closeret) {
1670 fprintf(stderr, "Error on ctf_fini_pos\n");
1671 }
1672 g_free(file_stream);
1673 fd_is_dir_ok:
1674 fstat_error:
1675 closeret = close(fd);
1676 if (closeret) {
1677 perror("Error on fd close");
1678 }
1679 error:
1680 return ret;
1681 }
1682
1683 static
1684 int ctf_open_trace_read(struct ctf_trace *td,
1685 const char *path, int flags,
1686 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1687 int whence), FILE *metadata_fp)
1688 {
1689 int ret, closeret;
1690 struct dirent *dirent;
1691 struct dirent *diriter;
1692 size_t dirent_len;
1693
1694 td->flags = flags;
1695
1696 /* Open trace directory */
1697 td->dir = opendir(path);
1698 if (!td->dir) {
1699 fprintf(stderr, "[error] Unable to open trace directory \"%s\".\n", path);
1700 ret = -ENOENT;
1701 goto error;
1702 }
1703
1704 td->dirfd = open(path, 0);
1705 if (td->dirfd < 0) {
1706 fprintf(stderr, "[error] Unable to open trace directory file descriptor for path \"%s\".\n", path);
1707 perror("Trace directory open");
1708 ret = -errno;
1709 goto error_dirfd;
1710 }
1711 strncpy(td->path, path, sizeof(td->path));
1712 td->path[sizeof(td->path) - 1] = '\0';
1713
1714 /*
1715 * Keep the metadata file separate.
1716 */
1717
1718 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
1719 if (ret) {
1720 fprintf(stderr, "[warning] Unable to open trace metadata for path \"%s\".\n", path);
1721 goto error_metadata;
1722 }
1723
1724 /*
1725 * Open each stream: for each file, try to open, check magic
1726 * number, and get the stream ID to add to the right location in
1727 * the stream array.
1728 */
1729
1730 dirent_len = offsetof(struct dirent, d_name) +
1731 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
1732
1733 dirent = malloc(dirent_len);
1734
1735 for (;;) {
1736 ret = readdir_r(td->dir, dirent, &diriter);
1737 if (ret) {
1738 fprintf(stderr, "[error] Readdir error.\n");
1739 goto readdir_error;
1740 }
1741 if (!diriter)
1742 break;
1743 /* Ignore hidden files, ., .. and metadata. */
1744 if (!strncmp(diriter->d_name, ".", 1)
1745 || !strcmp(diriter->d_name, "..")
1746 || !strcmp(diriter->d_name, "metadata"))
1747 continue;
1748 ret = ctf_open_file_stream_read(td, diriter->d_name,
1749 flags, packet_seek);
1750 if (ret) {
1751 fprintf(stderr, "[error] Open file stream error.\n");
1752 goto readdir_error;
1753 }
1754 }
1755
1756 free(dirent);
1757 return 0;
1758
1759 readdir_error:
1760 free(dirent);
1761 error_metadata:
1762 closeret = close(td->dirfd);
1763 if (closeret) {
1764 perror("Error on fd close");
1765 }
1766 error_dirfd:
1767 closeret = closedir(td->dir);
1768 if (closeret) {
1769 perror("Error on closedir");
1770 }
1771 error:
1772 return ret;
1773 }
1774
1775 /*
1776 * ctf_open_trace: Open a CTF trace and index it.
1777 * Note that the user must seek the trace after the open (using the iterator)
1778 * since the index creation read it entirely.
1779 */
1780 static
1781 struct bt_trace_descriptor *ctf_open_trace(const char *path, int flags,
1782 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1783 int whence), FILE *metadata_fp)
1784 {
1785 struct ctf_trace *td;
1786 int ret;
1787
1788 /*
1789 * If packet_seek is NULL, we provide our default version.
1790 */
1791 if (!packet_seek)
1792 packet_seek = ctf_packet_seek;
1793
1794 td = g_new0(struct ctf_trace, 1);
1795
1796 switch (flags & O_ACCMODE) {
1797 case O_RDONLY:
1798 if (!path) {
1799 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
1800 goto error;
1801 }
1802 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
1803 if (ret)
1804 goto error;
1805 break;
1806 case O_RDWR:
1807 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
1808 goto error;
1809 default:
1810 fprintf(stderr, "[error] Incorrect open flags.\n");
1811 goto error;
1812 }
1813
1814 return &td->parent;
1815 error:
1816 g_free(td);
1817 return NULL;
1818 }
1819
1820 static
1821 void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1822 struct bt_mmap_stream *mmap_info)
1823 {
1824 pos->mmap_offset = 0;
1825 pos->packet_size = 0;
1826 pos->content_size = 0;
1827 pos->content_size_loc = NULL;
1828 pos->fd = mmap_info->fd;
1829 pos->base_mma = NULL;
1830 pos->offset = 0;
1831 pos->dummy = false;
1832 pos->cur_index = 0;
1833 pos->packet_cycles_index = NULL;
1834 pos->packet_real_index = NULL;
1835 pos->prot = PROT_READ;
1836 pos->flags = MAP_PRIVATE;
1837 pos->parent.rw_table = read_dispatch_table;
1838 pos->parent.event_cb = ctf_read_event;
1839 }
1840
1841 static
1842 int prepare_mmap_stream_definition(struct ctf_trace *td,
1843 struct ctf_file_stream *file_stream)
1844 {
1845 struct ctf_stream_declaration *stream;
1846 uint64_t stream_id = 0;
1847 int ret;
1848
1849 file_stream->parent.stream_id = stream_id;
1850 if (stream_id >= td->streams->len) {
1851 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1852 "in metadata.\n", stream_id);
1853 ret = -EINVAL;
1854 goto end;
1855 }
1856 stream = g_ptr_array_index(td->streams, stream_id);
1857 if (!stream) {
1858 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
1859 "in metadata.\n", stream_id);
1860 ret = -EINVAL;
1861 goto end;
1862 }
1863 file_stream->parent.stream_class = stream;
1864 ret = create_stream_definitions(td, &file_stream->parent);
1865 end:
1866 return ret;
1867 }
1868
1869 static
1870 int ctf_open_mmap_stream_read(struct ctf_trace *td,
1871 struct bt_mmap_stream *mmap_info,
1872 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1873 int whence))
1874 {
1875 int ret;
1876 struct ctf_file_stream *file_stream;
1877
1878 file_stream = g_new0(struct ctf_file_stream, 1);
1879 file_stream->pos.last_offset = LAST_OFFSET_POISON;
1880 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1881
1882 file_stream->pos.packet_seek = packet_seek;
1883
1884 ret = create_trace_definitions(td, &file_stream->parent);
1885 if (ret) {
1886 goto error_def;
1887 }
1888
1889 ret = prepare_mmap_stream_definition(td, file_stream);
1890 if (ret)
1891 goto error_index;
1892
1893 /*
1894 * For now, only a single clock per trace is supported.
1895 */
1896 file_stream->parent.current_clock = td->single_clock;
1897
1898 /* Add stream file to stream class */
1899 g_ptr_array_add(file_stream->parent.stream_class->streams,
1900 &file_stream->parent);
1901 return 0;
1902
1903 error_index:
1904 if (file_stream->parent.trace_packet_header)
1905 bt_definition_unref(&file_stream->parent.trace_packet_header->p);
1906 error_def:
1907 g_free(file_stream);
1908 return ret;
1909 }
1910
1911 static
1912 int ctf_open_mmap_trace_read(struct ctf_trace *td,
1913 struct bt_mmap_stream_list *mmap_list,
1914 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1915 int whence),
1916 FILE *metadata_fp)
1917 {
1918 int ret;
1919 struct bt_mmap_stream *mmap_info;
1920
1921 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
1922 if (ret) {
1923 goto error;
1924 }
1925
1926 /*
1927 * for each stream, try to open, check magic number, and get the
1928 * stream ID to add to the right location in the stream array.
1929 */
1930 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
1931 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
1932 if (ret) {
1933 fprintf(stderr, "[error] Open file mmap stream error.\n");
1934 goto error;
1935 }
1936 }
1937
1938 return 0;
1939
1940 error:
1941 return ret;
1942 }
1943
1944 static
1945 struct bt_trace_descriptor *ctf_open_mmap_trace(
1946 struct bt_mmap_stream_list *mmap_list,
1947 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
1948 int whence),
1949 FILE *metadata_fp)
1950 {
1951 struct ctf_trace *td;
1952 int ret;
1953
1954 if (!metadata_fp) {
1955 fprintf(stderr, "[error] No metadata file pointer associated, "
1956 "required for mmap parsing\n");
1957 goto error;
1958 }
1959 if (!packet_seek) {
1960 fprintf(stderr, "[error] packet_seek function undefined.\n");
1961 goto error;
1962 }
1963 td = g_new0(struct ctf_trace, 1);
1964 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
1965 if (ret)
1966 goto error_free;
1967
1968 return &td->parent;
1969
1970 error_free:
1971 g_free(td);
1972 error:
1973 return NULL;
1974 }
1975
1976 static
1977 int ctf_convert_index_timestamp(struct bt_trace_descriptor *tdp)
1978 {
1979 int i, j, k;
1980 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
1981
1982 /* for each stream_class */
1983 for (i = 0; i < td->streams->len; i++) {
1984 struct ctf_stream_declaration *stream_class;
1985
1986 stream_class = g_ptr_array_index(td->streams, i);
1987 if (!stream_class)
1988 continue;
1989 /* for each file_stream */
1990 for (j = 0; j < stream_class->streams->len; j++) {
1991 struct ctf_stream_definition *stream;
1992 struct ctf_stream_pos *stream_pos;
1993 struct ctf_file_stream *cfs;
1994
1995 stream = g_ptr_array_index(stream_class->streams, j);
1996 if (!stream)
1997 continue;
1998 cfs = container_of(stream, struct ctf_file_stream,
1999 parent);
2000 stream_pos = &cfs->pos;
2001 if (!stream_pos->packet_cycles_index)
2002 continue;
2003
2004 for (k = 0; k < stream_pos->packet_cycles_index->len; k++) {
2005 struct packet_index *index;
2006 struct packet_index new_index;
2007
2008 index = &g_array_index(stream_pos->packet_cycles_index,
2009 struct packet_index, k);
2010 memcpy(&new_index, index,
2011 sizeof(struct packet_index));
2012 new_index.timestamp_begin =
2013 ctf_get_real_timestamp(stream,
2014 index->timestamp_begin);
2015 new_index.timestamp_end =
2016 ctf_get_real_timestamp(stream,
2017 index->timestamp_end);
2018 g_array_append_val(stream_pos->packet_real_index,
2019 new_index);
2020 }
2021 }
2022 }
2023 return 0;
2024 }
2025
2026 static
2027 int ctf_close_file_stream(struct ctf_file_stream *file_stream)
2028 {
2029 int ret;
2030
2031 ret = ctf_fini_pos(&file_stream->pos);
2032 if (ret) {
2033 fprintf(stderr, "Error on ctf_fini_pos\n");
2034 return -1;
2035 }
2036 ret = close(file_stream->pos.fd);
2037 if (ret) {
2038 perror("Error closing file fd");
2039 return -1;
2040 }
2041 return 0;
2042 }
2043
2044 static
2045 int ctf_close_trace(struct bt_trace_descriptor *tdp)
2046 {
2047 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
2048 int ret;
2049
2050 if (td->streams) {
2051 int i;
2052
2053 for (i = 0; i < td->streams->len; i++) {
2054 struct ctf_stream_declaration *stream;
2055 int j;
2056
2057 stream = g_ptr_array_index(td->streams, i);
2058 if (!stream)
2059 continue;
2060 for (j = 0; j < stream->streams->len; j++) {
2061 struct ctf_file_stream *file_stream;
2062 file_stream = container_of(g_ptr_array_index(stream->streams, j),
2063 struct ctf_file_stream, parent);
2064 ret = ctf_close_file_stream(file_stream);
2065 if (ret)
2066 return ret;
2067 }
2068 }
2069 }
2070 ctf_destroy_metadata(td);
2071 ret = close(td->dirfd);
2072 if (ret) {
2073 perror("Error closing dirfd");
2074 return ret;
2075 }
2076 ret = closedir(td->dir);
2077 if (ret) {
2078 perror("Error closedir");
2079 return ret;
2080 }
2081 free(td->metadata_string);
2082 g_free(td);
2083 return 0;
2084 }
2085
2086 static
2087 void ctf_set_context(struct bt_trace_descriptor *descriptor,
2088 struct bt_context *ctx)
2089 {
2090 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2091 parent);
2092
2093 td->ctx = ctx;
2094 }
2095
2096 static
2097 void ctf_set_handle(struct bt_trace_descriptor *descriptor,
2098 struct bt_trace_handle *handle)
2099 {
2100 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
2101 parent);
2102
2103 td->handle = handle;
2104 }
2105
2106 static
2107 void __attribute__((constructor)) ctf_init(void)
2108 {
2109 int ret;
2110
2111 ctf_format.name = g_quark_from_static_string("ctf");
2112 ret = bt_register_format(&ctf_format);
2113 assert(!ret);
2114 }
2115
2116 static
2117 void __attribute__((destructor)) ctf_exit(void)
2118 {
2119 bt_unregister_format(&ctf_format);
2120 }
This page took 0.129818 seconds and 5 git commands to generate.