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