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