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