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