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