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