Fix: segfault on error handling
[babeltrace.git] / formats / ctf / ctf.c
CommitLineData
fc93b2bd
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * Format registration.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
fc93b2bd 9 *
ccd7e1c8
MD
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:
fc93b2bd 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
fc93b2bd
MD
19 */
20
21#include <babeltrace/format.h>
22#include <babeltrace/ctf/types.h>
bbefb8dd 23#include <babeltrace/ctf/metadata.h>
70bd0a12 24#include <babeltrace/babeltrace-internal.h>
e4195791 25#include <babeltrace/ctf/events-internal.h>
98a04903
JD
26#include <babeltrace/trace-handle-internal.h>
27#include <babeltrace/context-internal.h>
43e34335
MD
28#include <babeltrace/uuid.h>
29#include <babeltrace/endian.h>
0f980a35 30#include <inttypes.h>
b4c19c1e 31#include <stdio.h>
0f980a35 32#include <sys/mman.h>
bbefb8dd 33#include <errno.h>
bbefb8dd 34#include <sys/types.h>
65102a8c 35#include <sys/stat.h>
bbefb8dd 36#include <fcntl.h>
65102a8c 37#include <dirent.h>
bbefb8dd 38#include <glib.h>
65102a8c
MD
39#include <unistd.h>
40#include <stdlib.h>
41
65102a8c
MD
42#include "metadata/ctf-scanner.h"
43#include "metadata/ctf-parser.h"
44#include "metadata/ctf-ast.h"
c34ea0fa 45#include "events-private.h"
f8370579 46#include "memstream.h"
65102a8c 47
0f980a35
MD
48/*
49 * We currently simply map a page to read the packet header and packet
8c572eba 50 * context to get the packet length and content length. (in bits)
0f980a35 51 */
8c572eba
MD
52#define MAX_PACKET_HEADER_LEN (getpagesize() * CHAR_BIT)
53#define WRITE_PACKET_LEN (getpagesize() * 8 * CHAR_BIT)
0f980a35 54
a0fe7d97
MD
55#ifndef min
56#define min(a, b) (((a) < (b)) ? (a) : (b))
57#endif
58
7d97fad9
MD
59#define NSEC_PER_SEC 1000000000ULL
60
61int opt_clock_raw,
62 opt_clock_seconds,
63 opt_clock_date,
64 opt_clock_gmt;
65
66uint64_t opt_clock_offset;
67
65102a8c 68extern int yydebug;
bbefb8dd 69
e9378815 70static
5b80ddfb 71struct trace_descriptor *ctf_open_trace(const char *path, int flags,
20d0dcf9
MD
72 void (*packet_seek)(struct stream_pos *pos, size_t index,
73 int whence),
74 FILE *metadata_fp);
e9378815 75static
f571dfb1
JD
76struct trace_descriptor *ctf_open_mmap_trace(
77 struct mmap_stream_list *mmap_list,
20d0dcf9
MD
78 void (*packet_seek)(struct stream_pos *pos, size_t index,
79 int whence),
f571dfb1 80 FILE *metadata_fp);
98a04903
JD
81static
82void ctf_set_context(struct trace_descriptor *descriptor,
83 struct bt_context *ctx);
84static
85void ctf_set_handle(struct trace_descriptor *descriptor,
86 struct bt_trace_handle *handle);
f571dfb1
JD
87
88static
bbefb8dd 89void ctf_close_trace(struct trace_descriptor *descriptor);
30c276af
JD
90static
91uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
92 struct bt_trace_handle *handle);
93static
94uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
95 struct bt_trace_handle *handle);
fc93b2bd 96
1ae19169
MD
97static
98rw_dispatch read_dispatch_table[] = {
d11e9c49
MD
99 [ CTF_TYPE_INTEGER ] = ctf_integer_read,
100 [ CTF_TYPE_FLOAT ] = ctf_float_read,
101 [ CTF_TYPE_ENUM ] = ctf_enum_read,
102 [ CTF_TYPE_STRING ] = ctf_string_read,
103 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
104 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
105 [ CTF_TYPE_ARRAY ] = ctf_array_read,
106 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_read,
d11e9c49
MD
107};
108
1ae19169
MD
109static
110rw_dispatch write_dispatch_table[] = {
d11e9c49
MD
111 [ CTF_TYPE_INTEGER ] = ctf_integer_write,
112 [ CTF_TYPE_FLOAT ] = ctf_float_write,
113 [ CTF_TYPE_ENUM ] = ctf_enum_write,
114 [ CTF_TYPE_STRING ] = ctf_string_write,
115 [ CTF_TYPE_STRUCT ] = ctf_struct_rw,
116 [ CTF_TYPE_VARIANT ] = ctf_variant_rw,
81dee1bb
MD
117 [ CTF_TYPE_ARRAY ] = ctf_array_write,
118 [ CTF_TYPE_SEQUENCE ] = ctf_sequence_write,
d11e9c49
MD
119};
120
1ae19169 121static
d11e9c49 122struct format ctf_format = {
bbefb8dd 123 .open_trace = ctf_open_trace,
f571dfb1 124 .open_mmap_trace = ctf_open_mmap_trace,
bbefb8dd 125 .close_trace = ctf_close_trace,
98a04903
JD
126 .set_context = ctf_set_context,
127 .set_handle = ctf_set_handle,
30c276af
JD
128 .timestamp_begin = ctf_timestamp_begin,
129 .timestamp_end = ctf_timestamp_end,
fc93b2bd
MD
130};
131
30c276af
JD
132static
133uint64_t ctf_timestamp_begin(struct trace_descriptor *descriptor,
134 struct bt_trace_handle *handle)
135{
136 struct ctf_trace *tin;
137 uint64_t begin = ULLONG_MAX;
138 int i, j;
139
140 tin = container_of(descriptor, struct ctf_trace, parent);
141
142 if (!tin)
143 goto error;
144
145 /* for each stream_class */
146 for (i = 0; i < tin->streams->len; i++) {
147 struct ctf_stream_declaration *stream_class;
148
149 stream_class = g_ptr_array_index(tin->streams, i);
150 /* for each file_stream */
151 for (j = 0; j < stream_class->streams->len; j++) {
152 struct ctf_stream_definition *stream;
153 struct ctf_file_stream *cfs;
154 struct ctf_stream_pos *stream_pos;
155 struct packet_index *index;
156
157 stream = g_ptr_array_index(stream_class->streams, j);
158 cfs = container_of(stream, struct ctf_file_stream,
159 parent);
160 stream_pos = &cfs->pos;
161
162 index = &g_array_index(stream_pos->packet_index,
163 struct packet_index, 0);
164 if (index->timestamp_begin < begin)
165 begin = index->timestamp_begin;
166 }
167 }
168
169 return begin;
170
171error:
172 return -1ULL;
173}
174
175static
176uint64_t ctf_timestamp_end(struct trace_descriptor *descriptor,
177 struct bt_trace_handle *handle)
178{
179 struct ctf_trace *tin;
180 uint64_t end = 0;
181 int i, j;
182
183 tin = container_of(descriptor, struct ctf_trace, parent);
184
185 if (!tin)
186 goto error;
187
188 /* for each stream_class */
189 for (i = 0; i < tin->streams->len; i++) {
190 struct ctf_stream_declaration *stream_class;
191
192 stream_class = g_ptr_array_index(tin->streams, i);
193 /* for each file_stream */
194 for (j = 0; j < stream_class->streams->len; j++) {
195 struct ctf_stream_definition *stream;
196 struct ctf_file_stream *cfs;
197 struct ctf_stream_pos *stream_pos;
198 struct packet_index *index;
199
200 stream = g_ptr_array_index(stream_class->streams, j);
201 cfs = container_of(stream, struct ctf_file_stream,
202 parent);
203 stream_pos = &cfs->pos;
204
205 index = &g_array_index(stream_pos->packet_index,
206 struct packet_index,
207 stream_pos->packet_index->len - 1);
208 if (index->timestamp_end > end)
209 end = index->timestamp_end;
210 }
211 }
212
213 return end;
214
215error:
216 return -1ULL;
217}
218
25ccc85b
MD
219/*
220 * Update stream current timestamp, keep at clock frequency.
221 */
2b9a764d 222static
9e88d150 223void ctf_update_timestamp(struct ctf_stream_definition *stream,
2b9a764d
MD
224 struct definition_integer *integer_definition)
225{
226 struct declaration_integer *integer_declaration =
227 integer_definition->declaration;
228 uint64_t oldval, newval, updateval;
229
7f3f572b 230 if (unlikely(integer_declaration->len == 64)) {
2b9a764d
MD
231 stream->timestamp = integer_definition->value._unsigned;
232 return;
233 }
234 /* keep low bits */
235 oldval = stream->timestamp;
236 oldval &= (1ULL << integer_declaration->len) - 1;
237 newval = integer_definition->value._unsigned;
238 /* Test for overflow by comparing low bits */
239 if (newval < oldval)
240 newval += 1ULL << integer_declaration->len;
241 /* updateval contains old high bits, and new low bits (sum) */
242 updateval = stream->timestamp;
243 updateval &= ~((1ULL << integer_declaration->len) - 1);
244 updateval += newval;
fca04958 245 stream->prev_timestamp = stream->timestamp;
2b9a764d
MD
246 stream->timestamp = updateval;
247}
248
25ccc85b
MD
249/*
250 * Print timestamp, rescaling clock frequency to nanoseconds and
251 * applying offsets as needed (unix time).
252 */
7d97fad9 253void ctf_print_timestamp(FILE *fp,
9e88d150 254 struct ctf_stream_definition *stream,
7d97fad9
MD
255 uint64_t timestamp)
256{
257 uint64_t ts_sec = 0, ts_nsec;
7d97fad9 258
c34ea0fa
MD
259 if (opt_clock_raw) {
260 ts_nsec = ctf_get_timestamp_raw(stream, timestamp);
25ccc85b 261 } else {
c34ea0fa 262 ts_nsec = ctf_get_timestamp(stream, timestamp);
25ccc85b 263 }
7d97fad9 264
c34ea0fa 265 /* Add command-line offset */
7d97fad9
MD
266 ts_sec += opt_clock_offset;
267
268 ts_sec += ts_nsec / NSEC_PER_SEC;
269 ts_nsec = ts_nsec % NSEC_PER_SEC;
270
271 if (!opt_clock_seconds) {
272 struct tm tm;
273 time_t time_s = (time_t) ts_sec;
274
275 if (!opt_clock_gmt) {
276 struct tm *res;
277
278 res = localtime_r(&time_s, &tm);
279 if (!res) {
280 fprintf(stderr, "[warning] Unable to get localtime.\n");
281 goto seconds;
282 }
283 } else {
284 struct tm *res;
285
286 res = gmtime_r(&time_s, &tm);
287 if (!res) {
288 fprintf(stderr, "[warning] Unable to get gmtime.\n");
289 goto seconds;
290 }
291 }
292 if (opt_clock_date) {
293 char timestr[26];
294 size_t res;
295
296 /* Print date and time */
297 res = strftime(timestr, sizeof(timestr),
298 "%F ", &tm);
299 if (!res) {
300 fprintf(stderr, "[warning] Unable to print ascii time.\n");
301 goto seconds;
302 }
303 fprintf(fp, "%s", timestr);
304 }
305 /* Print time in HH:MM:SS.ns */
306 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
307 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
308 goto end;
309 }
310seconds:
311 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
312 ts_sec, ts_nsec);
313
314end:
315 return;
316}
317
31262354 318static
9e88d150 319int ctf_read_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
31262354
MD
320{
321 struct ctf_stream_pos *pos =
322 container_of(ppos, struct ctf_stream_pos, parent);
f380e105 323 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 324 struct ctf_event_definition *event;
31262354 325 uint64_t id = 0;
31262354
MD
326 int ret;
327
3abe83c7
MD
328 /* We need to check for EOF here for empty files. */
329 if (unlikely(pos->offset == EOF))
330 return EOF;
331
5f643ed7
MD
332 ctf_pos_get_event(pos);
333
90fcbacc
JD
334 /* save the current position as a restore point */
335 pos->last_offset = pos->offset;
336 /* we just read the event, it is consumed when used by the caller */
337 stream->consumed = 0;
338
3abe83c7
MD
339 /*
340 * This is the EOF check after we've advanced the position in
341 * ctf_pos_get_event.
342 */
7f3f572b 343 if (unlikely(pos->offset == EOF))
31262354 344 return EOF;
5f643ed7 345 assert(pos->offset < pos->content_size);
31262354
MD
346
347 /* Read event header */
7f3f572b 348 if (likely(stream->stream_event_header)) {
a35173fe 349 struct definition_integer *integer_definition;
ccdb988e 350 struct definition *variant;
a35173fe 351
e28d4618 352 ret = generic_rw(ppos, &stream->stream_event_header->p);
7f3f572b 353 if (unlikely(ret))
31262354
MD
354 goto error;
355 /* lookup event id */
e28d4618 356 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
357 if (integer_definition) {
358 id = integer_definition->value._unsigned;
359 } else {
360 struct definition_enum *enum_definition;
361
e28d4618 362 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
a35173fe
MD
363 if (enum_definition) {
364 id = enum_definition->integer->value._unsigned;
365 }
31262354 366 }
764af3f4 367
e28d4618 368 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
369 if (variant) {
370 integer_definition = lookup_integer(variant, "id", FALSE);
371 if (integer_definition) {
372 id = integer_definition->value._unsigned;
373 }
374 }
c87a8eb2 375 stream->event_id = id;
ccdb988e 376
764af3f4 377 /* lookup timestamp */
5e2eb0ae 378 stream->has_timestamp = 0;
e28d4618 379 integer_definition = lookup_integer(&stream->stream_event_header->p, "timestamp", FALSE);
a35173fe 380 if (integer_definition) {
2b9a764d 381 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 382 stream->has_timestamp = 1;
a35173fe 383 } else {
ccdb988e
MD
384 if (variant) {
385 integer_definition = lookup_integer(variant, "timestamp", FALSE);
a35173fe 386 if (integer_definition) {
2b9a764d 387 ctf_update_timestamp(stream, integer_definition);
5e2eb0ae 388 stream->has_timestamp = 1;
a35173fe
MD
389 }
390 }
764af3f4 391 }
31262354
MD
392 }
393
394 /* Read stream-declared event context */
e28d4618
MD
395 if (stream->stream_event_context) {
396 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
397 if (ret)
398 goto error;
399 }
400
7f3f572b 401 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 402 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
403 return -EINVAL;
404 }
e28d4618 405 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 406 if (unlikely(!event)) {
3394d22e 407 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
408 return -EINVAL;
409 }
410
411 /* Read event-declared event context */
e28d4618
MD
412 if (event->event_context) {
413 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
414 if (ret)
415 goto error;
416 }
417
418 /* Read event payload */
7f3f572b 419 if (likely(event->event_fields)) {
e28d4618 420 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
421 if (ret)
422 goto error;
423 }
424
425 return 0;
426
427error:
3394d22e 428 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
31262354
MD
429 return ret;
430}
431
432static
9e88d150 433int ctf_write_event(struct stream_pos *pos, struct ctf_stream_definition *stream)
31262354 434{
f380e105 435 struct ctf_stream_declaration *stream_class = stream->stream_class;
c716f83b 436 struct ctf_event_definition *event;
c87a8eb2 437 uint64_t id;
31262354
MD
438 int ret;
439
c87a8eb2
MD
440 id = stream->event_id;
441
31262354 442 /* print event header */
7f3f572b 443 if (likely(stream->stream_event_header)) {
e28d4618 444 ret = generic_rw(pos, &stream->stream_event_header->p);
31262354
MD
445 if (ret)
446 goto error;
447 }
448
449 /* print stream-declared event context */
e28d4618
MD
450 if (stream->stream_event_context) {
451 ret = generic_rw(pos, &stream->stream_event_context->p);
31262354
MD
452 if (ret)
453 goto error;
454 }
455
7f3f572b 456 if (unlikely(id >= stream_class->events_by_id->len)) {
3394d22e 457 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
458 return -EINVAL;
459 }
e28d4618 460 event = g_ptr_array_index(stream->events_by_id, id);
7f3f572b 461 if (unlikely(!event)) {
3394d22e 462 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
463 return -EINVAL;
464 }
465
466 /* print event-declared event context */
e28d4618
MD
467 if (event->event_context) {
468 ret = generic_rw(pos, &event->event_context->p);
31262354
MD
469 if (ret)
470 goto error;
471 }
472
473 /* Read and print event payload */
7f3f572b 474 if (likely(event->event_fields)) {
e28d4618 475 ret = generic_rw(pos, &event->event_fields->p);
31262354
MD
476 if (ret)
477 goto error;
478 }
479
480 return 0;
481
482error:
3394d22e 483 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
31262354
MD
484 return ret;
485}
486
8563e754 487void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags)
8c572eba
MD
488{
489 pos->fd = fd;
490 pos->mmap_offset = 0;
491 pos->packet_size = 0;
492 pos->content_size = 0;
493 pos->content_size_loc = NULL;
494 pos->base = NULL;
495 pos->offset = 0;
496 pos->dummy = false;
8c572eba 497 pos->cur_index = 0;
8563e754
MD
498 if (fd >= 0)
499 pos->packet_index = g_array_new(FALSE, TRUE,
500 sizeof(struct packet_index));
501 else
502 pos->packet_index = NULL;
8563e754
MD
503 switch (open_flags & O_ACCMODE) {
504 case O_RDONLY:
505 pos->prot = PROT_READ;
506 pos->flags = MAP_PRIVATE;
507 pos->parent.rw_table = read_dispatch_table;
31262354 508 pos->parent.event_cb = ctf_read_event;
8563e754 509 break;
8563e754
MD
510 case O_RDWR:
511 pos->prot = PROT_WRITE; /* Write has priority */
512 pos->flags = MAP_SHARED;
513 pos->parent.rw_table = write_dispatch_table;
31262354 514 pos->parent.event_cb = ctf_write_event;
8563e754 515 if (fd >= 0)
06789ffd 516 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
8563e754
MD
517 break;
518 default:
519 assert(0);
8c572eba
MD
520 }
521}
522
46322b33 523void ctf_fini_pos(struct ctf_stream_pos *pos)
8c572eba
MD
524{
525 int ret;
526
527 if (pos->prot == PROT_WRITE && pos->content_size_loc)
528 *pos->content_size_loc = pos->offset;
529 if (pos->base) {
530 /* unmap old base */
531 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
532 if (ret) {
3394d22e 533 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
8c572eba
MD
534 strerror(errno));
535 assert(0);
536 }
537 }
538 (void) g_array_free(pos->packet_index, TRUE);
539}
540
20d0dcf9
MD
541/*
542 * for SEEK_CUR: go to next packet.
543 * for SEEK_POS: go to packet numer (index).
544 */
545void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence)
0f980a35 546{
d6425aaf
MD
547 struct ctf_stream_pos *pos =
548 container_of(stream_pos, struct ctf_stream_pos, parent);
75cc2c35
MD
549 struct ctf_file_stream *file_stream =
550 container_of(pos, struct ctf_file_stream, pos);
0f980a35 551 int ret;
8c572eba 552 off_t off;
20d0dcf9 553 struct packet_index *packet_index;
0f980a35 554
8c572eba
MD
555 if (pos->prot == PROT_WRITE && pos->content_size_loc)
556 *pos->content_size_loc = pos->offset;
0f980a35
MD
557
558 if (pos->base) {
559 /* unmap old base */
8c572eba 560 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 561 if (ret) {
3394d22e 562 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
563 strerror(errno));
564 assert(0);
565 }
847bf71a 566 pos->base = NULL;
0f980a35
MD
567 }
568
8c572eba 569 /*
46322b33 570 * The caller should never ask for ctf_move_pos across packets,
8c572eba
MD
571 * except to get exactly at the beginning of the next packet.
572 */
573 if (pos->prot == PROT_WRITE) {
989c73bc
MD
574 switch (whence) {
575 case SEEK_CUR:
576 /* The writer will add padding */
8c572eba 577 pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
989c73bc
MD
578 break;
579 case SEEK_SET:
20d0dcf9 580 assert(index == 0); /* only seek supported for now */
989c73bc
MD
581 pos->cur_index = 0;
582 break;
583 default:
584 assert(0);
585 }
8c572eba
MD
586 pos->content_size = -1U; /* Unknown at this point */
587 pos->packet_size = WRITE_PACKET_LEN;
989c73bc
MD
588 off = posix_fallocate(pos->fd, pos->mmap_offset,
589 pos->packet_size / CHAR_BIT);
8c572eba 590 assert(off >= 0);
847bf71a 591 pos->offset = 0;
8c572eba 592 } else {
5f643ed7 593 read_next_packet:
847bf71a
MD
594 switch (whence) {
595 case SEEK_CUR:
41e82e00
MD
596 {
597 uint32_t events_discarded_diff;
598
7d97fad9
MD
599 if (pos->offset == EOF) {
600 return;
601 }
3217ac37 602 /* For printing discarded event count */
20d0dcf9 603 packet_index = &g_array_index(pos->packet_index,
41e82e00 604 struct packet_index, pos->cur_index);
20d0dcf9 605 events_discarded_diff = packet_index->events_discarded;
3217ac37 606 file_stream->parent.prev_timestamp_end =
20d0dcf9 607 packet_index->timestamp_end;
41e82e00 608 if (pos->cur_index > 0) {
20d0dcf9 609 packet_index = &g_array_index(pos->packet_index,
41e82e00
MD
610 struct packet_index,
611 pos->cur_index - 1);
20d0dcf9 612 events_discarded_diff -= packet_index->events_discarded;
41e82e00 613 }
fca04958 614 file_stream->parent.events_discarded = events_discarded_diff;
7d97fad9 615 file_stream->parent.prev_timestamp = file_stream->parent.timestamp;
847bf71a 616 /* The reader will expect us to skip padding */
8c572eba 617 ++pos->cur_index;
847bf71a 618 break;
41e82e00 619 }
847bf71a 620 case SEEK_SET:
20d0dcf9 621 pos->cur_index = index;
fca04958 622 file_stream->parent.prev_timestamp = 0;
3217ac37 623 file_stream->parent.prev_timestamp_end = 0;
847bf71a
MD
624 break;
625 default:
626 assert(0);
627 }
628 if (pos->cur_index >= pos->packet_index->len) {
7d97fad9
MD
629 /*
630 * When a stream reaches the end of the
631 * file, we need to show the number of
632 * events discarded ourselves, because
633 * there is no next event scheduled to
634 * be printed in the output.
635 */
636 if (file_stream->parent.events_discarded) {
badea1a2
JD
637 /*
638 * We need to check if we are in trace
639 * read or called from packet indexing.
640 * In this last case, the collection is
641 * not there, so we cannot print the
642 * timestamps.
643 */
644 if ((&file_stream->parent)->stream_class->trace->collection) {
645 fflush(stdout);
646 fprintf(stderr, "[warning] Tracer discarded %d events at end of stream between [",
647 file_stream->parent.events_discarded);
648 ctf_print_timestamp(stderr, &file_stream->parent,
649 file_stream->parent.prev_timestamp);
650 fprintf(stderr, "] and [");
651 ctf_print_timestamp(stderr, &file_stream->parent,
652 file_stream->parent.prev_timestamp_end);
653 fprintf(stderr, "]. You should consider increasing the buffer size.\n");
654 fflush(stderr);
655 }
7d97fad9
MD
656 file_stream->parent.events_discarded = 0;
657 }
670977d3 658 pos->offset = EOF;
847bf71a
MD
659 return;
660 }
20d0dcf9 661 packet_index = &g_array_index(pos->packet_index, struct packet_index,
8c572eba 662 pos->cur_index);
20d0dcf9 663 pos->mmap_offset = packet_index->offset;
8c572eba
MD
664
665 /* Lookup context/packet size in index */
20d0dcf9
MD
666 file_stream->parent.timestamp = packet_index->timestamp_begin;
667 pos->content_size = packet_index->content_size;
668 pos->packet_size = packet_index->packet_size;
669 if (packet_index->data_offset < packet_index->content_size) {
75cc2c35 670 pos->offset = 0; /* will read headers */
20d0dcf9 671 } else if (packet_index->data_offset == packet_index->content_size) {
5f643ed7 672 /* empty packet */
20d0dcf9 673 pos->offset = packet_index->data_offset;
3abe83c7 674 whence = SEEK_CUR;
5f643ed7 675 goto read_next_packet;
7eda6fc7 676 } else {
2b9a764d
MD
677 pos->offset = EOF;
678 return;
679 }
8c572eba 680 }
0f980a35 681 /* map new base. Need mapping length from header. */
8c572eba
MD
682 pos->base = mmap(NULL, pos->packet_size / CHAR_BIT, pos->prot,
683 pos->flags, pos->fd, pos->mmap_offset);
847bf71a 684 if (pos->base == MAP_FAILED) {
3394d22e 685 fprintf(stderr, "[error] mmap error %s.\n",
847bf71a
MD
686 strerror(errno));
687 assert(0);
688 }
75cc2c35
MD
689
690 /* update trace_packet_header and stream_packet_context */
2d0bea29 691 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
75cc2c35 692 /* Read packet header */
2d0bea29 693 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
75cc2c35
MD
694 assert(!ret);
695 }
2d0bea29 696 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
75cc2c35 697 /* Read packet context */
2d0bea29 698 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
75cc2c35
MD
699 assert(!ret);
700 }
0f980a35
MD
701}
702
b4c19c1e
MD
703static
704int packet_metadata(struct ctf_trace *td, FILE *fp)
705{
706 uint32_t magic;
707 size_t len;
708 int ret = 0;
709
710 len = fread(&magic, sizeof(magic), 1, fp);
a0fe7d97 711 if (len != 1) {
b4c19c1e
MD
712 goto end;
713 }
714 if (magic == TSDL_MAGIC) {
715 ret = 1;
716 td->byte_order = BYTE_ORDER;
0d336fdf 717 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
718 } else if (magic == GUINT32_SWAP_LE_BE(TSDL_MAGIC)) {
719 ret = 1;
720 td->byte_order = (BYTE_ORDER == BIG_ENDIAN) ?
721 LITTLE_ENDIAN : BIG_ENDIAN;
0d336fdf 722 CTF_TRACE_SET_FIELD(td, byte_order);
b4c19c1e
MD
723 }
724end:
725 rewind(fp);
726 return ret;
727}
728
5c262147
MD
729/*
730 * Returns 0 on success, -1 on error.
731 */
732static
733int check_version(unsigned int major, unsigned int minor)
734{
735 switch (major) {
736 case 1:
737 switch (minor) {
738 case 8:
739 return 0;
740 default:
741 goto warning;
742 }
743 default:
744 goto warning;
745
746 }
747
748 /* eventually return an error instead of warning */
749warning:
3394d22e 750 fprintf(stderr, "[warning] Unsupported CTF specification version %u.%u. Trying anyway.\n",
5c262147
MD
751 major, minor);
752 return 0;
753}
754
b4c19c1e
MD
755static
756int ctf_open_trace_metadata_packet_read(struct ctf_trace *td, FILE *in,
757 FILE *out)
758{
759 struct metadata_packet_header header;
a0fe7d97 760 size_t readlen, writelen, toread;
f8254867 761 char buf[4096 + 1]; /* + 1 for debug-mode \0 */
b4c19c1e
MD
762 int ret = 0;
763
a91a962e 764 readlen = fread(&header, header_sizeof(header), 1, in);
a0fe7d97 765 if (readlen < 1)
b4c19c1e
MD
766 return -EINVAL;
767
768 if (td->byte_order != BYTE_ORDER) {
769 header.magic = GUINT32_SWAP_LE_BE(header.magic);
770 header.checksum = GUINT32_SWAP_LE_BE(header.checksum);
771 header.content_size = GUINT32_SWAP_LE_BE(header.content_size);
772 header.packet_size = GUINT32_SWAP_LE_BE(header.packet_size);
773 }
774 if (header.checksum)
3394d22e 775 fprintf(stderr, "[warning] checksum verification not supported yet.\n");
b4c19c1e 776 if (header.compression_scheme) {
3394d22e 777 fprintf(stderr, "[error] compression (%u) not supported yet.\n",
b4c19c1e
MD
778 header.compression_scheme);
779 return -EINVAL;
780 }
781 if (header.encryption_scheme) {
3394d22e 782 fprintf(stderr, "[error] encryption (%u) not supported yet.\n",
b4c19c1e
MD
783 header.encryption_scheme);
784 return -EINVAL;
785 }
786 if (header.checksum_scheme) {
3394d22e 787 fprintf(stderr, "[error] checksum (%u) not supported yet.\n",
b4c19c1e
MD
788 header.checksum_scheme);
789 return -EINVAL;
790 }
5c262147
MD
791 if (check_version(header.major, header.minor) < 0)
792 return -EINVAL;
b4c19c1e
MD
793 if (!CTF_TRACE_FIELD_IS_SET(td, uuid)) {
794 memcpy(td->uuid, header.uuid, sizeof(header.uuid));
795 CTF_TRACE_SET_FIELD(td, uuid);
796 } else {
43e34335 797 if (babeltrace_uuid_compare(header.uuid, td->uuid))
b4c19c1e
MD
798 return -EINVAL;
799 }
800
255b2138 801 toread = (header.content_size / CHAR_BIT) - header_sizeof(header);
a0fe7d97
MD
802
803 for (;;) {
f8254867 804 readlen = fread(buf, sizeof(char), min(sizeof(buf) - 1, toread), in);
b4c19c1e
MD
805 if (ferror(in)) {
806 ret = -EINVAL;
807 break;
808 }
4152822b 809 if (babeltrace_debug) {
f8254867 810 buf[readlen] = '\0';
3394d22e 811 fprintf(stderr, "[debug] metadata packet read: %s\n",
4152822b
MD
812 buf);
813 }
814
b4c19c1e
MD
815 writelen = fwrite(buf, sizeof(char), readlen, out);
816 if (writelen < readlen) {
817 ret = -EIO;
818 break;
819 }
820 if (ferror(out)) {
821 ret = -EINVAL;
822 break;
823 }
a0fe7d97
MD
824 toread -= readlen;
825 if (!toread) {
7f4b5c4d 826 ret = 0; /* continue reading next packet */
ddbc52af 827 goto read_padding;
a0fe7d97 828 }
b4c19c1e
MD
829 }
830 return ret;
ddbc52af
MD
831
832read_padding:
833 toread = (header.packet_size - header.content_size) / CHAR_BIT;
834 ret = fseek(in, toread, SEEK_CUR);
835 if (ret < 0) {
3394d22e 836 fprintf(stderr, "[warning] Missing padding at end of file\n");
ddbc52af
MD
837 ret = 0;
838 }
839 return ret;
b4c19c1e
MD
840}
841
842static
843int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp,
844 char **buf)
845{
846 FILE *in, *out;
847 size_t size;
848 int ret;
849
850 in = *fp;
c4f5487e
MD
851 /*
852 * Using strlen on *buf instead of size of open_memstream
853 * because its size includes garbage at the end (after final
854 * \0). This is the allocated size, not the actual string size.
855 */
f8370579 856 out = babeltrace_open_memstream(buf, &size);
a569a564
MD
857 if (out == NULL) {
858 perror("Metadata open_memstream");
b4c19c1e 859 return -errno;
a569a564 860 }
b4c19c1e
MD
861 for (;;) {
862 ret = ctf_open_trace_metadata_packet_read(td, in, out);
7f4b5c4d 863 if (ret) {
b4c19c1e 864 break;
7f4b5c4d
MD
865 }
866 if (feof(in)) {
867 ret = 0;
b4c19c1e
MD
868 break;
869 }
870 }
f8370579
MD
871 /* close to flush the buffer */
872 ret = babeltrace_close_memstream(buf, &size, out);
873 if (ret < 0) {
874 perror("babeltrace_flush_memstream");
875 fclose(in);
876 return -errno;
877 }
b4c19c1e
MD
878 fclose(in);
879 /* open for reading */
f8370579 880 *fp = babeltrace_fmemopen(*buf, strlen(*buf), "rb");
a569a564
MD
881 if (!*fp) {
882 perror("Metadata fmemopen");
883 return -errno;
884 }
b4c19c1e
MD
885 return 0;
886}
887
65102a8c 888static
b086c01a 889int ctf_open_trace_metadata_read(struct ctf_trace *td,
20d0dcf9 890 void (*packet_seek)(struct stream_pos *pos, size_t index,
ae23d232 891 int whence), FILE *metadata_fp)
65102a8c
MD
892{
893 struct ctf_scanner *scanner;
2d0bea29 894 struct ctf_file_stream *metadata_stream;
65102a8c 895 FILE *fp;
b4c19c1e 896 char *buf = NULL;
65102a8c
MD
897 int ret = 0;
898
2d0bea29 899 metadata_stream = g_new0(struct ctf_file_stream, 1);
b086c01a 900
06789ffd
MD
901 if (packet_seek) {
902 metadata_stream->pos.packet_seek = packet_seek;
b086c01a 903 } else {
06789ffd 904 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
905 ret = -1;
906 goto end_stream;
907 }
908
ae23d232
JD
909 if (metadata_fp) {
910 fp = metadata_fp;
911 } else {
912 td->metadata = &metadata_stream->parent;
913 metadata_stream->pos.fd = openat(td->dirfd, "metadata", O_RDONLY);
914 if (metadata_stream->pos.fd < 0) {
3394d22e 915 fprintf(stderr, "Unable to open metadata.\n");
ae23d232
JD
916 return metadata_stream->pos.fd;
917 }
65102a8c 918
ae23d232
JD
919 fp = fdopen(metadata_stream->pos.fd, "r");
920 if (!fp) {
3394d22e 921 fprintf(stderr, "[error] Unable to open metadata stream.\n");
a569a564 922 perror("Metadata stream open");
ae23d232
JD
923 ret = -errno;
924 goto end_stream;
925 }
926 }
65102a8c
MD
927 if (babeltrace_debug)
928 yydebug = 1;
929
b4c19c1e
MD
930 if (packet_metadata(td, fp)) {
931 ret = ctf_open_trace_metadata_stream_read(td, &fp, &buf);
932 if (ret)
933 goto end_packet_read;
da75b0f7 934 } else {
5c262147
MD
935 unsigned int major, minor;
936 ssize_t nr_items;
8c2df3f5 937
da75b0f7 938 td->byte_order = BYTE_ORDER;
8c2df3f5 939
5c262147
MD
940 /* Check text-only metadata header and version */
941 nr_items = fscanf(fp, "/* CTF %u.%u", &major, &minor);
942 if (nr_items < 2)
3394d22e 943 fprintf(stderr, "[warning] Ill-shapen or missing \"/* CTF x.y\" header for text-only metadata.\n");
5c262147
MD
944 if (check_version(major, minor) < 0) {
945 ret = -EINVAL;
946 goto end_packet_read;
947 }
8c2df3f5 948 rewind(fp);
b4c19c1e
MD
949 }
950
65102a8c
MD
951 scanner = ctf_scanner_alloc(fp);
952 if (!scanner) {
3394d22e 953 fprintf(stderr, "[error] Error allocating scanner\n");
65102a8c
MD
954 ret = -ENOMEM;
955 goto end_scanner_alloc;
956 }
957 ret = ctf_scanner_append_ast(scanner);
958 if (ret) {
3394d22e 959 fprintf(stderr, "[error] Error creating AST\n");
65102a8c
MD
960 goto end;
961 }
962
963 if (babeltrace_debug) {
3394d22e 964 ret = ctf_visitor_print_xml(stderr, 0, &scanner->ast->root);
65102a8c 965 if (ret) {
3394d22e 966 fprintf(stderr, "[error] Error visiting AST for XML output\n");
65102a8c
MD
967 goto end;
968 }
969 }
970
3394d22e 971 ret = ctf_visitor_semantic_check(stderr, 0, &scanner->ast->root);
65102a8c 972 if (ret) {
3394d22e 973 fprintf(stderr, "[error] Error in CTF semantic validation %d\n", ret);
65102a8c
MD
974 goto end;
975 }
3394d22e 976 ret = ctf_visitor_construct_metadata(stderr, 0, &scanner->ast->root,
ac5c6ca0 977 td, td->byte_order);
65102a8c 978 if (ret) {
3394d22e 979 fprintf(stderr, "[error] Error in CTF metadata constructor %d\n", ret);
65102a8c
MD
980 goto end;
981 }
982end:
983 ctf_scanner_free(scanner);
984end_scanner_alloc:
b4c19c1e 985end_packet_read:
8210f9da
MD
986 if (fp)
987 fclose(fp);
b4c19c1e 988 free(buf);
65102a8c 989end_stream:
2d0bea29
MD
990 close(metadata_stream->pos.fd);
991 if (ret)
992 g_free(metadata_stream);
0f980a35
MD
993 return ret;
994}
995
e28d4618 996static
c716f83b 997struct ctf_event_definition *create_event_definitions(struct ctf_trace *td,
9e88d150 998 struct ctf_stream_definition *stream,
4716614a 999 struct ctf_event_declaration *event)
e28d4618 1000{
c716f83b 1001 struct ctf_event_definition *stream_event = g_new0(struct ctf_event_definition, 1);
e28d4618
MD
1002
1003 if (event->context_decl) {
1004 struct definition *definition =
1005 event->context_decl->p.definition_new(&event->context_decl->p,
1006 stream->parent_def_scope, 0, 0, "event.context");
1007 if (!definition) {
1008 goto error;
1009 }
42dc00b7 1010 stream_event->event_context = container_of(definition,
e28d4618 1011 struct definition_struct, p);
42dc00b7 1012 stream->parent_def_scope = stream_event->event_context->p.scope;
e28d4618
MD
1013 }
1014 if (event->fields_decl) {
1015 struct definition *definition =
1016 event->fields_decl->p.definition_new(&event->fields_decl->p,
1017 stream->parent_def_scope, 0, 0, "event.fields");
1018 if (!definition) {
1019 goto error;
1020 }
42dc00b7 1021 stream_event->event_fields = container_of(definition,
e28d4618 1022 struct definition_struct, p);
42dc00b7 1023 stream->parent_def_scope = stream_event->event_fields->p.scope;
e28d4618 1024 }
d3ded99d 1025 stream_event->stream = stream;
42dc00b7 1026 return stream_event;
e28d4618
MD
1027
1028error:
42dc00b7
MD
1029 if (stream_event->event_fields)
1030 definition_unref(&stream_event->event_fields->p);
1031 if (stream_event->event_context)
1032 definition_unref(&stream_event->event_context->p);
e28d4618
MD
1033 return NULL;
1034}
1035
1036static
9e88d150 1037int create_stream_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618 1038{
f380e105 1039 struct ctf_stream_declaration *stream_class;
e28d4618
MD
1040 int ret;
1041 int i;
1042
1043 if (stream->stream_definitions_created)
1044 return 0;
1045
1046 stream_class = stream->stream_class;
1047
1048 if (stream_class->packet_context_decl) {
1049 struct definition *definition =
1050 stream_class->packet_context_decl->p.definition_new(&stream_class->packet_context_decl->p,
1051 stream->parent_def_scope, 0, 0, "stream.packet.context");
1052 if (!definition) {
1053 ret = -EINVAL;
1054 goto error;
1055 }
1056 stream->stream_packet_context = container_of(definition,
1057 struct definition_struct, p);
1058 stream->parent_def_scope = stream->stream_packet_context->p.scope;
1059 }
1060 if (stream_class->event_header_decl) {
1061 struct definition *definition =
1062 stream_class->event_header_decl->p.definition_new(&stream_class->event_header_decl->p,
1063 stream->parent_def_scope, 0, 0, "stream.event.header");
1064 if (!definition) {
1065 ret = -EINVAL;
1066 goto error;
1067 }
1068 stream->stream_event_header =
1069 container_of(definition, struct definition_struct, p);
1070 stream->parent_def_scope = stream->stream_event_header->p.scope;
1071 }
1072 if (stream_class->event_context_decl) {
1073 struct definition *definition =
1074 stream_class->event_context_decl->p.definition_new(&stream_class->event_context_decl->p,
1075 stream->parent_def_scope, 0, 0, "stream.event.context");
1076 if (!definition) {
1077 ret = -EINVAL;
1078 goto error;
1079 }
1080 stream->stream_event_context =
1081 container_of(definition, struct definition_struct, p);
1082 stream->parent_def_scope = stream->stream_event_context->p.scope;
1083 }
1084 stream->events_by_id = g_ptr_array_new();
1085 g_ptr_array_set_size(stream->events_by_id, stream_class->events_by_id->len);
1086 for (i = 0; i < stream->events_by_id->len; i++) {
4716614a 1087 struct ctf_event_declaration *event = g_ptr_array_index(stream_class->events_by_id, i);
c716f83b 1088 struct ctf_event_definition *stream_event;
e28d4618
MD
1089
1090 if (!event)
1091 continue;
42dc00b7
MD
1092 stream_event = create_event_definitions(td, stream, event);
1093 if (!stream_event)
e28d4618 1094 goto error_event;
42dc00b7 1095 g_ptr_array_index(stream->events_by_id, i) = stream_event;
e28d4618
MD
1096 }
1097 return 0;
1098
1099error_event:
1100 for (i = 0; i < stream->events_by_id->len; i++) {
c716f83b 1101 struct ctf_event_definition *stream_event = g_ptr_array_index(stream->events_by_id, i);
42dc00b7
MD
1102 if (stream_event)
1103 g_free(stream_event);
e28d4618
MD
1104 }
1105 g_ptr_array_free(stream->events_by_id, TRUE);
1106error:
1107 if (stream->stream_event_context)
1108 definition_unref(&stream->stream_event_context->p);
1109 if (stream->stream_event_header)
1110 definition_unref(&stream->stream_event_header->p);
1111 if (stream->stream_packet_context)
1112 definition_unref(&stream->stream_packet_context->p);
1113 return ret;
1114}
1115
0f980a35
MD
1116
1117static
46322b33 1118int create_stream_packet_index(struct ctf_trace *td,
0f980a35
MD
1119 struct ctf_file_stream *file_stream)
1120{
f380e105 1121 struct ctf_stream_declaration *stream;
0f980a35 1122 int len_index;
46322b33 1123 struct ctf_stream_pos *pos;
0f980a35
MD
1124 struct stat filestats;
1125 struct packet_index packet_index;
1126 int first_packet = 1;
1127 int ret;
1128
1129 pos = &file_stream->pos;
1130
1131 ret = fstat(pos->fd, &filestats);
1132 if (ret < 0)
1133 return ret;
1134
8895362d
MD
1135 if (filestats.st_size < MAX_PACKET_HEADER_LEN / CHAR_BIT)
1136 return -EINVAL;
1137
0f980a35
MD
1138 for (pos->mmap_offset = 0; pos->mmap_offset < filestats.st_size; ) {
1139 uint64_t stream_id = 0;
1140
1141 if (pos->base) {
1142 /* unmap old base */
8c572eba 1143 ret = munmap(pos->base, pos->packet_size / CHAR_BIT);
0f980a35 1144 if (ret) {
3394d22e 1145 fprintf(stderr, "[error] Unable to unmap old base: %s.\n",
0f980a35
MD
1146 strerror(errno));
1147 return ret;
1148 }
8c572eba 1149 pos->base = NULL;
0f980a35
MD
1150 }
1151 /* map new base. Need mapping length from header. */
8c572eba 1152 pos->base = mmap(NULL, MAX_PACKET_HEADER_LEN / CHAR_BIT, PROT_READ,
0f980a35 1153 MAP_PRIVATE, pos->fd, pos->mmap_offset);
dc48ecad
MD
1154 pos->content_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
1155 pos->packet_size = MAX_PACKET_HEADER_LEN; /* Unknown at this point */
0f980a35
MD
1156 pos->offset = 0; /* Position of the packet header */
1157
8c572eba
MD
1158 packet_index.offset = pos->mmap_offset;
1159 packet_index.content_size = 0;
1160 packet_index.packet_size = 0;
75cc2c35
MD
1161 packet_index.timestamp_begin = 0;
1162 packet_index.timestamp_end = 0;
41e82e00 1163 packet_index.events_discarded = 0;
8c572eba 1164
0f980a35 1165 /* read and check header, set stream id (and check) */
2d0bea29 1166 if (file_stream->parent.trace_packet_header) {
0f980a35 1167 /* Read packet header */
2d0bea29 1168 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
c5e74408
MD
1169 if (ret)
1170 return ret;
2d0bea29 1171 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("magic"));
0f980a35 1172 if (len_index >= 0) {
b1a2f580 1173 struct definition *field;
f4c56ac2 1174 uint64_t magic;
0f980a35 1175
2d0bea29 1176 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
f4c56ac2
MD
1177 magic = get_unsigned_int(field);
1178 if (magic != CTF_MAGIC) {
3394d22e 1179 fprintf(stderr, "[error] Invalid magic number 0x%" PRIX64 " at packet %u (file offset %zd).\n",
f4c56ac2 1180 magic,
8c572eba
MD
1181 file_stream->pos.packet_index->len,
1182 (ssize_t) pos->mmap_offset);
0f980a35
MD
1183 return -EINVAL;
1184 }
1185 }
1186
1187 /* check uuid */
2d0bea29 1188 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("uuid"));
0f980a35
MD
1189 if (len_index >= 0) {
1190 struct definition_array *defarray;
b1a2f580 1191 struct definition *field;
0f980a35 1192 uint64_t i;
43e34335 1193 uint8_t uuidval[BABELTRACE_UUID_LEN];
0f980a35 1194
2d0bea29 1195 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
b1a2f580
MD
1196 assert(field->declaration->id == CTF_TYPE_ARRAY);
1197 defarray = container_of(field, struct definition_array, p);
43e34335 1198 assert(array_len(defarray) == BABELTRACE_UUID_LEN);
0f980a35 1199
43e34335 1200 for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
0f980a35 1201 struct definition *elem;
0f980a35
MD
1202
1203 elem = array_index(defarray, i);
f4c56ac2 1204 uuidval[i] = get_unsigned_int(elem);
0f980a35 1205 }
43e34335 1206 ret = babeltrace_uuid_compare(td->uuid, uuidval);
0f980a35 1207 if (ret) {
3394d22e 1208 fprintf(stderr, "[error] Unique Universal Identifiers do not match.\n");
0f980a35
MD
1209 return -EINVAL;
1210 }
1211 }
1212
1213
2d0bea29 1214 len_index = struct_declaration_lookup_field_index(file_stream->parent.trace_packet_header->declaration, g_quark_from_static_string("stream_id"));
0f980a35 1215 if (len_index >= 0) {
b1a2f580 1216 struct definition *field;
0f980a35 1217
2d0bea29 1218 field = struct_definition_get_field_from_index(file_stream->parent.trace_packet_header, len_index);
f4c56ac2 1219 stream_id = get_unsigned_int(field);
0f980a35
MD
1220 }
1221 }
1222
2d0bea29 1223 if (!first_packet && file_stream->parent.stream_id != stream_id) {
3394d22e 1224 fprintf(stderr, "[error] Stream ID is changing within a stream.\n");
0f980a35
MD
1225 return -EINVAL;
1226 }
1227 if (first_packet) {
2d0bea29 1228 file_stream->parent.stream_id = stream_id;
46322b33 1229 if (stream_id >= td->streams->len) {
3394d22e 1230 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
0f980a35
MD
1231 return -EINVAL;
1232 }
46322b33 1233 stream = g_ptr_array_index(td->streams, stream_id);
0f980a35 1234 if (!stream) {
3394d22e 1235 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared in metadata.\n", stream_id);
0f980a35
MD
1236 return -EINVAL;
1237 }
2d0bea29 1238 file_stream->parent.stream_class = stream;
01c76b24 1239 ret = create_stream_definitions(td, &file_stream->parent);
862434ec
MD
1240 if (ret)
1241 return ret;
0f980a35
MD
1242 }
1243 first_packet = 0;
1244
2d0bea29 1245 if (file_stream->parent.stream_packet_context) {
dc48ecad 1246 /* Read packet context */
2d0bea29 1247 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
c5e74408
MD
1248 if (ret)
1249 return ret;
dc48ecad 1250 /* read content size from header */
2d0bea29 1251 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("content_size"));
dc48ecad 1252 if (len_index >= 0) {
b1a2f580 1253 struct definition *field;
dc48ecad 1254
2d0bea29 1255 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1256 packet_index.content_size = get_unsigned_int(field);
dc48ecad
MD
1257 } else {
1258 /* Use file size for packet size */
8c572eba 1259 packet_index.content_size = filestats.st_size * CHAR_BIT;
dc48ecad
MD
1260 }
1261
1262 /* read packet size from header */
2d0bea29 1263 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("packet_size"));
dc48ecad 1264 if (len_index >= 0) {
b1a2f580 1265 struct definition *field;
dc48ecad 1266
2d0bea29 1267 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1268 packet_index.packet_size = get_unsigned_int(field);
dc48ecad
MD
1269 } else {
1270 /* Use content size if non-zero, else file size */
8c572eba 1271 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
dc48ecad 1272 }
75cc2c35
MD
1273
1274 /* read timestamp begin from header */
2d0bea29 1275 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_begin"));
75cc2c35 1276 if (len_index >= 0) {
75cc2c35
MD
1277 struct definition *field;
1278
2d0bea29 1279 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1280 packet_index.timestamp_begin = get_unsigned_int(field);
75cc2c35
MD
1281 }
1282
1283 /* read timestamp end from header */
2d0bea29 1284 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("timestamp_end"));
75cc2c35 1285 if (len_index >= 0) {
75cc2c35
MD
1286 struct definition *field;
1287
2d0bea29 1288 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
f4c56ac2 1289 packet_index.timestamp_end = get_unsigned_int(field);
75cc2c35 1290 }
41e82e00
MD
1291
1292 /* read events discarded from header */
1293 len_index = struct_declaration_lookup_field_index(file_stream->parent.stream_packet_context->declaration, g_quark_from_static_string("events_discarded"));
1294 if (len_index >= 0) {
1295 struct definition *field;
1296
1297 field = struct_definition_get_field_from_index(file_stream->parent.stream_packet_context, len_index);
1298 packet_index.events_discarded = get_unsigned_int(field);
1299 }
0f980a35
MD
1300 } else {
1301 /* Use file size for packet size */
8c572eba 1302 packet_index.content_size = filestats.st_size * CHAR_BIT;
0f980a35 1303 /* Use content size if non-zero, else file size */
8c572eba 1304 packet_index.packet_size = packet_index.content_size ? : filestats.st_size * CHAR_BIT;
0f980a35 1305 }
546293fa
MD
1306
1307 /* Validate content size and packet size values */
1308 if (packet_index.content_size > packet_index.packet_size) {
7e18eedf 1309 fprintf(stderr, "[error] Content size (%" PRIu64 " bits) is larger than packet size (%" PRIu64 " bits).\n",
546293fa
MD
1310 packet_index.content_size, packet_index.packet_size);
1311 return -EINVAL;
1312 }
1313
7e18eedf
JN
1314 if (packet_index.packet_size > ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT) {
1315 fprintf(stderr, "[error] Packet size (%" PRIu64 " bits) is larger than remaining file size (%" PRIu64 " bits).\n",
1316 packet_index.content_size, ((uint64_t)filestats.st_size - packet_index.offset) * CHAR_BIT);
546293fa
MD
1317 return -EINVAL;
1318 }
1319
847bf71a
MD
1320 /* Save position after header and context */
1321 packet_index.data_offset = pos->offset;
0f980a35 1322
0f980a35
MD
1323 /* add index to packet array */
1324 g_array_append_val(file_stream->pos.packet_index, packet_index);
1325
8c572eba 1326 pos->mmap_offset += packet_index.packet_size / CHAR_BIT;
0f980a35
MD
1327 }
1328
847bf71a 1329 /* Move pos back to beginning of file */
06789ffd 1330 ctf_packet_seek(&pos->parent, 0, SEEK_SET); /* position for write */
847bf71a 1331
0f980a35
MD
1332 return 0;
1333}
1334
e28d4618 1335static
9e88d150 1336int create_trace_definitions(struct ctf_trace *td, struct ctf_stream_definition *stream)
e28d4618
MD
1337{
1338 int ret;
1339
1340 if (td->packet_header_decl) {
1341 struct definition *definition =
1342 td->packet_header_decl->p.definition_new(&td->packet_header_decl->p,
1343 stream->parent_def_scope, 0, 0, "trace.packet.header");
1344 if (!definition) {
1345 ret = -EINVAL;
1346 goto error;
1347 }
1348 stream->trace_packet_header =
1349 container_of(definition, struct definition_struct, p);
1350 stream->parent_def_scope = stream->trace_packet_header->p.scope;
1351 }
1352
1353 return 0;
1354
1355error:
1356 return ret;
1357}
1358
0f980a35
MD
1359/*
1360 * Note: many file streams can inherit from the same stream class
1361 * description (metadata).
1362 */
1363static
b086c01a 1364int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags,
20d0dcf9 1365 void (*packet_seek)(struct stream_pos *pos, size_t index,
b086c01a 1366 int whence))
0f980a35
MD
1367{
1368 int ret;
1369 struct ctf_file_stream *file_stream;
1370
46322b33 1371 ret = openat(td->dirfd, path, flags);
a569a564
MD
1372 if (ret < 0) {
1373 perror("File stream openat()");
0f980a35 1374 goto error;
a569a564 1375 }
0f980a35 1376 file_stream = g_new0(struct ctf_file_stream, 1);
b086c01a 1377
06789ffd
MD
1378 if (packet_seek) {
1379 file_stream->pos.packet_seek = packet_seek;
b086c01a 1380 } else {
06789ffd 1381 fprintf(stderr, "[error] packet_seek function undefined.\n");
b086c01a
JD
1382 ret = -1;
1383 goto error_def;
1384 }
1385
8563e754 1386 ctf_init_pos(&file_stream->pos, ret, flags);
2d0bea29 1387 ret = create_trace_definitions(td, &file_stream->parent);
e28d4618
MD
1388 if (ret)
1389 goto error_def;
25ccc85b
MD
1390 /*
1391 * For now, only a single slock is supported.
1392 */
1393 file_stream->parent.current_clock = td->single_clock;
0f980a35
MD
1394 ret = create_stream_packet_index(td, file_stream);
1395 if (ret)
1396 goto error_index;
1397 /* Add stream file to stream class */
2d0bea29
MD
1398 g_ptr_array_add(file_stream->parent.stream_class->streams,
1399 &file_stream->parent);
0f980a35
MD
1400 return 0;
1401
1402error_index:
2d0bea29
MD
1403 if (file_stream->parent.trace_packet_header)
1404 definition_unref(&file_stream->parent.trace_packet_header->p);
e28d4618 1405error_def:
46322b33 1406 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1407 close(file_stream->pos.fd);
1408 g_free(file_stream);
1409error:
65102a8c
MD
1410 return ret;
1411}
1412
bbefb8dd 1413static
5b80ddfb 1414int ctf_open_trace_read(struct ctf_trace *td,
8c250d87 1415 const char *path, int flags,
20d0dcf9 1416 void (*packet_seek)(struct stream_pos *pos, size_t index,
ae23d232 1417 int whence), FILE *metadata_fp)
bbefb8dd
MD
1418{
1419 int ret;
65102a8c
MD
1420 struct dirent *dirent;
1421 struct dirent *diriter;
1422 size_t dirent_len;
bbefb8dd 1423
46322b33 1424 td->flags = flags;
bbefb8dd
MD
1425
1426 /* Open trace directory */
46322b33
MD
1427 td->dir = opendir(path);
1428 if (!td->dir) {
3394d22e 1429 fprintf(stderr, "[error] Unable to open trace directory.\n");
bbefb8dd
MD
1430 ret = -ENOENT;
1431 goto error;
1432 }
1433
46322b33
MD
1434 td->dirfd = open(path, 0);
1435 if (td->dirfd < 0) {
3394d22e 1436 fprintf(stderr, "[error] Unable to open trace directory file descriptor.\n");
a569a564
MD
1437 perror("Trace directory open");
1438 ret = -errno;
65102a8c
MD
1439 goto error_dirfd;
1440 }
5b80ddfb
MD
1441 strncpy(td->path, path, sizeof(td->path));
1442 td->path[sizeof(td->path) - 1] = '\0';
0f980a35 1443
65102a8c
MD
1444 /*
1445 * Keep the metadata file separate.
1446 */
bbefb8dd 1447
06789ffd 1448 ret = ctf_open_trace_metadata_read(td, packet_seek, metadata_fp);
65102a8c
MD
1449 if (ret) {
1450 goto error_metadata;
1451 }
bbefb8dd
MD
1452
1453 /*
1454 * Open each stream: for each file, try to open, check magic
1455 * number, and get the stream ID to add to the right location in
1456 * the stream array.
bbefb8dd
MD
1457 */
1458
65102a8c 1459 dirent_len = offsetof(struct dirent, d_name) +
46322b33 1460 fpathconf(td->dirfd, _PC_NAME_MAX) + 1;
bbefb8dd 1461
65102a8c 1462 dirent = malloc(dirent_len);
bbefb8dd 1463
65102a8c 1464 for (;;) {
46322b33 1465 ret = readdir_r(td->dir, dirent, &diriter);
65102a8c 1466 if (ret) {
3394d22e 1467 fprintf(stderr, "[error] Readdir error.\n");
65102a8c 1468 goto readdir_error;
65102a8c
MD
1469 }
1470 if (!diriter)
1471 break;
d8ea2d29
MD
1472 /* Ignore hidden files, ., .. and metadata. */
1473 if (!strncmp(diriter->d_name, ".", 1)
65102a8c
MD
1474 || !strcmp(diriter->d_name, "..")
1475 || !strcmp(diriter->d_name, "metadata"))
1476 continue;
06789ffd
MD
1477 ret = ctf_open_file_stream_read(td, diriter->d_name,
1478 flags, packet_seek);
dc48ecad 1479 if (ret) {
3394d22e 1480 fprintf(stderr, "[error] Open file stream error.\n");
dc48ecad
MD
1481 goto readdir_error;
1482 }
65102a8c 1483 }
bbefb8dd 1484
65102a8c 1485 free(dirent);
bbefb8dd 1486 return 0;
65102a8c
MD
1487
1488readdir_error:
1489 free(dirent);
1490error_metadata:
46322b33 1491 close(td->dirfd);
65102a8c 1492error_dirfd:
46322b33 1493 closedir(td->dir);
bbefb8dd
MD
1494error:
1495 return ret;
1496}
1497
e9378815 1498static
5b80ddfb 1499struct trace_descriptor *ctf_open_trace(const char *path, int flags,
20d0dcf9 1500 void (*packet_seek)(struct stream_pos *pos, size_t index,
ae23d232 1501 int whence), FILE *metadata_fp)
bbefb8dd 1502{
46322b33 1503 struct ctf_trace *td;
bbefb8dd
MD
1504 int ret;
1505
2715de36
MD
1506 /*
1507 * If packet_seek is NULL, we provide our default version.
1508 */
1509 if (!packet_seek)
1510 packet_seek = ctf_packet_seek;
1511
46322b33 1512 td = g_new0(struct ctf_trace, 1);
bbefb8dd 1513
8c572eba 1514 switch (flags & O_ACCMODE) {
bbefb8dd 1515 case O_RDONLY:
b61922b5 1516 if (!path) {
3394d22e 1517 fprintf(stderr, "[error] Path missing for input CTF trace.\n");
b61922b5
MD
1518 goto error;
1519 }
06789ffd 1520 ret = ctf_open_trace_read(td, path, flags, packet_seek, metadata_fp);
bbefb8dd
MD
1521 if (ret)
1522 goto error;
1523 break;
989c73bc 1524 case O_RDWR:
3394d22e 1525 fprintf(stderr, "[error] Opening CTF traces for output is not supported yet.\n");
46322b33 1526 goto error;
bbefb8dd 1527 default:
3394d22e 1528 fprintf(stderr, "[error] Incorrect open flags.\n");
bbefb8dd
MD
1529 goto error;
1530 }
1531
46322b33 1532 return &td->parent;
bbefb8dd
MD
1533error:
1534 g_free(td);
1535 return NULL;
1536}
1537
f571dfb1
JD
1538
1539void ctf_init_mmap_pos(struct ctf_stream_pos *pos,
1540 struct mmap_stream *mmap_info)
1541{
1542 pos->mmap_offset = 0;
1543 pos->packet_size = 0;
1544 pos->content_size = 0;
1545 pos->content_size_loc = NULL;
1546 pos->fd = mmap_info->fd;
1547 pos->base = 0;
1548 pos->offset = 0;
1549 pos->dummy = false;
1550 pos->cur_index = 0;
1551 pos->packet_index = NULL;
1552 pos->prot = PROT_READ;
1553 pos->flags = MAP_PRIVATE;
1554 pos->parent.rw_table = read_dispatch_table;
1555 pos->parent.event_cb = ctf_read_event;
1556}
1557
1558static
1559int prepare_mmap_stream_definition(struct ctf_trace *td,
1560 struct ctf_file_stream *file_stream)
1561{
f380e105 1562 struct ctf_stream_declaration *stream;
f571dfb1
JD
1563 uint64_t stream_id = 0;
1564 int ret;
1565
1566 file_stream->parent.stream_id = stream_id;
1567 if (stream_id >= td->streams->len) {
3394d22e 1568 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1569 "in metadata.\n", stream_id);
1570 ret = -EINVAL;
1571 goto end;
1572 }
1573 stream = g_ptr_array_index(td->streams, stream_id);
1574 if (!stream) {
3394d22e 1575 fprintf(stderr, "[error] Stream %" PRIu64 " is not declared "
f571dfb1
JD
1576 "in metadata.\n", stream_id);
1577 ret = -EINVAL;
1578 goto end;
1579 }
1580 file_stream->parent.stream_class = stream;
1581 ret = create_stream_definitions(td, &file_stream->parent);
1582end:
1583 return ret;
1584}
1585
1586static
1587int ctf_open_mmap_stream_read(struct ctf_trace *td,
1588 struct mmap_stream *mmap_info,
20d0dcf9 1589 void (*packet_seek)(struct stream_pos *pos, size_t index,
f571dfb1
JD
1590 int whence))
1591{
1592 int ret;
1593 struct ctf_file_stream *file_stream;
1594
1595 file_stream = g_new0(struct ctf_file_stream, 1);
1596 ctf_init_mmap_pos(&file_stream->pos, mmap_info);
1597
06789ffd 1598 file_stream->pos.packet_seek = packet_seek;
f571dfb1
JD
1599
1600 ret = create_trace_definitions(td, &file_stream->parent);
1601 if (ret) {
1602 goto error_def;
1603 }
1604
1605 ret = prepare_mmap_stream_definition(td, file_stream);
1606 if (ret)
1607 goto error_index;
1608
1609 /* Add stream file to stream class */
1610 g_ptr_array_add(file_stream->parent.stream_class->streams,
1611 &file_stream->parent);
1612 return 0;
1613
1614error_index:
1615 if (file_stream->parent.trace_packet_header)
1616 definition_unref(&file_stream->parent.trace_packet_header->p);
1617error_def:
1618 g_free(file_stream);
1619 return ret;
1620}
1621
1622int ctf_open_mmap_trace_read(struct ctf_trace *td,
1623 struct mmap_stream_list *mmap_list,
20d0dcf9 1624 void (*packet_seek)(struct stream_pos *pos, size_t index,
f571dfb1
JD
1625 int whence),
1626 FILE *metadata_fp)
1627{
1628 int ret;
1629 struct mmap_stream *mmap_info;
1630
06789ffd 1631 ret = ctf_open_trace_metadata_read(td, ctf_packet_seek, metadata_fp);
f571dfb1
JD
1632 if (ret) {
1633 goto error;
1634 }
1635
1636 /*
1637 * for each stream, try to open, check magic number, and get the
1638 * stream ID to add to the right location in the stream array.
1639 */
3122e6f0 1640 bt_list_for_each_entry(mmap_info, &mmap_list->head, list) {
06789ffd 1641 ret = ctf_open_mmap_stream_read(td, mmap_info, packet_seek);
f571dfb1 1642 if (ret) {
3394d22e 1643 fprintf(stderr, "[error] Open file mmap stream error.\n");
f571dfb1
JD
1644 goto error;
1645 }
1646 }
1647
1648 return 0;
1649
1650error:
1651 return ret;
1652}
1653
1654static
1655struct trace_descriptor *ctf_open_mmap_trace(
1656 struct mmap_stream_list *mmap_list,
20d0dcf9
MD
1657 void (*packet_seek)(struct stream_pos *pos, size_t index,
1658 int whence),
f571dfb1
JD
1659 FILE *metadata_fp)
1660{
1661 struct ctf_trace *td;
1662 int ret;
1663
1664 if (!metadata_fp) {
1665 fprintf(stderr, "[error] No metadata file pointer associated, "
1666 "required for mmap parsing\n");
1667 goto error;
1668 }
06789ffd
MD
1669 if (!packet_seek) {
1670 fprintf(stderr, "[error] packet_seek function undefined.\n");
f571dfb1
JD
1671 goto error;
1672 }
1673 td = g_new0(struct ctf_trace, 1);
06789ffd 1674 ret = ctf_open_mmap_trace_read(td, mmap_list, packet_seek, metadata_fp);
f571dfb1
JD
1675 if (ret)
1676 goto error_free;
1677
1678 return &td->parent;
1679
1680error_free:
1681 g_free(td);
1682error:
1683 return NULL;
1684}
1685
0f980a35
MD
1686static
1687void ctf_close_file_stream(struct ctf_file_stream *file_stream)
1688{
46322b33 1689 ctf_fini_pos(&file_stream->pos);
0f980a35
MD
1690 close(file_stream->pos.fd);
1691}
1692
e9378815 1693static
46322b33 1694void ctf_close_trace(struct trace_descriptor *tdp)
bbefb8dd 1695{
46322b33 1696 struct ctf_trace *td = container_of(tdp, struct ctf_trace, parent);
0f980a35
MD
1697 int i;
1698
46322b33
MD
1699 if (td->streams) {
1700 for (i = 0; i < td->streams->len; i++) {
f380e105 1701 struct ctf_stream_declaration *stream;
0f980a35 1702 int j;
e9378815 1703
46322b33 1704 stream = g_ptr_array_index(td->streams, i);
e9378815
MD
1705 if (!stream)
1706 continue;
2d0bea29 1707 for (j = 0; j < stream->streams->len; j++) {
0f980a35 1708 struct ctf_file_stream *file_stream;
2d0bea29 1709 file_stream = container_of(g_ptr_array_index(stream->streams, j), struct ctf_file_stream, parent);
0f980a35
MD
1710 ctf_close_file_stream(file_stream);
1711 }
1712
1713 }
46322b33 1714 g_ptr_array_free(td->streams, TRUE);
0f980a35 1715 }
e003ab50
JD
1716
1717 if (td->event_declarations) {
1718 for (i = 0; i < td->event_declarations->len; i++) {
1719 struct bt_ctf_event_decl *event;
1720
1721 event = g_ptr_array_index(td->event_declarations, i);
64c2c249
JD
1722 if (event->context_decl)
1723 g_ptr_array_free(event->context_decl, TRUE);
1724 if (event->fields_decl)
1725 g_ptr_array_free(event->fields_decl, TRUE);
1726 if (event->packet_header_decl)
1727 g_ptr_array_free(event->packet_header_decl, TRUE);
1728 if (event->event_context_decl)
1729 g_ptr_array_free(event->event_context_decl, TRUE);
1730 if (event->event_header_decl)
1731 g_ptr_array_free(event->event_header_decl, TRUE);
1732 if (event->packet_context_decl)
1733 g_ptr_array_free(event->packet_context_decl, TRUE);
e003ab50
JD
1734 g_free(event);
1735 }
1736 g_ptr_array_free(td->event_declarations, TRUE);
1737 }
46322b33 1738 closedir(td->dir);
bbefb8dd
MD
1739 g_free(td);
1740}
1741
98a04903
JD
1742static
1743void ctf_set_context(struct trace_descriptor *descriptor,
1744 struct bt_context *ctx)
1745{
1746 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1747 parent);
1748
1749 td->ctx = ctx;
1750}
1751
1752static
1753void ctf_set_handle(struct trace_descriptor *descriptor,
1754 struct bt_trace_handle *handle)
1755{
1756 struct ctf_trace *td = container_of(descriptor, struct ctf_trace,
1757 parent);
1758
1759 td->handle = handle;
1760}
1761
7fb21036 1762void __attribute__((constructor)) ctf_init(void)
fc93b2bd
MD
1763{
1764 int ret;
1765
4c8bfb7e 1766 ctf_format.name = g_quark_from_static_string("ctf");
fc93b2bd
MD
1767 ret = bt_register_format(&ctf_format);
1768 assert(!ret);
1769}
698f0fe4
MD
1770
1771/* TODO: finalize */
This page took 0.208166 seconds and 4 git commands to generate.