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